Linux input and output files, Linux input and output files

Source: Internet
Author: User
Tags rewind

Linux input and output files, Linux input and output files
1. File Operations (buffering) based on file pointers)

In linux, operations on directories and devices are file operations. files are classified into common files, directory files, link files, and device files.

1.1. Create, open, and close a file

Prototype:

# Include <stdio. h> // header file inclusion

FILE * fopen (const char * pach, const char * mode); // FILE name mode

Int fclose (FILE * stream );

Fopen opens or creates a file in mode. If the file is successfully opened, a file pointer is returned. If the file fails, NULL is returned.

The access permission for the files created by fopen is determined by combining 0666 with the current umask.

1.2. Read and Write files

There are many data read/write functions based on file pointers, which can be divided into the following groups:

Data Block read/write:

# Include <stdio. h>

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

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

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.

1. 3. File positioning:

File Location refers to reading or setting the current read/write point of the file. All functions that read and write data through the file pointer read and write data from the current read/write point of the file.

Common functions include:

# Include <stdio. h>

Int feof (FILE * stream); // the common usage is while (! Feof (fp ))

Int fseek (FILE * stream, long offset, int whence); // you can specify the length of the current read/write point and the offset whence to be offset.

Long ftell (FILE * stream); // used to obtain the current read/write location of the FILE stream

Void rewind (FILE * stream); // move the read/write location of the FILE stream to the beginning of the FILE (fp, 0, SEEK_SET );

Feof determines whether the next row at the end of the file is reached (note that it will be done again after the end of the file)

Fseek sets the length of the current read/write point and offset whence to offset. The whence can be:

SEEK_SET (file start-> 0)

SEEK_CUR (current file location-> 1)

SEEK_END (end of the file-> 2)

Ftell obtains the current read/write point

Rewind moves the current file read/write point to the file header

. Formatted read/write:

# 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,...); // eg: sprintf (buf, "the string is; % s", str );

Int sscanf (char * str, const char * format ,...);

Writes formatted strings starting with f to the file stream.

Writes formatted strings starting with s to str.

. Read/write of a single character:

You can use the following functions to read and write one character at a time.

# Include <stdio. h>

Int fgetc (FILE * stream );

Int fputc (int c, FILE * stream );

Int getc (FILE * stream); e is equivalent to fgetc (FILE * stream)

Int putc (int c, FILE * stream); e is equivalent to fputc (int c, FILE * stream)

Int getchar (void); e is equivalent to fgetc (stdin );

Int putchar (int c); e is equivalent to fputc (int c, stdout );

Getchar and putchar read and write data from the standard input and output streams, and other functions read and write data from the file stream streams.

1. 6. String read/write:

Char * fgets (char * s, int size, FILE * stream );

Int fputs (const char * s, FILE * stream );

Int puts (const char * s); e is equivalent to fputs (const char * s, int size, stdout );

Char * gets (char * s); e is equivalent to fgets (const char * s, int size, stdin );

Fgets and fputs read and write a row of data from the file stream;

Puts and gets read and write a row of data from the standard input/output stream.

Fgets can specify the size of the target buffer. Therefore, it is secure relative to gets. However, when fgets is called, if the number of characters in the current row in the file is greater than the size, the next fgets call will take effect, the remaining characters of the row will be read. When fgets reads a line of characters, the line break at the end of the line will be retained.

Fputs does not automatically add line breaks at the end of a row, but puts automatically adds a line break to the standard output stream.

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.