C language Read and write files

Source: Internet
Author: User
Tags fread

FOPEN (3) Linux Programmer ' s Manual FOPEN (3)

NAME
fopen, Fdopen, Freopen-stream open functions

Synopsis
#include <stdio.h>

FILE *fopen (const char *path, const char *mode);
FILE *fdopen (int fildes, const char *mode);
File *freopen (const char *path, const char *mode, file *stream);

DESCRIPTION
The fopen () function opens the file whose name is the string pointed to by Path and associates a
Stream with it.

The argument mode points to a string beginning with one of the following sequences (Additional
Characters may follow these sequences.):

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, otherwise it 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 not exist.
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 it does
Not exist. The initial file position for reading are at the beginning of the file, but out-
Put is all appended to the end of the file.

The mode string can also include the letter "B" either as a last character or as a character
Between the characters in any of the two-character strings described above. Strictly for
Compatibility with C89 and have no effect; The ' B ' is ignored to all POSIX conforming systems,
including Linux. (Other systems may treat text files and binary files differently, and adding the
' B ' is a good idea if you do I/O to a binary file and expect this your program is ported
to Non-unix environments.)

Any created files would have mode s_irusr| s_iwusr| s_irgrp| s_iwgrp| s_iroth| S_iwoth (0666), as modi-
Fied by the process ' umask value (see Umask (2)).

FREAD (3) Linux Programmer ' s Manual FREAD (3)

NAME
Fread, Fwrite-binary Stream input/output

Synopsis
#include <stdio.h>

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

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

DESCRIPTION
The function fread () reads NMEMB elements of data, each size bytes long, from the stream pointed
To by stream, storing them at the location given by PTR.

The function fwrite () writes NMEMB elements of data, each size bytes long, to the stream pointed
To by stream, obtaining them from the location given by PTR.

For non-locking counterparts, see Unlocked_stdio (3).

RETURN VALUE
Fread () and fwrite () return the number of items successfully read or written (i.e., not the number
of characters). If An error occurs, or the End-of-file are reached, the return value is a short
Item count (or zero).

Fread () does not distinguish between End-of-file and error, and callers must use FEOF (3) and fer-
ROR (3) to determine which occurred.

C language Read and write 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.