Linux application Development-file programming-Library functions

Source: Internet
Author: User
Tags fread

---restore content starts---

C library function file operation is independent of the specific system platform, the portability is good.

Library functions-Create and open

File*fopen (const char*filename,const Char*mode)

Mode: Open modes

Common open Mode:

R,RB: Read-only mode open

W,WB: Write-only open, if the file does not exist, create the file

A,ab: Append mode open, if file does not exist, create the file

r+,r+b,rb+: Read and write mode open

w+,w+b,wh+: Read and write mode open, if the file does not exist, create the file

a,a+b,ab+: Read and append mode open, if the file does not exist, create the file

B is used to differentiate between binary and text files, which is differentiated in Dos,window, where Linux does not differentiate between binary file box text files.

Library functions-Read

size_t fread (void*ptr,size_t size,size_t n,file *stream);

Reads n fields from a file pointed to by stream, each field is a size byte, and puts the read data into a character array referred to by PTR, returning the actual number of bytes read.

Library Functions-Write

size_t fwrite (void*ptr,size_t size,size_t n,file *stream);

Read characters:

int fgetc (file*stream);

---restore content ends---

C library function file operation is independent of the specific system platform, the portability is good.

Library functions-Create and open

File*fopen (const char*filename,const Char*mode)

Mode: Open modes

Common open Mode:

R,RB: Read-only mode open

W,WB: Write-only open, if the file does not exist, create the file

A,ab: Append mode open, if file does not exist, create the file

r+,r+b,rb+: Read and write mode open

w+,w+b,wh+: Read and write mode open, if the file does not exist, create the file

a,a+b,ab+: Read and append mode open, if the file does not exist, create the file

B is used to differentiate between binary and text files, which is differentiated in Dos,window, where Linux does not differentiate between binary file box text files.

Read

size_t fread (void*ptr,size_t size,size_t n,file *stream);

Write:

size_t fwrite (void*ptr,size_t size,size_t n,file *stream);

Read characters

int fgetc (file*stream);

Example:

#include <stdio.h>

Main ()

{

FILE*FP;

Char ch;

if ((Fp=fopen ("C1.txt", "RT")) ==null)

{

printf ("\ncannot open file strike any key exit");

Getch ();

Exit (1);

}

CH=FGETC (FP);

while (CH!=EOP)

{

Putchar (CH);

CH=FGETC (FP);

}

Fclose (FP);

}

Write character

int fputs (Inyt c,file*stream)

Example:

#include <stdio.h>

Main ()

{

FILE*FP;

Char ch;

if ((Fp=fopen ("string", "w+")) ==null)

{

printf ("\ncannot open file, strike any key exit");

Getch ();

Exit (1);

}

printf ("Input a string:\n");

Ch=fgetchar ();

while (ch!= ' \ n ')

{

FPUTC (CH,FP);

Ch=getchar ();

}

printf ("\ n");

Fclose (FP);

}


Format read:

FSCANF (File*stream,char*format[,argument ...])

Format input from a stream

#include <stdlib.h>

#include <stdio.h>

int mian (void)

{

int i;

printf ("Input an Interger:");

if (fscanf (stdin, "%d", &i))

printf ("The integer read was:%i\n", i);

  

}

Format write:

fprintf (File*stream,char*format[,argument ...])

Format the output from one stream

#include <stdlib.h>

#include <process.h>

File*stream;

int mian (void)

{

int i=10;

Double fp=1.5;

Char s[]= "This is a string";

Char c= ' \ n ';

Stream=fopen ("Fprintf.out", "" W);

fprintf (Stream, "%s%c", s,c);

fprintf (Stream, "%d\n", I);

fprintf (Stream, "%f\n", FP);

Fclose (stream);

  

}

Positioning:

int fseek (File*stream,long offset,int whence);

Whence:

Seek_set

Seek_cur

Seek_end

Path obtained:

CHAR*GETCWD (char*buffer,size_t size);

Copy the path to buffer, if buffer is too small, return-1;

To create a directory:

int mkdir (char*dir,int mode);

0 returns success, 1 indicates an error.

Linux application Development-file programming-Library functions

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.