File Operations in C Language

Source: Internet
Author: User
Tags rewind

A file is a collection of data stored in external memory of a computer. Calculation
Computer basis? File name, correct? File Operations
C language? What do you mean? A file is considered as a sequence of characters, that is, a forward stream. The basic unit of storage is byte.
? Are all file read/write functions in the header? The stdio. h file defines:
? Read/write by character functions fgetc (), fputc ()
? Functions fgets () and fputs () read and write by string ()
? Functions fprintf () and fscanf () read and write according to the format requirements ()
? Functions fread () and fwrite () read/write by data block ()


File pointer variable = fopen (? (File Processing Method );
Why can't the file be opened because it does not exist? Is it called? Fopen () returns a NULL pointer.


Fputc (character ,? File pointer variable)
? Fp is A pointer variable of the file type. Can it be A variable of the common character 'a' (or variable of the character type) written above? The current location of the file, and make? The position pointer moves one byte down. If the write operation is successful, the returned value is this character; otherwise, the EOF is returned.




Fgetc () function
Fgetc (? Component pointer variable)
Returns the characters at the current position of the file and? The position pointer moves one character down. If a file ends, the returned value is the end mark EOF.


Fputs (string ,? Component pointer variable)
Write? The file is successful. The return value of the function is 0. Otherwise, the value is EOF.


Fgets (character array, number of characters ,? Component pointer variable)
What does it do? Is it directed from fp? The current position of the file starts to read n-1 characters, and add the string ending sign '\ 0' to the string array 'str. If a linefeed or end sign EOF is returned when a character is read from a file, the read ends. The function returns the str string? Address.


Fprintf (File pointer variable, format control, output table column)
What does it do? Is the variables num, name, and score written in the format of % ld, % s, and % 5.1f? The current location of the file to which the fp points


Fscanf (? Component pointer variable, format control, input table column );
What does it do? Is from the current position of the file pointed to by fp, press
Take the data in the format of % ld, % s, and % 5.1f and assign it to the variable.
Num, name, and score.


Functions fread () and fwrite () read/write by data block ()


Fwrite (write? The storage address of the data block of the file, the number of bytes of a data block, and the number of data blocks ,? Component pointer variable );
For example, if the stu [20] array of the struct student type is known, fwrite (& stu [1], sizeof (struct student), 2, fp );
If the number of data blocks written is returned successfully, 2


Fread () function
What is the function fread? From? Read data blocks in batches
Fread (storage address of the data block read from the file ,? The number of data blocks, the number of data blocks, and the file type pointer variable );


It is known that stu1 is? A struct student variable
Fread (& stu1, sizeof (struct student), 1, fp );
? Slave? The current position of the file to which the part type pointer fp points starts. Read one data block, which is the number of bytes occupied by the struct student type variable, then, the read content is put into the stu1 variable.
? Note: When the fwrite () and fread () functions read and write files? In binary mode, you can read and write any type of data. Most common? Read and Write array and struct data


When a file is in use, what is the pointer to the opened file? The current position of the file. when data is read or written each time, the data is read or written from the current position of the file pointer, and then the pointer is automatically moved to the read/write bottom? Data bit. Therefore, file pointer locating is very important.
Common Library functions defined in the stdio. h header file of C language.
Function feof (), rewind (), fseek (), ftell ()


Feof (File pointer variable );
If the current position of the file pointed to by the file pointer is the end mark EOF, then the function returns? Non-zero value; otherwise, 0 is returned.


The function rewind () will re-point the pointer to the file to the starting position of the file. The function has no return value. How does it work? Format:
Rewind (? Component pointer variable );
Fseek (? Component pointer variable, offset, starting position );


?
? Function fseek ()
The function fseek () can direct? Pointer variable pointing? To read and write files randomly. It is called in the form:
Fseek (? File pointer variable, offset, start position );
The fseek () function moves the pointer forward or backward Based on the starting position of the file. Where is the offset? The number of long integers, indicating the number of bytes that are moved from the starting position. Positive numbers indicate that the pointer is moved back, and negative numbers indicate that the pointer is moved forward. The start position is represented by numbers 0, 1, 2, or SEEK_SET, SEEK_CUR, and SEEK_END? File start, current location, and end location. If the pointer is set successfully, the returned value is 0. Otherwise, the value is not 0.


Function ftell ()
Function ftell () is used to test the pointing? The current position of the pointer. It's called? Method:
Ftell (file type pointer variable );
What is the return value of the function? A constant integer. If the test is successful, the distance between the pointer pointing to the file and the position pointing to the file is returned? The number of bytes at the beginning of the object. Otherwise,-1L is returned.

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.