C Language Library functions--manipulating files

Source: Internet
Author: User

C library function read the code of the file

I/O buffering mechanism C language library function write files are written in memory and then written to disk at once. Increased efficiency.

Read and write files, do not operate the system, generally use the C library function. Porting can be in any operating system that can support C without modification.

FILE *fopen (const char *path, const char *mode);

Mode parameter:

R Open text file for reading. The stream is positioned at the beginning of the file.

r+ Open for reading and writing. The stream is positioned at the beginning of the file.

W Truncate file to zero length or create text file for writing. The stream is positioned at the beginning of the file.

w+ Open for reading and writing. The file is created if it does isn't exist, Otherwiseit is truncated. The stream is positioned at the beginning of the file.

A Open for appending (writing at end of file). The file is created if it does notexist. The stream is positioned at the end of the file.

A + Open for reading and appending (writing at end of file). The file is created if itdoes not exist. The initial file position for reading are at the beginning of the
file, but output was always appended to the end of the file.

The R Read method opens the file, the beginning of the file begins to read

r+ Read and write, position is also at the beginning of the file

W truncate file (empty) and create file write, location is also file start

w+ read and write files, if the file does not exist on the creation of files, there is truncated files, location is also the beginning of the file.

A append write to open the file, if the file does not exist, create the file, the location at the end of the file

A + opens append file read and write. If the file does not exist, the file is created.

int fclose (FILE *fp);

size_t fread (void *ptr, size_t size, size_t nmemb, FILE *stream);

The size represents the read, NMEMB indicates the number of read records.

such as a 100-character buff can read size =100, Nmemb =1

You can also size =1, Nmemb =100

Return when full size is read, otherwise 0

size_t fwrite (const void *ptr, size_t size, size_t nmemb,file *stream);


int main (int arg, char *args[])
{
FILE *p = fopen (Args[1], "r+");
if (p = = NULL)
{
printf ("Error is%s\n", Strerror (errno));
}

Else
{
printf ("success\n");
Char buf[100];
size_t rc = 0;

while (1)
{
size_t tmp = Fread (buf, 1, sizeof (BUF), p);//The principle is that the second parameter multiplied by the third parameter cannot exceed the size of a buffer
RC + = tmp;
if (TMP = = 0)
Break

}
printf ("rc =%d\n", RC);
Fclose (P);
}
return 0;
}

C Language Library functions--manipulating files

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.