C language-file operations and applications (1)

Source: Internet
Author: User

1. From the perspective of file encoding,

Files are divided into text files and binary files;

Devices in C language are also processed as files, such as input device (keyboard) stdin and output device (Display Device) stdout.

2. For file operations, you can use the pointer variable pointing to the struct variable of the file.

Declare the pointer to the file struct variable in the program.

File opening function fopen

File * fopen (char * filename, char * Mode)

If you want to open a text file named k.txt in the \ vcvisual directory for read operations, write

Fopen ("\ Vc \ k.txt", "R ");

File close function fclose

Int fclose (File * stream );

Call method:

Fclose (FP );

If the object is successfully closed, 0 is returned. If the object fails to be closed, a non-0 value is returned.

File character reading function fgets

Int fgetc (File * FP)

Call Method

Ch = fgets (FP );

The function reads a character from the position indicated by FP and returns the ASCII value of the character to Ch.

File character writing function fputs

Int fputs (char CH, file * FP );

Call method:

Fputc (CH, FP );

Write a character (CH value) at the position pointed by FP. The write is successful. The return value of the function is the ASCII value of the character. If the write is unsuccessful, EOF (-1) is returned );

File formatting READ function

Int fscanf (File * FP, char * format, memory address list );

Start from the current position of the file pointed to by FP, read data in the given format, and assign it to the memory variable. The data flows from the file to the memory. At the same time, the read/write position pointer is moved back.

File formatting and writing functions

Int fprintf (File * stream, char * format, variable list );

Write the data in the Variable list to the current position pointed to by the file FP in a given format.

File data block READ function

Int fread (void * buffer, int size, int N, file * FP );

Starting from the current position of the file to which FP points, read size bytes at a time, repeat n times, and store the read data in the memory starting from buffer. At the same time, move the read/write position Pointer Forward by size * n Bytes. buffer is the memory start address (where to store) for reading data ).

File data block write function

Int fwrite (void * buffer, int size, int N, file * FP );

Starting from the buffer, the output size is several bytes at a time, repeat n times, and store the output data to the file pointed to by the FP. At the same time, move the read/write position Pointer Forward by size * n Bytes, where buffer is the starting address (where to start output) of the data to be output in the memory ).

Position pointer reset function rewind

Int rewind (File pointer );

Call Method

Rewind (FP); returns the position pointer of the file to the file header. The second function does not return the value.

Random read/write Functions

Fseek (FP, offset, origin );

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.