C Language File Operation summary

Source: Internet
Author: User
Tags fread

The difference between a text file and a binary file

As you know, computer storage is physically binary, so the difference between a text file and a binary file is not physical, but logical. The two are only different at the coding level.
In simple terms,

    • Text files are character-encoded files, common encodings are ASCII-encoded, Unicode-encoded, and so on.
    • Binary files are value-coded files, and you can specify what a value is (a process that can be considered a custom encoding), depending on your application.

From the above can be seen text file is basically fixed length encoding, based on character Fuma, each character in the specific encoding is fixed, ASCII code is 8 bits of encoding, Unicode generally accounted for 16 bits.
Binary files can be thought of as variable-length encodings, because it is the value code, and how many bits represent a value that you decide entirely.

C Text read-write and binary read and write
It should be said that C text read and write and binary read and write is a programming level problem, and the specific operating system, so "text-to-read file must be a text file, binary read and write files must be binary files" such views are wrong. The following narrative does not explicitly indicate the operating system type, all alluding to Windows.

The difference between read and write of C and binary read and write is only reflected in the processing of carriage return newline.
When the text is written, each time a "\ n" (0AH newline) is encountered, it is replaced with a ' \ r \ n ' (0d0ah, carriage return), and then written to the file, and when the text is read, it each encounters a "\ r \ n" To change it to "\ n" and then to the read buffer.
When binary reads and writes, it does not have any transformations and writes the data in the write buffer directly to the file.

For documents with the content "ab123\r\n" (0D 0A),
PF1 = fopen ("F:\\1.txt", "R"), or PF1 = fopen ("F:\\1.txt", "RB");
for (int i=0;i <6;i++) {
Fread (&A[I],1,1,PF1);
printf ("%0x", A[i]);
}
Fclose (PF1);//Close file

The results were as follows:
The 0 A and the 0D.

Second, c file operation

    1. File pointers

FILE *FP;

2. Functions

fopen ();

such as: FP = fopen ("File_pathe_name", "R");

Parameter 1: File name,

Parameter 2: Open mode

"R" (Read Only) opens a text file "W" (write only) for input and opens a text file "a" (append) to the end of the text file to add data
"RB" (read only) open a binary file for input "WB" (write only) open a binary file for output

"AB" (append) adds data to the end of a binary file
"r+" (read/write) open a text file for read/write "w+" (read/write) to create a new text file for read/write "A +" (read/write) open a text file "rb+" (read/write) for read/write open a binary file

"wb+" (read/write) to create a new binary file for read/write "ab+" (read/write) open a binary file for reading/writing

Parameter 3: Return value, pointer to the file

When there is an error opening the file, NULL is returned, which can be used in the program to determine whether the open file was successful.

FILE *FP; if (fp = open ("file_name""RB") = = NULL) {    printf ( " Open file failed!\n " );  }

Fclose ();

Fclose (FP);

The shutdown succeeded returns 0, and the failure returned non 0.

File read and write operations

The C language provides a variety of functions for reading and writing files: character reading and writing functions: Fgetc and FPUTC string read and write functions: Fgets and fputs data block read and write functions: Freed and Fwrite formatted read-write functions: fscanf and fprintf You need to include the header file with the above function stdio.h

Determine if the end of the file is read:

EOF is not an output character with a value of-1, when the character value read in equals

-1, indicates that the read-in is not a normal character but a file terminator

ch = fgetc (FP);  while (ch = EOF) {   putchar (ch);    = fgetc (FP);}

Read and write Data blocks

Fread (BUFFER,SIZE,COUNT,FP);

Description: Buffer: A pointer to the first address of the storage data space. Size: The number of bytes of data to read
Count: How many size bytes of data to read
FP: pointing to a file for read operations

Fwrite (BUFFER,SIZE,COUNT,FP);

Description: Buffer: A pointer to the first address of the storage data space. Size: Number of bytes to write data
Count: How many size bytes of data to write
FP: pointing to a file for write operations

Formatted read-write function call:

fprintf (file pointers, format strings, output table columns);

FSCANF (file pointer, format string, input table column);

function function:

   Read-in or output character examples from a disk file  :

fprintf (FP, "%d,%6.2f", i,t); FSCANF (FP, "%d,%f", &i,&t);

Description

    1. A) fscanf, fprintf functions and scanf, printf letters used in the preceding

      The function of the number is similar to that of the formatted read-write function.

    2. b) The difference between the two is that the fscanf, fprintf function read-write object does not

      is a keyboard and a monitor, but a disk file.

    3. c) using fprintf, fscanf function to read and write disk files, easy to use,

      However, type conversion is required at input and output, which consumes higher resources.

    4. D) In cases where memory and disk frequently exchange data, it is best not to use the fprintf and FSCANF functions, but with the fread and fwrite functions.

Locating and random reading and writing of files

Rewind (FP);

Fseek ();

Fseek functions (typically used in binary files)

To define a function:

int fseek (file type pointer, displacement amount, starting point); function function:

Move the Read and write location of the file stream. ? Description: Starting position

File at the beginning Seek_set 0 file current position seek_cur 1 file End Seek_end 2-bit shift: The number of bytes moved forward and backward, starting at base point. The general requirement is a long type.

C Language File Operation summary

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.