Reproduced C + + file read and write functions--fopen, fread and Fwrite, fgetc and FPUTC, Fgets and Fputs, FTELLF and fseek, rewind

Source: Internet
Author: User
Tags fread rewind

Http://blog.sina.com.cn/s/blog_61437b3b0102v0bt.html

http://blog.csdn.net/chenwk891/article/details/8776479 in c\c++, the file operations are implemented by the library function, mainly divided into two types of read and write operations, the following detailed explanation of all the following about the use of file operations: (1) fopen () function: Open Fileinclude header file: #includeformat: FILE * fopen (const char * path,const char * mode);Parameters:Path : File path that needs to be openedmode: How to open the file, specifically as followsR opens the file as read-only, and the file must exist. r+ Open the file as read-write, the file must be present. rb+ Read and write open a binary file that allows read data. rt+ Read and write open a text file that allows reading and writing. W Open Write-only file, if the file exists then the file length is clear to 0, that is, the contents of the file will disappear. If the file does not exist, the file is created. w+ Open a read-write file, if the file exists then the file length is clear to zero, that is, the contents of the file will disappear. If the file does not exist, the file is created. A write-only file opens in an additional way. If the file does not exist, the file will be created, and if the file exists, the data written will be added to the end of the file, that is, the original content of the file will be retained. (EOF character reserved) A + opens a read-write file in an additional way. If the file does not exist, the file will be created, and if the file exists, the data written will be added to the end of the file, that is, the original content of the file will be retained. (the original EOF character is not retained) WB only writes Open or creates a new binary file; Only write data is allowed. wb+ read-write open or create a binary file that allows reading and writing. wt+ read-write open or create a text file; at+ Read and write open a text file that allows you to read or append data at the end of the text. ab+ Read and write open a binary file that allows you to read or append data at the end of the file. Now let's summarize the file opening method above:File usage by r,w,a,t,b,+ Six characters, the meaning of each character is: R (Read): Read W (write): Writes a (append): Append t (text): A literal file, can be omitted do not write B (banary): binary file +: Read and Write return value: Returns a pointer to the file that is pointing to the stream if the file is open successfully. Returns null if the file fails to open, and the error code exists in errno. Description in general, after opening the file will make some file read or write action, if the file fails to open, the next read and write actions will not be smooth, so generally after fopen () for error judgment and processing. (2) fread () function and fwrite () function: (can be used to implement the operation of the data block)1. General Call Form Fread (BUFFER,SIZE,COUNT,FP); Fwrite (BUFFER,SIZE,COUNT,FP); 2. Description (1) Buffer: is a pointer to Fread, which is the storage address of the read-in data.   For Fwrite, the address of the data to be output. (2) Size: The number of bytes to read and write, (3) Count: The number of bytes of data to read and write, and (4) The FP: file-type pointer. The C language also provides read and write functions for the entire block of data. Can be used to read and write a set of data, such as an array element, the value of a struct variable, and so on. The general form of a read block function call is: Fread (BUFFER,SIZE,COUNT,FP); The general form of a write block function call is: fwrite (BUFFER,SIZE,COUNT,FP); Where buffer is a pointer to the Fread function, which represents the first address that holds the input data. In the Fwrite function, it represents the first address that holds the output data. A size represents the number of bytes of data block. Count represents the number of block blocks of data to read and write. The FP represents a file pointer. For example: Fread (FA,4,5,FP); The meaning is that from the file referred to in the FP, each read 4 bytes (a real number) into the real group FA, read 5 consecutive times, that is, read 5 real numbers into FA. (3) fgetc () and FPUTC (): Character reading and writing functionsfgetc function (getc function) (1) General call Form CH=FGETC (FP), (2) function reads a character from the specified file, that is, from the file pointed to by the FP read into a word assigned to Ch. (3) The return value succeeds: The character of the returned value; failure: Returns EOF (-1). FPUTC function (): (1) General call Form FPUTC (CH,FP); (2) The function writes a character to the disk file, and the character ch is output to the file that the FP points to. (3) The return value succeeds: The return value is the character of the output; failure: Returns EOF (-1). Description: Function Putchar () is a macro defined in stdio.h with a preprocessing command, namely: #define PUTCHAR (c) FPUTC (c,stdout) 4, Fgets () and fputs () functions: Read and write String functions(1) fgets (): Char *fgets (char *str, int num, FILE *fp) parameter description: str: Save string read from file fp: file pointer to read file num: Indicates that a string that is read from a file does not exceed n-1 characters. After the last character read, add the string end Flag ' fputs ' (2) (char * s,file * stream); 5, fscanf () and fprintf () functions: formatted read-write functions(1) fprintf () function: output format string to stream or write formatted string to output stream (file) prototype: int fprintf (filename *stream, const char *format [, argument] ... ); Example: fprintf (FP, "%s%s%d%f", STR1,STR2, A, b); (2) fscanf () function: The contents of the output file into a variable.   int fscanf (FILE *stream, char *format,[argument ...]);  int fscanf (file pointer, format string, input list);  For Example:file *FP;  Char a[];  int b;  Double C; FSCANF (FP, "%s%d%lf", A,&b,&c) return value: integral type, value equal to [argument ...] The number 6. Ftell () function:Gets the current read and write location of the streaming file whose return value is the number of bytes from the file header that the current read-write location deviates from. Prototype: Long Ftell (FILE *FP) 7. Fseek () function:Moves the FP file read-write location pointer to the specified location. int fseek (file *fp,long offset,int origin); Origin refers to the "starting point", which has a value of three constants, as follows: The starting point corresponds to a number representing the file location Seek_set 0 file at the beginning of the Seek_cur 1 file current Location Seek_end 2 End of file 8. Rewind () function:Re-pointing the file pointer to the beginning of a stream prototype: Int rewind (file *stream); Example: generally we will get the number of characters in the file by using the following method: File *fs=fopen ("C:\1.txt", "R");//Creating a stream long length=0;//declaration file Length fseek (fs,0,seek_end);//Place the file internal pointer to the end of the file Length=ftell (FS),//Read the position of the file pointer, get the number of file characters rewind (fs);//The file pointer Reset to the front of the file

Reproduced C + + file read and write functions--fopen, fread and Fwrite, fgetc and FPUTC, Fgets and Fputs, FTELLF and fseek, rewind

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.