Basic c: files and operations, basic c operations

Source: Internet
Author: User
Tags rewind

Basic c: files and operations, basic c operations
File opening and closing

First, define a FILE pointer type in the format of FILE * FILE pointer name ;;

FILE * fp; // fp is the defined FILE pointer.

 

● Open the file fopen () function in the format of fp = fopen ("file name", "open mode ");

Fp = fopen ("c: \ myfile", "r"); // The opening method is described below

 

The path descriptor "\" cannot be used directly in the file name, and must be in the form of escape characters "\\". The first "\" in "\" is an escape character, and the second represents a path descriptor.

 

● Close the file fcolse () function. Format: fclose (File pointer );

fclose(fp);

Function: If fclose is normal, 0 is returned. Otherwise, EOF (value:-1) is returned, indicating that an error has occurred.

● File opening method:

There are 12 types of data, which are composed of five characters: r (read), w (write), a (append), B (binary), and + (read/write.

"R", "w", "a", "r +", "w +", "a +": these six open text files

"Rb", "wb", "AB", "rb +", "wb +", "AB +": these six open binary files

"R" Open a text file to read the file
"W" Open a text file, you can write the file, first cut the file length to 0. If the file does not exist, create
"" Open a text file, write the file, and append the content to the end of the existing file. If the file does not exist, create
"R +" Open a text file, which can be updated (read and written), that is, the file can be read and written.
"W +" Open a text file and update it (read and write). If the file exists, cut it to 0. If the file does not exist, create
"A +" Open a text file, update (read and write), and Append content to the end of an existing file. If the file does not exist, create it first. You can read the entire file, however, only content can be appended during write operations.
"Rb", "wb", "AB", "rb +", "wb +", "AB +", "r + B", "w + B ", "a + B" Similar to the previous mode, the file is opened in binary instead of text mode.

Note: If you open any file in "w" mode, the file content will be deleted so that the program can start to operate on an empty file.

 

● File end determination feof () function, format: feof (File pointer)

Here is an example to help you understand:

While (! Feof (fp) // we have added the end sign-1 {c = fgetc (fp); print ("when it reaches the end of the file, this section of text does not show ");} // the meaning of this section of the program is: when it is! Feof (fp) is 1. When a byte of data is read from the current position and assigned to variable c, the value of feof (fp) is 1, feof (fp) is 0, and the cycle ends

 

Feof () function: test whether the internal read/write position pointer pointing to the file pointer has reached the end of the file. 1 is returned when the file ends. Otherwise, 0 is returned.

File read/write 1. character, string read/write

Write character function fputc. function: Write a character to this disk file. If the output is successful, the output character is returned. Otherwise, the EOF is returned. Format:

Fputc (ch, fp); // output the ch value to the file pointed to by fp.

 

Ch: the character to be output. It can be a character constant or a character variable.

 

The fgetc function reads a character from a specified file. The file must be opened in read or read/write mode. If the read is successful, the read character is returned. Otherwise, the EOF is returned. Format:

Ch = fgetc (fp); // If the read succeeds, the returned character is assigned to ch.

 

Write string function fputs, format: fputs ("string", file pointer); function: Write a string of characters to the specified file. If the write is successful, 0 is returned. Otherwise, a non-0 value is returned.

fputs("Welcome to my blog", fp);

Note: The fputs function removes the null characters in the string written to the file. The ptus function replaces the null character with the newline character for output.

 

Read string function, in the format of fgets (string, n, fp); function: Read n-1 characters from the file pointed to by fp to store the first parameter. If you encounter a line break or EOF, end.

Fgets (str, n, fp); // str is a character array. Reads n-1 characters from the file pointed to by fp and stores them in the str array.

Note: After the string is read, add the '\ 0' character at the end. The return value returned by the fgets function is the first address of the string.

 

 

2. Read and Write Data blocks

Data Block Write File function fwrite (), format:

fwrite(buffer, size, count, fp);

Buffer: A pointer used to indicate the address with output data.

Size: the number of bytes to be written each time.

Count: the number of size data items written.

Note: After completing the write operation (fwrite (), you must close the file (fclose ())

 

The read file function fread () of the data block. format:

fread(buffer, size, count, fp);

Count: the number of data items in size read.

 

3. format the read/write file Function

Read File fprintf (), format: fprintf (File pointer, "format control", output list );

Fprintf (fp, "% d, % d", a, B); // Write a and B to the file pointed to by fp.

Note: The fprintf () function always converts the representation of the output item in the memory to a string in the specified format and then writes it to the specified file.

 

Write File fscanf (), format: fscanf (File pointer, "format control", address list );

Fprintf (fp, "% d, % d", & a, & B); // read the data in the file pointed to by fp and store it in a, B

Note: The data read by the fscanf () function from a file must exist in the string form (text form.

 

File positioning ● rewind () function, in the format of rewind (File pointer); function: re-return the read/write position of the file pointer to the beginning of the file.
rewind(fp);
● Fseek () function, in the format of fseek (File pointer, displacement w, starting point s); function: Move the file pointer to w bytes away from the starting point.
Fseek (fp, 2, 0); // move the file pointer to 2 bytes away from the starting point 0 offset
W: If w is positive, it indicates moving toward the end of the file. If w is negative, it indicates moving toward the file header. If w is 0, it indicates not moving. S: It can be 0, 1, 2; s = 0 indicates the file header, s = 0 indicates the current position, and s = 0 indicates the end of the file. ● Ftell () function, in the format of ftell (File pointer); function: obtains the current position in the stream file, expressed by the displacement relative to the beginning of the file. If the ftell () function returns-1L, an error occurs.
I = ftell (fp); // store the current location to the variable iif (I =-1L) printf ("error \ n "); // if an ERROR occurs in the called function (if this file does not exist), an ERROR is output.
File detection common file detection functions include two ferror () and clearerr () ferror () functions. function: If the return value of the ferror () function is 0, no error is reported, if the value is not 0, an error occurs. Format:
ferror(fp)

Clearerr () function. function: sets the file error mark and end mark to 0. Format:
clearerr(fp)

 

20170104

 

Related Article

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.