Summary File operation functions (1)-C Language

Source: Internet
Author: User

When the process starts running, three files of the corresponding device are automatically opened. They are standard input, output, and error streams, which are represented by the Global File pointers stdin, stdout, and stderr respectively, the corresponding file descriptor is 0, 1, 2; stdin has a readable attribute. By default, it refers to reading input from the keyboard. stdout and stderr have writable attributes, by default, data is directed to the output data of the screen.


Convert the file path to a file pointer:

# Include // The header file contains

FILE * fopen (const char * pach, const char * mode); // FILE name mode

Int fclose (FILE * stream );

Fopen opens or creates a file in mode. If the file is successfully opened, a file pointer is returned. If the file fails, NULL is returned.

The access permission for the files created by fopen is determined by combining 0666 with the current umask.

In Linux, 'B' (Binary) in the mode can be removed, but we recommend that you do not remove it to maintain compatibility with other systems. AB and a + B are append modes. In both modes, no matter where the file read/write points are located, data is added at the end of the file when writing data, therefore, it is suitable to ensure data integrity when multiple processes write the same file. Generally, the r or w mode is used.

Convert a directory name to a directory pointer:

# Include

# Include

DIR * opendir (const char * name); // open a directory and return a DIR pointer.

Struct dirent * readdir (DIR * dir); // read the information of a directory and return the struct pointer of the information.

Void rewinddir (DIR * dir); // locate the directory file header

Int closedir (DIR * dir); // close the directory file

To read the directory information, follow these steps:

Use the opendir function to open the directory;

Use the readdir function to iteratively read the contents of the directory. If you have read the contents at the end of the Directory and want to start reading again, you can use the rewinddir function to locate the file pointer to the starting position of the directory file;

Use the closedir function to close the Directory

Opendir () is used to open the directory specified by the parameter name and return the directory stream in the DIR * format. It is similar to the file operation function open, the returned value is used for reading and searching directories. If the function fails, NULL is returned;

File pointers are often used for string reading, writing, and formatting of input and output:

Char buf [1024];

Char * fgets (char * s, int size, FILE * stream );

Int fputs (const char * s, FILE * stream );

Fgets and fputs read and write a row of data from the file stream. When the file pointer is stdin/stdout, it indicates input and output from the standard input stream and output stream respectively. This is similar to the gets and puts functions.

Fgets can specify the size of the target buffer. When reading a file, the size is greater than the total number of characters in the row plus 2 (two extra characters, one for saving the '\ n' line feed of the file itself, the end identifier '\ 0' of a stored string itself). The file will not continue to read, but the row is read completely, and the pointer pointing to the file will be automatically offset to the next row.

In this case, the buf [strlen (buf)-1] Stores '\ n'

Use char * to allocate memory with malloc.

Fputs does not automatically add line breaks at the end of a row, but puts automatically adds a line break to the standard output stream.

Read and Write Data in binary format by block:

Function prototype:

Size_t fread (void * buffer, size_t size, size_t count, FILE * fp );

Size_t fwrite (void * buffer, size_t size, size_t count, FILE * fp );

Function: read/write data blocks fread and fwrite are generally used for the input/output of binary files.

Fread: reads count blocks from the files pointed to by fp. The length of each block is size bytes and is stored in the memory with the buffer as the first address. Buffer must already be in the memory.

Fwrite: reads count blocks from the memory with the buffer as the first address. Each block has a size byte and is written to the file pointed to by fp.

Returned value: successful. The number of read/write blocks is returned. If an error occurs or the end of the file, 0 is returned.

Note:

Buffer: pointer to the first address of the data block to be input/output

Size: size of the read/write data block (in bytes)

Count: number of data blocks to read/write

Fp: file pointer to read/write

Sample Code: # include
 
  
# Include
  
   
Typedef struct STUDENT {char sNo [5]; char sName [20]; double score;} STUDENT; void main () {/* operate a piece */STUDENT stu [3] = {"0001", "Zhao Jun", 89 },{ "0002", "Li Qian", 90 }, {"0003", "Zhang Fang", 100 }}; STUDENT stu2; FILE * fp = fopen ("d: \ B .txt", "wb + "); // write open, convert to file pointer if (NULL = fp) {printf ("Can not open this file! \ N "); exit (0);} fwrite (stu, sizeof (STUDENT), sizeof (stu)/sizeof (STUDENT), fp); rewind (fp ); /* file pointer back to header */fseek (fp, sizeof (STUDENT), SEEK_SET);/* locate, skip a record */fread (& stu2, sizeof (STUDENT), 1, fp);/* read the specified size from the file */printf ("% s --> % lf \ n ", stu2.sNo, stu2.sName, stu2.score); fclose (fp );}
  
 



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.