Summary of file input and output

Source: Internet
Author: User
Tags fread
1. File pointer-based data read/write

Data Reading and Writing based on file pointers, usually standard functions, can be used in Windows and Linux.

Data Block read/write
NAME       fread, fwrite - binary stream input/outputSYNOPSIS       #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  loca-       tion 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 loca-       tion given by ptr.
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 is reached, the return value is a short item count (or       zero).

Fread reads nmemb elements from the file stream and writes them to the memory directed by PTR. The size of each element is size byte.

Fwrite reads nmemb elements from the memory directed by PTR and writes them to the file stream. Each element has size bytes.

All file read/write functions start to read and write from the current read/write point of the file. After reading, the current read/write point automatically moves the size * nmemb byte. String read/write

 
SYNOPSIS 
       #include <stdio.h>
Char * fgets (char * s, int size, file * stream); int fputs (const char * s, file * stream); char * gets (char * s );? Equivalent to fgets (const char * s, int size, stdin); int puts (const char * s); equivalent to fputs (const char * s, int size, stdout );
 
DESCRIPTION        fgets()  reads  in  at most one less than size characters from stream and stores them       into the buffer pointed to by s.  Reading stops after an EOF or a newline.  If a new-       line  is read, it is stored into the buffer.  A ‘\0‘ is stored after the last charac-       ter in the buffer.       gets()  reads a line from stdin into the buffer pointed to by s until either a termi-       nating newline or EOF, which it replaces with ‘\0‘.  No check for buffer  overrun  is       performed (see BUGS below).       fputs() writes the string s to stream, without its trailing ‘\0‘.       puts() writes the string s and a trailing newline to stdout.

Gets () ignores '\ n'. If the program is executed, press enter, and the string stores' \ 0 '. If an error occurs or ends at the end of the file, null is returned.

Puts () will replace '\ 0' with \ N output. In case of an error, EOF is returned.

Fgets () returns the first address of the array, '\ n' is also saved, and an' \ 0' is added. If the object ends, null is returned. If an error occurs or ends at the end of the file, null is returned.

Fputs does not automatically add line breaks at the end of a row. In case of an error, EOF is returned.

Note: When reading strings from a file, '\ 0' D is automatically added to the end.

RETURN VALUE       puts() and fputs() return a non-negative number on success, or EOF on error.       gets() and fgets() return s on success, and NULL on error or when end of file  occurs       while no characters have been read.
Formatted read/write
SYNOPSIS 
       #include <stdio.h>
Int printf (const char * format,...); // equivalent to fprintf (stdout, format ,...); Int scanf (const char * format ,...);
       int fprintf(FILE *stream, const char *format, ...);       int fscanf(FILE *stream, const char *format, …);       
       int sprintf(char *str, const char *format, ...);                    int sscanf(char *str, const char *format, …);

Writes formatted strings starting with F to the file stream, or reads formatted strings from the file stream.

Writes formatted strings starting with S to STR, or reads formatted strings from Str.

If you write a function, the number of written characters (not including '\ 0') is returned. If an error occurs, a negative number is returned.

For a READ function, the number of matched values is returned. If an error occurs or the end of the file is reached, EOF is returned.

Note: For read functions, spaces are ignored when matching strings, and '\ 0' is added at the end '.

Single Character read/write

RETURN VALUE       fgetc(),  getc()  and getchar() return the character read as an unsigned char cast to       an int or EOF on end of file or error.       fputc(),  putc()  and putchar() return the character written as an unsigned char cast       to an int or EOF on error.       puts() and fputs() return a non-negative number on success, or EOF on error.
2. file descriptor-based data read/write

The read and write functions are called in Linux and are only used in Linux. Not buffered.

Note:

For pipelines, the read return value can be in the following three situations:

1. Reading is normal. The number of characters read is returned.

2. The peer write end is disabled, and read returns 0.

3. Close your own read end. If a read error occurs,-1 is returned.

NAME       read - read from a file descriptorSYNOPSIS       #include <unistd.h>       ssize_t read(int fd, void *buf, size_t count);DESCRIPTION       read()  attempts  to  read  up to count bytes from file descriptor fd into the buffer       starting at buf.       If count is zero, read() returns zero and has no other results.  If count is  greater       than SSIZE_MAX, the result is unspecified.RETURN VALUE       On  success,  the  number of bytes read is returned (zero indicates end of file), and       the file position is advanced by this number.  It is not an error if this  number  is       smaller than the number of bytes requested; this may happen for example because fewer       bytes are actually available right now (maybe because we were close  to  end-of-file,       or  because  we  are  reading from a pipe, or from a terminal), or because read() was       interrupted by a signal.  On error, -1 is returned, and errno is  set  appropriately.       In this case it is left unspecified whether the file position (if any) changes.
NAME       write - write to a file descriptorSYNOPSIS       #include <unistd.h>       ssize_t write(int fd, const void *buf, size_t count);DESCRIPTION       write()  writes up to count bytes from the buffer pointed buf to the file referred to       by the file descriptor fd.RETURN VALUE       On success, the number of bytes written is returned (zero indicates nothing was writ-       ten).  On error, -1 is returned, and errno is set appropriately.

Summary of file input and output

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.