Linux input and output with files

Source: Internet
Author: User
Tags fread rewind sprintf stdin

1. File manipulation based on file pointers (buffering)

Linux directory and device operations are file operations, files are divided into ordinary files, directory files, linked files and device files.

1.1. File creation, opening and closing

The prototypes are:

#include <stdio.h>//header file contains

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

int fclose (FILE *stream);

Fopen opens or creates a file in mode, and if successful, returns a file pointer, or null if it fails.

Access to files created by fopen will be determined by combining 0666 with the current umask.

1.2. Read and Write files

There are many functions of data reading and writing based on file pointers, which can be divided into following groups:

Data block read and 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 stream stream, writes to the memory that PTR points to, and each element is sized in bytes.

Fwrite reads NMEMB elements from the memory pointed to by PTR, writes to the file stream stream, each element is of size bytes.

All the file read and write functions are read and written from the current reading and writing point of the file, and after reading and writing, the current read and write points automatically move size*nmemb bytes backwards.

1.3. File Location:

File positioning refers to reading or setting the current reading and writing point of the file, all the functions of reading and writing data through the file pointer, both read and write data from the file's current reading and writing point.

The commonly used functions are:

#include <stdio.h>

int feof (FILE * stream); The usual usage is while (!feof (FP))

int fseek (FILE *stream, long offset, int whence);//Set Current read-write point to offset whence length as offset

Long Ftell (FILE *stream); Used to obtain the current read and write location of the file stream

void Rewind (FILE *stream); Move the Read and write location of the file stream to the beginning of the file Èfseek (FP, 0, Seek_set);

Feof determine whether to reach the next end of the file (note that you will do it again after you reach the end of the file)

Fseek set the current read-write point to offset whence length, whence can be:

Seek_set (file opening –>0)

Seek_cur (file current position –>1)

Seek_end (end of File –>2)

Ftell getting the current read-write point

Rewind move the file's current read-write point to the file header

1.4. Format read and 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 the formatted string to the stream stream, beginning with F

The formatted string is written to the string str, beginning with S

1.5. Single character read and write:

Use the following function 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); È equivalent to fgetc (file* stream)

int PUTC (int c, FILE *stream); È equivalent to FPUTC (int C, file* stream)

int GetChar (void); È equivalent to fgetc (stdin);

int Putchar (int c); È equivalent to FPUTC (int c, stdout);

GetChar and Putchar Read and write data from the standard input and output stream, and other functions read and write data from the stream stream.

1.6. String read and write:

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

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

int puts (const char *s); È equivalent to fputs (const char *s, int size, stdout);

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

Fgets and Fputs Read and write a row of data from a stream stream;

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

Fgets can specify the size of the target buffer, so relative to get security, but when Fgets is called, if the number of characters in the current line in the file is greater than size, the next time fgets is called, the remaining characters of the line will continue to be read, and a newline character at the end of the line is retained when Fgets reads one line of characters.

Fputs does not automatically add newline characters at the end of a line, but puts automatically adds a newline character to the standard output stream.

Linux input and output with 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.