C file operations

Source: Internet
Author: User

1. fopen fclose
1.1 FILE * fopen (const char * path, const char * mode)
(1) return

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.
Therefore, the test file can be opened as follows:
If (fp = NULL)
Do_error ();

(2) Mode:
R (read) w (write) a (append)
B (binary operation)
+ (Read/write allowed)

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.

 

 

1.2 fclose (FILE * fp)
Paired with fopen, after yongfopen opens a file, you must use fclose to close the file.

 


2. fgetc fputc characters
2.1 int fgetc (FILE * stream)
The function returns a character from the current position of the input stream, and moves the file pointer indicator to the next character. If it has reached the end of the file, the function returns EOF, which indicates that this operation has ended.

 

[Cpp]
<SPAN style = "FONT-SIZE: 14px"> while (ch = fgetc (fp ))! = EOF)
{
Do_by_char ();
} </SPAN>

While (ch = fgetc (fp ))! = EOF)
{
Do_by_char ();
}


2.2 int fputc (int ch, FILE * stream)
The function completes writing the value of the ch character to the current position of the specified stream file, and moves the file pointer one by one.
The returned value of the fputc () function is the value of the written characters. If an error occurs, an EOF is returned.


2.3 getchar putchar

[Cpp]
<SPAN style = "FONT-SIZE: 14px"> # define getchar () getc (stdin)
# Define putchar (c) putc (c), stdout) </SPAN>

# Define getchar () getc (stdin)
# Define putchar (c) putc (c), stdout)


3. fgets fputs row

3.1 char * fgets (char * line, int line_size, FILE * fp)
Read up to line_size-1 characters from the stream file fp and place them into the character array pointed to by line. Read characters until a carriage return or EOF (File Terminator) is met, or a limited number of characters are read.


[Cpp]
<SPAN style = "FONT-SIZE: 14px"> while (fgets (str, 128, fp )! = NULL)
{
Do_by_line ();
} </SPAN>

While (fgets (str, 128, fp )! = NULL)
{
Do_by_line ();
}

 

[Cpp]
<SPAN style = "FONT-SIZE: 14px"> char * fgets (char * s, int n, FILE * fp)
{
Register int c;
Register char * cs;

Cs = s;
While (-- n> 0 & (c = getc (fp ))! = EOF)
If (* cs ++ = c) = '\ n ')
Break;

* Cs = '\ 0 ';

Return (c = EOF & cs = s )? NULL: s;
} </SPAN>

Char * fgets (char * s, int n, FILE * fp)
{
Register int c;
Register char * cs;
 
Cs = s;
While (-- n> 0 & (c = getc (fp ))! = EOF)
If (* cs ++ = c) = '\ n ')
Break;

* Cs = '\ 0 ';
 
Return (c = EOF & cs = s )? NULL: s;
}

 

3.2 int fputs (char * line, FILE * fp)
When the operation is successful, the function returns 0, and the Failure Returns a non-zero value.


[Cpp]
<SPAN style = "FONT-SIZE: 14px"> int fputs (char * line, FILE * fp)
{
Int c;
While (c = * s ++)
Putc (c, fp );
Return ferror (fp )? EOF: 0;
} </SPAN>

Int fputs (char * line, FILE * fp)
{
Int c;
While (c = * s ++)
Putc (c, fp );
Return ferror (fp )? EOF: 0;
}


4. fscanf fprintf format

4.1 · int fscanf (FILE * stream, char * format, arg_list)
Same as scanf

4.2 int fprintf (FILE * stream, char * format, arg_list)
Same as printf


[Cpp]
<SPAN style = "FONT-SIZE: 14px"> while (fscanf (fp, "% s % d", str, & I )! = EOF)
{
Fprintf (stdout, "% s \ t % d", str, I );
} </SPAN>

While (fscanf (fp, "% s % d", str, & I )! = EOF)
{
Fprintf (stdout, "% s \ t % d", str, I );
}

 

5. fread fwrite Block
5.1 int fread (void * buf, int size, int count, FILE * stream)
The fread () function reads the count (number of fields) field from the stream file to which the stream points. Each field is of size (Field Length) characters long, and place them in the string array pointed to by B u f (buffer.

The fread () function returns the number of actually read fields. If the number of fields required to be read by the function call exceeds the number of fields stored in the file, an error occurs or has reached the end of the file. In actual operation, check carefully.


5.2 int fwrite (void * buf, int size, int count, FILE * stream)
From the character array pointed to by the buf (buffer zone), the function writes the count (number of fields) field to the stream to which the stream points. Each field is of a size character long, the number of fields written when the function operation is successful. For block-based file read/write, files can only be created in binary file format when they are created.

 

[Cpp]
<SPAN style = "FONT-SIZE: 14px"> while (! Feof (fp ))
{
Fread (str, sizeof (int), 127, fp );
} </SPAN>

While (! Feof (fp ))
{
Fread (str, sizeof (int), 127, fp );
}


6. fseek
Int fseek (FILE * fp, long offset, mode)

Mode:

SEEK_SET: 0 -- start

SEEK_CUR: 1 -- Current

SEEK_END: 2 -- end

 

 

 

 

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.