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

Source: Internet
Author: User
Tags fread 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. The specific differences and relationships are as follows:

The physical storage of text files and binary files in the computer file system is binary, which isno difference in physical storage is 01 yards ., this has no objection, their difference is mainly on the logical storage, that is, the coding.
The text file format is stored when the value is encoded as a character and then deposited into its character encoding binarytext files represent and store data in ' characters ' as units, for example, for 1, a text file would think of it as the character ' 1 ' and then save its ASCII encoded value (which is assumed to be ASCII encoding), which is physically 0x31 the binary value, and ifBinary Save 1, the second binary value is saved directly, for example, if the program is processing 1 integers, the binary value saved is 0x00000001 (4 bytes).
Of course, if the program is stored by character is char ch = ' 1 ', then the binary saved value is its ASCII code, because the binary of the variable is its ASCII code. Can be summed upBinary is the encoding of the value itself, then it is the indefinite length of the code, because the value itself is unequal byte, such as an integer of 4 bytes then save in a binary file is the native binary value of these four bytes.

In conclusion, you can know that the text file and binary file is not the same as the encoding, and this is the user behavior, the data in what kind of encoding (character or value itself) into the file is the user actively selected, that is, the interface to write the choice, if the binary interface to write to the file is a binary file, If you write a file as a character, it is a text file. Since there is a write time encoding will have to read the code, only two encoding corresponding to read the correct results, such as open a binary file with Notepad will be garbled, here a little mention of the suffix name, the suffix is not determined whether it is a text file, the binary file can also be the TXT suffix name,suffix is only used to associate open programs to users to make notes, and the specific encoding of the file does not matter

You can use the character interface to read and write binary files, only need to do some processing, so so-called binary files, text files are mainly embodied in the read-write method here. In addition, there is a clear difference between windows and the text file read and write, the line will be changed \ n automatically replaced \ r \ n.

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

File operation mode

Depending on how the application accesses the file, it can be divided into file operations with buffers and non-buffered file operations, the specific differences are briefly described as follows:

Buffered file operations: Advanced file operations will automatically open a memory buffer in the user space for the files being used, and the I/O functions that follow the ANSI C standard are buffered file operations

Non-buffered file operations: Low-level file operations, if required, can only be set by the user himself in the program for each file buffer, followed by POSIX standard system call I/O function is this type.

Specific 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 stream, the buffer is the most important unit, according to the need can be divided into full buffer, row buffer, no buffering.

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

Problem ResolutionAbout streams and their functions

On a Linux system, the system opens three files per process by default, that is, each process can operate by default three streams: standard input stream, output stream, error stream.

The macro definition in the system source file is as follows:


The main functions of the file stream are:

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

2 ' buffering function: The data is read and written in a centralized, thus reducing the number of system calls

File stream pointers

At the application programming level, the operation of the program convection is mainly embodied in the file stream pointer, before the operation of a file, you need to open the file with fopen, and then return the file stream pointer associated with the file, All read and write operations for this file are done through a file pointer. The following is the structure of the files that can be accessed at the application layer, so struct members can access them in the 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 Descriptor # if 0}
The above structure contains all the information that the I/O library needs to manage the flow.

The following is a 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 parameter, read the file {perror ("open1");    Exit (Exit_failure);        } if ((Fp_des=fopen (argv[2], "w+")) = = NULL)//From the second command-line file parameter, write the file {perror ("open2");    Exit (Exit_failure);                  } setvbuf (fp_src,buffer1,_iolbf,128);         Explicitly set the location and type of the buffer the PRT content under do {/* * Below is a hint of content that is obtained from 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 operation are as follows:


The final copy succeeds.

Buffer type

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

For standard flow ANSI C there are the following requirements:

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 sample program for testing the 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. Three of these buffers are defined as follows:


A sample code is as follows:

/* 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

Specific code see STDIO.H source code

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

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

For ASCII files, it can be judged by whether =eof

For binary files, you need to use feof to determine whether to end, return 1. Otherwise, 0.

The Ferror function determines whether there is an error and returns 0 without errors.

File stream Positioning

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

Fseek () function modifies 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, under ANSI C standard, the first need to contact the source and destination files to open the source file in a read-write way 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.

One of the most important is to determine whether the file is finished. One way is to use the feof function, and the other is to use the return value of the read operation, for example, 128 bytes per read, the number of bytes will be less than 128 if the end position is read, or 1 if there is an error.

One implementation code is as follows:

#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)//Open the file specified in read-only form 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 concrete results are not demonstrated. You can compile your own validation.


ElSE

Format input and output operations for streams

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

1.

printf () and scanf (0 functions.

2.

fprintf () function and fscanf () function

3.sprintf function

4.SSCANF () function

Here's a 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 directory management

Common file \ Connection file and directory file property management


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

Related Article

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.