File Operation Summary

Source: Internet
Author: User
Tags rewind

1. Concepts and Definitions

The C language introduces the concept of streaming files, that is, the files are considered to be composed of character (byte) Data Order. According to the data organization form, they are divided into ASCII files and binary files.

(1) ASCII file: a text file. Each byte stores an ascii code, representing a character.

(2) binary file: the data in the memory is input to the disk as is in the memory storage format.

The output in ASCII format corresponds to characters one by one. One byte represents one character. Therefore, it is easy to process characters one by one, and it is also easy to output characters, but it generally occupies a large storage space, it also takes time to convert the binary and ASCII codes;

Output in binary format can save storage space and conversion time, but one byte does not correspond to one character and cannot be output directly. Generally, intermediate results must be temporarily stored in the external storage, if you want to input Memory later, you can save binary files.

 

2. Open and Close a file

 

Open: open a file using the fopen () function

File * FP;

Fp = fopen (file name, file method );

For example, FP = fopen ("C: \ exp \ 1.txt"," rb "); pay attention to escape characters

Common forms

If (FP = fopen ("file1", "R") = NULL)

{

Printf ("can not open the file ");

Exit (0)

}

If the file cannot be opened, a null pointer is returned;

 

When you input a text file to a computer, convert the carriage return linefeed into a linefeed. When the word is output, convert the linefeed to the carriage return and linefeed. This type of conversion is not performed when a binary file is used. The data format in the memory is exactly the same as that in the data output to an external file.

Close: fclose (FP); if it is disabled, 0 is returned; otherwise, EOF (-1) is returned );

 

3. file read/write

 

(1) fputc (CH, FP); the output returns the output characters successfully, and the Failure Returns EOF;

Ch = fgetc (FP); judge whether the file is complete

Ch = fgetc (FP );

While (Ch! = EOF)

{

Putchar (CH );

Ch = fgetc (FP );

}

Another method for determining the end of a file, feof (FP), was discussed in detail in the previous article.

 

(2) fread function and fwrite () function

Fread (bufffer, size, Count, FP); reads data of Count size from FP to buffer

Fwrite (buffer, size, Count, FP); write count size data to fp from Buffer

Buffer is a pointer;

Fread and fwrite functions are generally used for input and output of binary files, because they process input and output data based on the length of data blocks, in the case of character conversion, it is likely to be different from the original situation;

 

(3) fscanf and fprintf

Fprintf (File pointer, Format String, output list );

Fscanf (File pointer, Format String, output list );

These two functions are convenient for reading and writing disk files and are easy to understand, but the ASCII code must be converted into binary (in memory) during input ), the output must be converted back (to display text files). It takes a lot of time and is not suitable for frequent data exchanges between memory and disk;

 

4. Other read/write Functions

(1) putw () and getw () Functions

Putw (10, FP );

I = getw (FP );

Used to read and write a word (integer) from a disk file );IfC compiler library functions do not include these two functions. You can also implement them by yourself:

Putw (int I, file * FP)

{

Char * s;

S = & I;

Putc (s [0], FP );

Putc (s [1], FP );

Return (I );

}

(2) functions for reading and writing other types of data

We can also write it by ourselves, for example, defining a function that reads and writes real numbers (in binary mode) to disk files;

Void putfloat (float num, file * FP)

{

Char * s;

Int count;

S = & num;

For (COUNT = 0; count <4; count ++) Put (s [count], FP );

}

(3) fgets () and fputs () functions,

Fgets (STR, N, FP); enter n-1 characters, and then add '\ 0' at the end. If EOF is encountered before n-1 characters are read, the reading ends; if this function succeeds, the first address of STR is returned;

Fputs ("China", FP); '\ 0' at the end of the string is not output. If the output is successful, the function value is 0; otherwise, it is EOF;

 

5. File locating

 

(1) Rewind () function

Rewind (FP); the file pointer returns the beginning of the file

 

(2) fseek () function and random read/write

Fseek (<file type pointer >,< displacement >,< start point> );

Start Point: 0 file start 1 current location 2 file end

The displacement is long type, in bytes, such as 100l and-10l;

 

(3) I = ftell (FP); get the current location;

If the returned value is-1, an error occurs.

I = ftell (FP );

If (I =-1l) printf ("error ");

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.