C language file function, C Language Function

Source: Internet
Author: User

C language file function, C Language Function

FILE * fp:
The FILE should be capitalized, which is actually a structure defined by the system, in the stdio. h FILE. The structure contains the file name, File status, and the current read/write information of the file.
Fp is a pointer variable pointing to the FILE structure. Through fp, you can find the information structure of a FILE, find the FILE, and perform operations on the FILE.

FILE * fp = fopen ("FILE path and name", "open mode"). If this function fails to be executed, NULL is returned.
Open Mode: r: open a text file read-only, provided that the file must exist
W: open a text file and write it only. If the file does not exist, create the file.
A: Add a text file
Rb: open a binary file read-only, provided that the file must exist
Wb: open a binary file and write it only. If the file does not exist, create the file.
AB: Add a binary file
R +: open a text file to read/write
W +: generate a text file read/write
A +: open a text file to read/write
Rb +: Open binary file read/write
Wb +: Generate binary file read/write
AB +: Open binary file read/write

Char ch = fgetc (fp ):
Read a character from the file. If the character reaches the end of the file or an error occurs, the EOF
Fputc (ch, fp );
Write the string ch to the file. If the function fails to be executed, EOF is returned.
Fgets (char buffer, int size, FILE * fp)
Read a string of characters from the file to the array buffer. When a line break is encountered, the line break is retained in the array and the reading is not performed. The array ends with '\ 0'.
Fputs (char buffer, FILE * fp );
Writes a string to a file. The return value of the function is the number of characters actually written to the file.

 

 

 

# Include <stdio. h>
Void rewind (FILE * fp); move the FILE stream write location to the beginning of the FILE

# Include <stdio. h>
Int fseek (FILE * fp, long offset, int whence );
Function: moves the fp file read/write position pointer to the specified position.
SEEK_SET: The New read/write location starting with offset
SEEK_CUR: Increase the offset displacement at the current read/write position.
SEEK_END: point the read/write position to the end of the file and then increase the offset displacement.
If the function is successfully executed, 0 is returned. If the function fails,-1 is returned;

# Include <stdio. h>
Long ftell (FILE * fp) is used to obtain the current read/write position of a FILE stream. The returned value is the number of bytes that the current read/write position deviates from the FILE header. (the default value is '\ 0 ')

#include <stdio.h>  int main()  {     FILE *fp;     char filename[80];     long length;     printf("Input the file name:");     gets(filename);     fp=fopen(filename,"rb");     if(fp==NULL)         printf("file not found!/n");     else     {         fseek(fp,0,SEEK_END);        length=ftell(fp);         printf("the file length %1d bytes\n",length);         fclose(fp);     } return 0; } 

  

 

 

Fread (buffer, size, count, fp );
Fwrite (buffer, size, count, fp );
(1) buffer: A pointer. For fread, It is the storage address for reading data. For fwrite, It is the address of the data to be output.
(2) size: the number of bytes to read and write;
(3) count: the number of data items of size bytes to be read and written;
(4) fp: file pointer.

# Include <stdio. h> int main () {FILE * pFile; float buffer [] = {2.0, 3.0, 8.0}; pFile = fopen ("myfile. bin "," wb "); // open the file write operation fwrite (buffer, 1, sizeof (buffer), pFile); // write the floating point group to the file myfile. bin fclose (pFile); // close the file float read [3]; pFile = fopen ("myfile. bin "," rb "); // re-open the file read operation fread (read, 1, sizeof (read), pFile ); // read data from the file printf ("% f \ t % f \ n", read [0], read [1], read [2]); fclose (pFile); // close the file return 0 ;}

  

 

Include <stdio. h>
Int fflush (FILE * fp );
Function Description: fflush () will force the data in the buffer to be written back to the file specified by the parameter stream. If the parameter stream is NULL, fflush () will update all open file data.

Returned value: 0 is returned for success, EOF is returned for failure, and the error code is stored in errno.

 

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.