C language read/write file fopen

Source: Internet
Author: User

First, we will introduce two functions:

/*
Function: open a file
Function prototype: file * fopen (const char * path, const char * mode );
Related functions: open, fclose, fopen_s [1], _ wfopen
Required database: <stdio. h>
Returned value: After the file is successfully opened, the file pointer pointing to the stream will be returned. If the file fails to be opened, null is returned and the error code is stored in errno.
In general, after opening a file, some operations will be performed to read or write the file. If the file fails to be opened, the subsequent read/write operations will not proceed smoothly () then, make error judgment and handling.
Parameter description:
The parameter path string contains the path and file name of the file to be opened. The parameter mode string represents the stream format.
Mode has the following forms of strings:
R: open the file in read-only mode. The file must exist.
R + can open a file in read/write mode. The file must exist.
RB + read/write open a binary file and allow read/write data.
RW + read/write opens a text file, allowing reading and writing.
W. Open and write only the file. If the file exists, the file length is 0, indicating that the file content will disappear. If the file does not exist, the file is created.
W + open the readable and writable file. If the file exists, the file length is cleared to zero, that is, the file content disappears. If the file does not exist, the file is created.
A. Open and write-only files as an attachment. If the file does not exist, the file will be created. If the file exists, the written data will be added to the end of the file, that is, the original content of the file will be retained. (EOF reserved)
A + opens readable and writable files by appending them. If the file does not exist, the file will be created. If the file exists, the written data will be added to the end of the file, that is, the original content of the file will be retained. (The original EOF is not retained)
WB only opens or creates a new binary file; only data can be written.
WB + enables read/write operations or creates a binary file, allowing read and write operations.
Open a binary file through AB + read/write, and allow you to read or append data to the end of the file.
At + open a file named string, and a indicates append, which means that the existing content of the original file is written at the time of writing, instead of overwriting from the beginning, t indicates that the type of the opened file is a text file, and the + number indicates that the file can be read or written.
The preceding morphological string can be added with a B character, such as a combination of Rb, W + B, and AB +. The B character is added to instruct the function library to open the file in binary mode. If B is not added, t is added by default, namely RT and wt. t indicates opening the file in text mode. The new file created by fopen () has the s_irusr | s_iwusr | s_irgrp | s_iwgrp | s_iroth | s_iwoth (0666) Permission. For the File Permission, see umask value.
Some C compilation systems may not provide all of these functions. Some C versions do not use "R +", "W +", "A +", but use "RW ", "WR", "Ar", etc. Readers should pay attention to the rules of the system used.
Differences between binary and text Modes
1. in windows, in text mode, "" indicates a line break. If you open a file in text mode and use functions such as fputs to write the line break "\ n", the function automatically adds "\ r" to "\ n ". That is, the actual file written is "".
2. in Unix/Linux-like text mode, "\ n" indicates a line break. Therefore, in Linux, there is no difference between the text mode and the binary mode.
*/

/*
 Size_t fwrite (const void * buffer, size_t size, size_t count, file * stream );
Note: This function operates files in binary format, not limited to text files.
Return Value: returns the number of actually written data blocks.
(1) buffer: A pointer. For fwrite, It is the address for getting data;
(2) Size: Number of Single-word segments to be written;
(3) count: number of data items to be written in size bytes;
(4) stream: pointer to the target file;
(5) return the number of actually written data items count.
*/

Test code:

// Fopentest. cpp: defines the entry point of the console application. // # Include "stdafx. H "# include" string. H "// strlen () header file # include" stdlib. H "// header file int _ tmain (INT argc, _ tchar * argv []) required by ITOA () {char buffer [7] =" AAA \ r \ n "; file * file = fopen ("E: \ filetest. log "," AB "); fwrite (buffer, sizeof (char), sizeof (char) * 5/* two of them \ Number of escape not involved */, file ); const char * pfilename = const_cast <char *> (_ file _); // const char * pfileline = const_cast <char *> (_ line __); char * pfileline = new char [256]; ITOA (_ line __, pfileline, 10); // long-> char fwrite (pfilename, sizeof (char ), strlen (pfilename), file); fwrite (pfileline, sizeof (char), strlen (pfileline), file); fclose (File); Return 0 ;}

 

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.