1. What is a file in C?

  • We are already familiar with the concept of files, such as Word documents, TXT files, source files, etc.File is a kind of data source, the main function is to save data.
  • In the operating system, different hardware devices are treated as one file in order to unify the operation of various hardware devices and simplify the interface.The operation on these hardware is the same as the operation on ordinary files on disk.

2. What is the correct process (three steps) for file operation? What are the two main ways to read a file?

  • Open file –> Read/write file –> close file
  • There are two main ways to read and write a file: ① it can be read character by character ② it can read an entire line

3. What is file flow? What is an input stream? What is an output stream?

As mentioned in the article load Memory to Run, all files (stored on disk) must be loaded into memory to process, and all data must be written to files (disk) so that it will not be lost.

  • The process of passing data between files and memory is calledFile streamLike water moving from one place to another.
  • The process of copying data from a file to memory is calledThe input stream
  • The process of saving from memory to file is calledThe output stream

4. What are the two functions in C to change where a file is read or written?

  • void rewind ( FILE *fp );Move the location pointer to the beginning of the file
  • int fseek ( FILE *fp, long offset, int origin );Moves the position pointer to any position

5. How does C get file size?

  • C language does not provide a function to obtain file size, to achieve this function, you must write your own function.
long fsize(FILE *fp){
 long n;
 fpos_t fpos;  // The current position
 fgetpos(fp, &fpos);  // Get the current location
 fseek(fp, 0, SEEK_END); // Move the internal pointer to the end of the file
 n = ftell(fp); // The ftell() function is used to get the number of bytes between the file's internal pointer (position pointer) and the beginning of the file
 fsetpos(fp,&fpos);  // Restore the previous position
 return n;
}
Copy the code