File type, C language file read/write, File Buffer, file opening method, file operation function

Source: Internet
Author: User
Tags rewind to domain

File type, C language file read/write, File Buffer, file opening method, file operation function
File types include stream files and device files. device files such as VGA interfaces, serial ports, USB ports, network ports, and serial ports are abstracted into files by the operating system.

When we write a program, three files are opened by default.

Stdin: standard input, stdout: standard output, stderr: standard error. scanf actually receives standard input data. In this case, standard input is our Keyboard.

There are four ways to clear the buffer:

  • A. Add '\ n ';

    B. The program Exits normally;

    C. You can also clear the buffer through fflush (stdout.

    D. The buffer zone is automatically cleared when it is full (the default buffer size of the program is 8192 bytes (defined in C )).

    If the buffer is designed, it is best to use the queue method for design. The reason for using the buffer in C language is to increase the buffer. The refresh buffer is used to refresh the buffer above stdout.

    3. Observe the following files:


    (1) When '\ n' is added (row buffer refresh, this' \ n' is hard to work when disk files are encountered), as follows:


    At this time, helloworld can be printed, indicating that '\ n' can print the result of the program.

    (2) comment while (1); the program running result is as follows:


    The above phenomenon indicates that if the program Exits normally, helloworld can also be printed.

    (3) Use fflush (stdout); you can also output strings.


    (4) The fourth method is to wait until the buffer in the program is used up.



    The above method indicates that if the buffer is full, the result value can also be printed.

    Conclusion: There are four methods to clear the data.


    4. view the methods defined in the header file: cd/usr/include

    View cat libio. h to view the FILE



    FILE nature:



    Fopen: Open the FILE. The parameter type is FILE * fopen (const char * path, const char * mode );

    The first parameter indicates the path of the file to be opened, mode indicates the access attribute: Read and Write appending, and fope returns a file pointer type (the path here can be relative path and absolute path, absolute path is recommended). It is a FILE type and a struct. FILE is a struct defined by typedef.

    FILE Type:


    There are 6 modes:

    Mode file type

    Operation Method

    R

    Open the file as a read, read and write pointer location, starting with the file

    W

    Write to open the file. If the file does not exist, create the file. If the file exists, cut off the source file (the so-called source file refers to only a part of memory). The read/write pointer starts with the file.

    A

    Append write to open the file. If the file does not exist, create the file,

    R +

    Open a file in readable and writable mode. If the file does not exist, it is not created.

    W +

    Readable and writable open file. If the file does not exist, the file is created. If yes, the file is truncated.

    A +

    Readable and writable append to open the file. If the file does not exist, create

    More Window Operating Systems


    Rb

    The operation is a binary file. other meanings are the same as those of r.

    Wb


    AB


    Fclose: Close the FILE. The parameter type is int fclose (FILE * fp). The parameter indicates the specified FILE. Note that fclose actually calls the fflush () function.



    Note: The preceding "w" can be changed to.

    Determine the attributes of the opened file based on its own attributes. Pay attention to unauthorized access.

    When opening the file, if the pointer is put back successfully, if the pointer fails, NULL is returned.


    The maximum number of files opened by default in the operating system is 1024, but the maximum number of files can be modified.



    5. file operation functions:

    Fgets: reads a row from the system each time. Prototype: int * fgets (char * s, int size, FILE * stream); finally, the content is stored in size.

    Parameters:

    * S: random pointer, pointing to the address used to store the obtained data.

    Size: the size of the data that is stored in an integer.

    * Stream: file structure pointer, the file stream to be read.

    Return Value:

    1. If yes, the first parameter s is returned.

    2. If an end-of-file is encountered while reading characters, the eof indicator is configured in the standby mode. If no characters are read, the buf keeps the original content and returns NULL;

    3. If a read error occurs, the error indicator is set, and NULL is returned. The buf value may be changed.


    Fputs: Write a line to the FILE. prototype: int fputs (const char * s, FILE * stream );


    The running result is as follows:


    File copy code:



    Fgetc: Read a character at a time. The function prototype is int fgets (FILE * fp). A read character is returned. If an error is returned, the EOF is returned.

    Meaning: Read a character from the object pointed to by the file pointer stream. After reading a byte, move the cursor to a byte.


    Fgetc () returns the read characters. If EOF is returned, it indicates the end of the file or an error occurs.

    Example


    Fputc:

    Function Name: writes the character file function fputc ();

    Function Format: int fputc (int n, FILE * fp)

    Parameter explanation: fp is the file pointer, and its value is obtained when the fopen () command is executed to open the file.

    N indicates the number of output characters.

    Although a function is defined as an integer, it uses only eight lower bits.

    Return Value: When a function is called normally, the function returns the ASCII code value of the read character. If an error occurs, the function returns EOF (-1). When a character or byte is correctly written

    After the data is written to the file, the internal write pointer will automatically move a byte location. EOF is a macro defined in the header file stdio. h.




    Size_t fread (void * ptr, size_t size, size_t nmemb, FILE * fp); is a function. Reads data from a file stream. A maximum of nmemb elements are read. Each element size is a byte. If the call is successful, the number of actually read elements is returned. If the call is unsuccessful, 0 is returned.The size of each record is size, and nmemb elements should be read. size_t fread (void * ptr, size_t size, size_t nmemb, FILE * fp ); reading nmemb from * fp indicates the number of elements requested. Size: indicates the number of reads. In * ptr, fread is also read in binary mode.

    Fwrite: writes files as records. fwrite writes binary data.

    Function prototype: size_t fwrite (const void * buffer, size_t size, size_t count, FILE * stream );

    Note: The functions here operate files in binary format, not limited to domain text files

    Return Value: return the actually written data block.

    1. Buffer: A pointer. For fwrite, It is the address for obtaining data;

    2. Size: Number of Single-word segments to be written

    3. Count: the number of data items to write size bytes.

    4. Stream: pointer to the target file;

    5. Returns the number of actually written data items count. (size_t is an unsigned int on a 32-bit platform and a long unsigned int on a 64-bit platform)

      Fflush (FILE * fp): refresh the buffer.


      Fflush (FILE * fp)


      Fflush () can also be called when the program exits automatically. The function refresh method is called when the main function returns. Exit (main (); closes open files and releases the applied memory.

      Feof: whether the test file reaches the end

      1. Fseek:

      2. When the second parameter in fseek is too large, this indicates expanding one piece. Note that files must be expanded only when the second write operation is performed.

        Rewind: point the pointer to a read/write pointer file to the beginning of the file. However, the fseek function is powerful, and rewind calls fseek.

        Zookeeper


  • 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.