C Language-file operations

Source: Internet
Author: User

Summarize what you have learned this afternoon:

13.1

Files are typically a named store on a hard disk or SSD, and there are two types of "text mode" and "binary mode" when interacting with a file using the C language. There is no difference between the two modes for UNIX and Linux, but for other systems, the partitioning of the storage area is different.

I/O (input and output) can choose two standards, one is the operating system carries the underlying I/O, and the C program carries a standard I/O package, which is portable.

Each C program automatically opens three files, standard input, standard output, and standard error output, and the third one sends error messages to the display, which is a standard notation.

For standard I/O, in addition to carrying a large number of file manipulation functions to use, there is also an advantage is that "input and output are buffered."

13.2

Exit gives control to the operating system, return gives control to the previous level (i.e. Exit Function), except in the recursive main function, exit and return are the same effect;

Doubt: The book says that UNIX and Lniux have only one file type, but the information found is not ...

fopen when opening a file, there are several modes, where the Append ' + ' indicates extended permissions, ' x ' means ' Safe mode ' (I named it myself), ' B ' denotes binary.

The FP points to a data object.

The input and output functions of the file are used stdout for standard input and output.

If GETC () reads to the end of the file, it will return EOF

The distinction between scanf/printf and fscanf/fprintf is based on the relationship to memory.

If the disk is full, the disk is moved, or an I/O error occurs, the fclose () function fails;

The standard way to open files:

if (in = fopen (name,"w")) = = NULL) {    "Can ' t creat output file.\n");    Exit (Exit_fallure); }

Exit (exit_fallure) means exit the program and close all files;

13.3

At the same time the number of operating files is limited, the range is generally 10-20.

When writing programs, constants are often defined using code like # define MAX 41, which is intended to be convenient for future modifications.

A null character (the terminating flag of a string) is not the same as a space character.

The carriage return and newline characters are different, enter the return key in the big keyboard under Windows, and enter ' \ R ' and line feed ' \ n '.

The get () function involves cross-border issues, preferably not appearing in formal program editing, and can be replaced with fgets (), but because of the difference between fgets and gets when dealing with newline characters, a self-written s_gets () is used in CPP, And C11 with an extensible get_s (), the specific use of the compiler will involve the choice of ... After a survey, it's time to get started with VS and compile with clang.

13.5

Each time the function operates on a file pointer, it moves a pointer in the file structure data body that the file pointer points to.

Fseek () and Ftell () are used to move and manipulate file pointers.

Fgetpos () and Fsetpos () are functions that operate on large files.

13.7

Here to record some file operation functions, not commonly used, but put here to give yourself a wake up ...

ungetc--putting the execution character back into the input stream

fflush--Update Stream

setvbuf--creates a buffer that is used for the replacement of standard I/O functions.

Fread and Fwrite operate the data in binary, and the parameters need to be noted, which is useful for exams:

size_t fwrite (constvoid * Restrict ptr, size_t size, size_t nmemb, FILE * restrict FP);

This is very convenient ...

feof--returns a non-0 value when the end of the file is detected at the last input call (wait for a program to try)

ferror--when a read or write error occurs, FERROR returns a value other than 0, otherwise returns 0.

Above.

C Language-file operations

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.