Linux Programming Learning Notes----ANSI C file I/O management

Source: Internet
Author: User
Tags fread posix rewind stdin

Reprint Please specify source: http://blog.csdn.net/suool/article/details/38129201


Problem Introduction

Types of files

Depending on how the data is stored, you can divide the file into text files and binary files. Detailed differences and relationships such as the following:

text files and binary files in the computer file system physical storage is binary, that is, there is no difference in physical storage is 01 yards , this no objection. Their differences are mainly on logical storage, which is coding.
when the text file format is stored as a character and then into its character encoded binary , the text file uses ' character ' as the unit to represent and store the data , for example, for the 1 value, the text file will be considered a character ' 1 ' then save its ASCII encoded value (this is assumed to be ASCII encoding), which is physically 0x31 this binary value. In the case of binary saving 1, the binary value is saved directly , for example, assuming that the program is processing 1 as an integer, the binary value saved is 0x00000001 (4 bytes).
Of course, assuming that the program is stored by character char ch = ' 1 ', then the binary saved value is its ASCII code. Because the binary of the variable is originally its ASCII code.

Can summarize the binary file is the value of the encoding itself, then is the indefinite length of the code, because the value itself is unequal byte , such as an integer 4 bytes then the binary file is stored in the binaries is the original binary value of four bytes.



In conclusion, it is possible to know that the text file is not the same as the binary file, and this is the user behavior, which is the user's choice of what kind of encoding (the character or the value itself) the file is chosen by the users, that is, the interface to write. Suppose a binary interface is written to a file then it is a binary file, assuming that writing the file as a character is a text file.

Since there is a write-time encoding will have the readout of the code, only two encoding corresponding ability to read the correct results. If you open a binary file with Notepad, it will appear garbled. Here the suffix name, the suffix is not determined whether it is a text file, the binary file can also be a txt suffix name, the suffix is only used to associate open the program to the user to do notes. There is no relation to the detailed encoding of the file .

Ability to read and write binary files using the character interface. Just need to do some processing on it, so so-called binary files, text file main body is now read and write way here. In addition, there is a noticeable difference between windows and the text file read and write, the line will be replaced \ \ \ r \ n.



The final text files and binaries are primarily Windows-based concepts, and Unix/linux does not differentiate between the two files. They treat all documents equally. Treat all files as binary files.

File operation mode

Depending on how the application has access to the file, it can be divided into file operations with buffers and non-buffered file operations, with detailed differences such as the following:

Buffered file operations: Advanced file operations, in the user space themselves actively for the files being used to open a memory buffer, following the ANSI C standard I/O function is buffered file operation

Non-buffered file operations: Low-level file operations, if necessary, only by the user himself in the program to set a buffer for each file, followed the POSIX standard system calls I/O function is such a type.

For detailed differences See: http://www.360doc.com/content/11/0521/11/5455634_118306098.shtml


ANSI C library function in order to achieve his characteristics, using the concept of flow, in the implementation of the flow, the buffer is the most important unit, according to need to be able to be divided into full buffer, row buffer, no buffering.

The following is a detailed discussion of the methods and functions related to the flow of ANSI C and file I/O.

Problem ResolutionAbout streams and their functions

In Linux, the system defaults to three files per process, that is, each process can operate three streams by default: standard input stream, output stream, error stream.

The macro definitions in the system source file are as follows:


The main functions of the file stream are:

1 ' formatted content: Enables different input and output format conversions.

2 ' buffering function: Reduce the number of system calls by centralizing the data read and write

File stream pointers

At the application programming level, the operation of the program convection is mainly in the body of the file stream pointer now, before manipulating a file, you need to open the file with fopen, and then the file stream pointer that returns the file is associated with the file. All read and write operations for the file are completed through the file pointer. The following is the structure of files that can be visited at the application level, so struct members can access them in user space:

struct _io_file {  int _flags;           /* High-order word is _io_magic; Rest is flags. */#define _IO_FILE_FLAGS _flags/  * The following pointers correspond to the C + + STREAMBUF protocol. */  * NOTE:
   TK uses the _io_read_ptr and _io_read_end fields directly. */  char* _io_read_ptr;   /* Current Read pointer */  char* _io_read_end;   /* End of Get area. */  char* _io_read_base;  /* Start of Putback+get area. */  char* _io_write_base;/* Start of Put area. */  char* _io_write_ptr;  /* current put pointer. */  char* _io_write_end;  /* End of Put area. */  char* _io_buf_base;   /* Start of the reserve area. */  char* _io_buf_end;    /* End of reserve area. */..............  int _fileno;                         /File Description Descriptor # if 0}
The above structure includes all the information that the I/O library needs to manage the flow.

The following is a demo sample program that prints an open file flow pointer pointer member information, in which a copy of the file is implemented using standard C library functions, and the current read-write location and buffer information is printed in real time during the copy process.

The code is as follows:

#include <stdlib.h> #include <stdio.h> #include <string.h> #define PRT (content,msg) printf (CONTENT "                                : \t%p\n ", MSG) int main (int argc, char *argv[]) {FILE *fp_src,*fp_des;                        File read-write pointer char buffer[10],buffer1[128];    File read-write buffer int i;        if ((Fp_src=fopen (argv[1], "r+")) = = NULL)//From the first command-line file, read the file {perror ("open1");    Exit (Exit_failure);        } if ((Fp_des=fopen (argv[2], "w+")) = = NULL)//From the second command-line file, write the file {perror ("open2");    Exit (Exit_failure);                  } setvbuf (fp_src,buffer1,_iolbf,128);         Explicitly setting the location and type of the buffer do {/* * below the PRT content is a hint of content, which is obtained by reading the file structure that the application layer can access.     */PRT ("Src_io_read_ptr", fp_src->_io_read_ptr);        Source file read location, source prt ("_io_read_end", fp_src->_io_read_end);        PRT ("_io_read_base", fp_src->_io_read_base);        PRT ("Src_io_write_ptr", fp_src->_io_write_ptr); PRT ("_io_write_base", FP_src->_io_write_base);        PRT ("_io_write_end", fp_src->_io_write_end);        PRT ("_io_buf_base", fp_src->_io_buf_base);        source file buffer Position prt ("_io_buf_end", fp_src->_io_buf_end);        memset (buffer, ' + ', 10);                   i = fread (BUFFER,1,10,FP_SRC);                       Read fwrite (buffer,1,i,fp_des);          Write PRT ("Des_io_read_ptr", fp_des->_io_read_ptr);   PRT ("Des_io_write_ptr", fp_des->_io_write_ptr);    Target file Write location}while (i==10);    Fclose (FP_SRC);                                      Fclose (Fp_des); Close file read/write}

The results of the execution are as follows:


Finally successfully copied.

Buffer type

Just said three types of buffer, they are still in the system source code files have been defined.

For standard flow ANSI C there are requirements such as the following:

1. Standard input and output: when and only if the interaction device is not involved, they are fully buffered

2. Standard error: It will never be fully buffered

The following is a demo sample program for the test buffer type.

Code:

#include <stdio.h>void pr_stdio (const char *, file *); int main (void) {file *fp;    Fputs ("Enter any character\n", stdout);    if (GetChar () ==eof) printf ("GetChar error");    Fputs ("One line to Standard error\n", stderr);                    Pr_stdio ("stdin", stdin);                   Test for standard input stream Pr_stdio ("stdout", stdout);                   Test for standard output stream Pr_stdio ("stderr", stderr);    Test for standard error stream if (fp = fopen ("/ETC/MOTD", "r") = = NULL)//plain file printf ("fopen error");    if (fgetc (fp) = = EOF) printf ("Getc error");    Pr_stdio ("/ETC/MOTD", FP); return (0);}     void Pr_stdio (const char *name, FILE *fp) {printf ("stream =%s,", name);    if (Fp->_flags & _io_unbuffered)//unbuffered printf ("unbuffered");    else if (Fp->_flags & _io_line_buf)//row buffer printf ("line buffered");                 else printf ("fully buffered"); AllBuffer printf (", Buffer size =%ld\n", fp->_io_buf_end-fp->_io_buf_base);} 

The results are as follows:

Specifies the buffer type, through the SRETBUF function. Among the three types of buffers, such as the following:


A demo sample code such as the following:

/* Example Show usage of setbuf () &setvbuf () */#include <stdio.h> #include <error.h> #include <string.h    >int Main (int argc, char * * argv) {int i;    FILE * FP;    Char msg1[]= "hello,wolrd\n";    Char msg2[] = "Hello\nworld";    Char buf[128]; Open a file and set Nobuf (used Setbuf). and write string to It,check it before close of flush the stream if (fp = FOP        En ("No_buf1.txt", "w")) = = NULL) {perror ("File Open failure!");    Return (-1);                                    } setbuf (Fp,null);    Set the Buff NULL memset (buf, ' + ', 128);                        Fwrite (MSG1, 7, 1, FP);    Write message to File printf ("Test setbuf (no buf)!check no_buf1.txt\n");            printf ("Now BUF data is:buf=%s\n", buf);    Test buff printf ("Press ENTER to continue!\n");    GetChar ();    Fclose (FP); Open a file and set Nobuf (used setvbuf). and write string to It,check it before close of flush the stream if (fp = fo Pen ("no_buf2.tXT "," w ")) = = NULL) {perror (" File Open failure! ");    Return (-1);    } setvbuf (FP, NULL, _IONBF, 0);                                memset (buf, ' 128 ');    Clear the Buff fwrite (MSG1, 7, 1, FP);    printf ("Test setvbuf (no buf)!check no_buf2.txt\n");    printf ("Now BUF data is:buf=%s\n", buf);    printf ("Press ENTER to continue!\n");    GetChar ();    Fclose (FP); Open a file and set line BUF (used setvbuf). and write string (include ' \ n ') to it,////check it before close of flu        SH the stream if (fp = fopen ("L_buf.txt", "w")) = = NULL) {perror ("File Open failure!");    Return (-1);    } setvbuf (FP, buf, _iolbf, sizeof (BUF));    memset (buf, ' 128 ');    Fwrite (MSG2, sizeof (MSG2), 1, FP);    printf ("Test setvbuf (line buf)!check L_buf.txt, because line buf, only data before enter send to file\n");    printf ("Now BUF data is:buf=%s\n", buf);    printf ("Press ENTER to continue!\n");    GetChar ();    Fclose (FP); Open a FIle and set full buf (used setvbuf). and write string to it for 20th time (it's large than the BUF)//check it before CL        OSE of Flush the stream if (fp = fopen ("F_buf.txt", "w") = = NULL) {perror ("File Open failure!");    Return (-1);    } setvbuf (FP, buf, _IOFBF, sizeof (BUF));    memset (buf, ' 128 ');    Fwrite (MSG2, sizeof (MSG2), 1, FP);    printf ("Test setbuf (full buf)!check f_buf.txt\n");    printf ("Now BUF data is:buf=%s\n", buf);    Check data of the current buff printf ("Press ENTER to continue!\n");    GetChar (); Fclose (FP);}

Result


ANSI C file I/O operations

1. Open the close file three functions:

fopen ()

Fclose ()

Fflush ()//Refresh Stream

Detailed code See STDIO.H source

2. Read and Write files

Character units

1. Character Reading

Reading from the stream


From the stdin:

/* Read a character from stdin.   This function was a possible cancellation point and therefore not   marked with __throw.  */extern int GetChar (void);
2. Character writing

/* Write a character to STREAM.   These functions is possible cancellation points and therefore not   marked with __throw.   These functions is a possible cancellation point and therefore not   marked with __throw.  */extern int FPUTC (int __c, file *__stream), extern int putc (int __c, file *__stream);/* Write a character to stdout.
   
    this function is a possible cancellation point and therefore not   marked with __throw.  */extern int Putchar (int __c);
   
Line Units

Line read-Out & line Write

/* Write a string to STREAM.   This function was a possible cancellation point and therefore not   marked with __throw.  */extern int fputs (const char *__restrict __s, FILE *__restrict __stream);/* Write a string, followed by a newline, to St Dout.   This function was a possible cancellation point and therefore not   marked with __throw.  */extern int puts (const char *__s);

Block units

Write

/* Read chunks of generic data from STREAM.   This function was a possible cancellation point and therefore not   marked with __throw.  */extern size_t fread (void *__restrict __ptr, size_t __size,     size_t __n, FILE *__restrict __stream) __wur;/* Write Ch Unks of generic data to STREAM.   This function was a possible cancellation point and therefore not   marked with __throw.  */extern size_t fwrite (const void *__restrict __ptr, size_t __size,      size_t __n, FILE *__restrict __s);

File stream detection

That is, whether the file has been read or written to the end or error., use the feof function.

For ASCII files, it is possible to infer by =eof

For binary files, you need to use feof to infer whether the end is complete and return 1. otherwise 0.

The Ferror function infers if there is an error and returns 0 without errors.

File stream Positioning

The Ftell () function returns the current read and write location of the stream, the number of bytes from the beginning of the file

Fseek () function changes the current read and write location

The rewind () function resets the read-write position to the beginning.

/* Seek to a certain position on STREAM.   This function was a possible cancellation point and therefore not   marked with __throw.  */extern int fseek (FILE *__stream, long int __off, int __whence);/* Return the current position of stream.   This function was a possible cancellation point and therefore not   marked with __throw.  */extern long int ftell (FILE *__stream) __wur;/* Rewind to the beginning of stream.   This function was a possible cancellation point and therefore not   marked with __throw.  */extern void Rewind (FILE *__stream);

Instance Application

In order to achieve the disk file replication, the ANSI C standard, the first need to contact the source file and the target file to open the source file, write to open the target file, the source file is written in the data set to the buffer, and then from the memory to write to the disk where the destination file resides.

The most important thing is to infer whether the file is finished. One way is to use the feof function, and another way is to use the return value of the read operation, for example, 128 bytes per read, only to read the end of the poison when the number of bytes will be less than 128, assuming that an error returns-1.

An implementation code such as the following:

#include <stdio.h>int main (int Argc,char *argv[]) {    FILE *fp=null;    char ch;    if (argc<=1)    {        printf ("Check usage of%s \ n", argv[0]);        return-1;    } if ((Fp=fopen (argv[1], "R")) ==null)//The file specified in the read-only form of argv[1]    {        printf ("Can not open%s\n", argv[1]);        return-1;    }    while (CH=FGETC (FP))!=eof)   //The data in the opened file is output byte-by-bit to the standard output stdout        FPUTC (ch,stdout);    Fclose (FP);       Close file    return 0;}
The detailed results are not demonstrated. Ability to compile validation on its own.


ElSE

Format input and output operations for streams

In fact, it is a few functions of the explanation, this part of a lot of information, so not in the talk, the detailed can own Google or look at the source.

1.

printf () and scanf (0 functions.

2.

fprintf () function and fscanf () function

3.sprintf function

4.SSCANF () function

Here's a demo sample program that uses SSCANF to get CPU frequency:

#include <stdio.h> #include <string.h> float get_cpu_clock_speed () {       file* fp;       Char buffer[1024];       size_t Bytes_read;       char* match;       float Clock_speed;              fp = fopen ("/proc/cpuinfo", "R");       Bytes_read = fread (buffer, 1, sizeof (buffer), FP);       Fclose (FP);       if (Bytes_read = = 0 | | bytes_read = = sizeof (buffer))       return 0;       Buffer[bytes_read] = ' + ';       Match = strstr (buffer, "CPU MHz");//Match       if (match = = NULL)       return 0;       SSCANF (Match, "CPU MHz:%f", &clock_speed);//read       return clock_speed;} int main (void) {       printf ("CPU clock Speed:%4.0f mhz\n ", Get_cpu_clock_speed ());       return 0; }

Result


Reference

About binary and text files http://www.cnblogs.com/whutzhou/p/3215210.html

Linux Advanced Programming Joseph Lee

NEXT

POSIX File and folder management

Common file \ Connection file and folder file property management


Reprint Please specify source: http://blog.csdn.net/suool/article/details/38129201

Linux Programming Learning Notes----ANSI C file I/O management

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.