C Language File operation and application (i)

Source: Internet
Author: User
Tags function prototype

Opening and closing of files:

    1. Opening of the file (fopen function)

(1). Function Prototypes:

FILE *fopen (char *path,char *mode);

(2). Function Description: Open the file specified by path according to mode, if the path file is not found: if it is opened in read mode, the failure will open, and if it is opened, a new file will be created by the name specified by path;

(3). Parameter description: path is a character pointer that points to the filename string of the file to be opened or established. mode is a character pointer to a file-handling string, as shown in the following table;

How files are used

Meaning

"R" (read-only)

Open a text file for input

"W" (write only)

Open a text file for output

"A" (append)

Add data to the end of a text file

"RB" (Read only)

Open a binary file for input

"WB" (write only)

Open a binary file for output

"AB" (write only)

Add data to the end of a binary file

"R+" (Read and write)

Open a text file for read/write

"w+" (Read and write)

Create a new text file for read/write

"A +" (Read/write)

Open a text file for read/write

"Rb+" (Read and write)

Open a binary file for read/write

"Wb+" (Read and write)

Create a new binary file for read/write

"Ab+" (Read and write)

Open a binary file for read/write

    1. Close of file (Fclose function)

(1). Function Prototypes:

int fclose (FILE *FP);

(2). Function Description: Closes the file pointed to by the FP. (It is important to remember to close after the file is opened, because it may lose data in other operations, etc.)

(3). Parameter description: FP is the file pointer of the open file;

  For example:

//defines a pointer named FP fileFILE *FP;//determine if a file named test is read to fail//because opening the file may fail, so try to add this judgment;if((Fp=fopen ("Test","R")) = = NULL)//Open operation not successful{printf ("The file can not be opened.\n"); exit (1);//to end the execution of a program}fclose (FP); //to close an open file:

File read and write operations:

!--[if!supportlists]-->1. read a character from a file (fgetc function

function prototype: int fgetc (FILE *fp);

!--[if!supportlists]-->2. Write a character to the file (FPUTC function

function prototype: int FPUTC (char ch,file *FP);// writes the character ch to fp points to the file;

!--[if!supportlists]-->3. read a string from the file (fgets function

function prototype: char *fgets (char *str,int n,file *FP);// read from fp file n-1 characters into str character array ( or read to end of file , and at the end of the str array, add

!--[if!supportlists]-->4. Write a string to the file (fputs function

Function prototype:int fputs (char *str,file *FP);// writes a string in str to a file of FP reference

5. fscanffunctions and fprintf functions

Function prototype:int fscanf (FILE *fp,char *format,arg);//format is a format string,arg is an input list /// For example : fscanf (FP, " %d ", &i);

          int fprintf (FILE *fp,char *format,...); / For example :fprintf (FP, "%s", str);

In fact, fscanf and fprintf function is similar to scanf,printf function, just one from the keyboard to obtain data from a file, from a write data to the screen one write file.

C Language File operation and application (i)

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.