Input and Output Functions in C Language

Source: Internet
Author: User
1. Error Report

1. The perror () function is a simple and unified method for reporting errors. Many functions of the ansi c function library call the operating system to complete some tasks, especially for I/O functions. At any time, when the operating system executes anything as required, there is a possibility of failure. The standard library function saves the error code in an external integer variable errno (defined in errno. h) and passes this information to the user program, prompting you for the exact cause of operation failure.

2. perror () function prototype:

# Include <stdio. h>

Void perror (charconst * message );

If the message is not null and points to a non-empty string, the perror function prints the string followed by a semicolon and a space, then, a message is printed to explain the current error code of errno.

3. The biggest advantage of the perrno function is that it is easy to use. Good programming practices require that any operation reads that may produce errors should be checked after execution to determine whether it is successfully executed.

Note:

Errno is set only when a library function fails. When the function is successfully executed, the errno value will not be changed. Therefore, you cannot test the errno value to determine whether an error has occurred. Therefore, it is meaningful to check the errno value only when the called function prompts an error.

Ii. terminate execution

1. The function for terminating execution is exit, which is used to terminate the execution of a program.

2. Its prototype is:

# Include <stdlib. h>

Void exit (intstatus );

3. The status parameter is returned to the operating system to indicate whether the program is successfully completed.

This value is the same as the status value of the integer returned by the main () function. Pre-defined symbols exit_success and exit?failure indicate whether the program is successfully terminated or failed.

4. This function can be used when the program finds an error that makes it unable to continue execution. We often call exit to terminate the program after calling perrno.

5. This function does not return values. After the exit function is complete, the program disappears.

3. Text Stream

1. streams are divided into text streams and binary streams.

2. Some features of text stream may vary in different systems. One of them is the maximum length of the text line. The standard allows at least 254 characters. Another feature that may be different is the end mode of text lines.

1> In the MS-DOS system, text lines are scheduled to end with a carriage return and a linefeed.

2> in Unix systems, only one line break is used to end.

3. A standard text line is defined as zero or multiple characters followed by a line break indicating the end.

Iv. binary stream

1. bytes in a binary stream are written into a file or device in the form of a program, and read into the program in the form of a file or device.

2. This type of stream applies to non-text data. However, if the I/O function modifies the last line character of a text file, it can also be used for text files.

Five files

1. One of the declarations contained in stdio. H is the file structure. File is a data structure used to access a stream.

2. If several streams are activated at the same time, each stream has a corresponding file associated with it. To perform some operations on the stream, you can call some suitable functions and pass them a file parameter associated with the stream.

3. For each ansi c program, the system must provide at least three streams during runtime:

1> standard input: The standard input is the default input source.

2> standard output: The standard output is the default output setting.

3> standard error: the place where the error is written. The perror function also writes its output to this place. In many systems, standard output and standard errors are the same by default. However, if a different stream is prepared for the error message, even if the standard output is redirected to another place, the error message will still appear on the screen or other default output devices.

These streams are named stdin, stout, and stderr. They are all pointer to the file structure.

4. Many operating systems allow users to modify the default standard input and output devices during program execution.

For example:

Both MS-DOS and UNIX Systems Support input/output redirection using the following method: Program <DATA> answer

When the program is executed, it will read from the file data instead of the keyboard as the standard input, it will write the standard output to the file answer instead of the screen.

5. The program declares a pointer variable for each file that must be simultaneously active. Its type is file *. This Pointer Points to the file structure, which is used by the stream when it is active.

6. Open the function by calling the fopen function. To open a stream, you must specify the files or devices to be accessed and their access methods (such as reading, writing, or both reading and writing ). Fopen () and operating system authentication files or devices exist and the file structure is initialized. Then, read or write the file as needed. Finally, call the flose function to close the stream. Disabling a stream can prevent the files associated with it from being accessed again and ensure that any data stored in the buffer zone is correctly written to the file, release the file structure so that it can be used for other files.

7. I/O functions process data in three basic forms: single character, text line, and binary data. For each form, there is a set of specific functions to process them.

8. Open the stream

1> the fopen () function opens a specific file and associates a stream with the file. The prototype is as follows:

File * fopen (char const * Name, char const * mode );

Both parameters are strings.

Name is the name of the opened file or device. The rules for creating file names vary in different systems. Therefore, fopen () uses the file name as a string rather than a parameter for path name, drive letter, and file extension name. This parameter specifies the name of the file-file * variable to be opened. The variable is used by the program to save the value returned by fopen and does not affect the file to be opened.

The mode parameter indicates whether the stream is read-only, write-only, read-write, and whether it is a text stream or binary stream.

The Mode starts with R, W, or a, indicating whether the opened stream is used for reading, writing, or adding. If a file is opened for reading, it must already exist. However, if a file is opened for writing, if it already exists, its original content will be deleted. If it does not exist, create a new file. If a file used for adding does not exist, it will be deleted. If it already exists, its original content will not be deleted. In either case, data can only be written from the end of the file.

Adding "A +" to the mode indicates that the file is opened for update and the stream can be read or written. However, if you have read some data from a file, you must call one of the file locating functions (fseek, fsetpos, and rewind) before writing data to it ).

After writing some data to a file, if you want to read some data from the file, you must first call the fflush function or one of the file locating functions.

If the fopen () function is successfully executed, it returns a pointer to the file structure, which represents the newly created stream. If the function fails to be executed, it returns a null pointer, and errno will prompt the nature of the problem.

Note: The Return Value of the fopen function should always be checked. If the function fails, it returns a null value. If the program check is incorrect, the NULL pointer will be passed to the subsequent I/O functions.

9. Close the stream

1> the stream is closed with the fclose function. Its prototype is as follows:

Int fclose (File * F );

For the output stream, the fclose function refreshes the buffer before the file is closed. If it is successfully executed, fclose returns zero; otherwise, EOF is returned.

Vi. Standard I/O Constants

1. A large number of constants related to input and output are defined in stdio. h. The actual value of EOF is several more characters than that of a single character, so that the binary is incorrectly interpreted as EOF.

2. How many files can a program open at the same time?

He can ensure that at least fopen_max files can be opened simultaneously. This constant contains three standard streams with at least 8 values.

3. The constant filename_max is an integer value that is used to indicate the size of a character array to accommodate the maximum valid file name supported by the compiler.

VII. Temporary Files

1. In the program, we will use a file to temporarily save data. When the program ends, the file is deleted because the data contained in the file is no longer used.

2. tmpfile () function

1> function: create a file. The file is automatically deleted when the file is closed or the program is terminated. This file is opened in WB + mode, which enables it to be used for binary and text data.

2> If a temporary file must be opened in another mode or opened by one program but read by another program, tmpfile function creation is not applicable. In these cases, the fopen function must be used, and the remove function must be used to display and delete the result file.

3> the temporary file name can be created using the tmpnam function. Its prototype is as follows:

Char * tmpnam (char * Name );

If the parameter passed to the function is null, the function returns a pointer to the static array, which contains the created file name. Otherwise, the parameter is assumed to be a pointer to a character array with a specified length of at least l_tmpnam. In this case, the file name is created in this array, and the return value is this parameter. In either case, the created file name will not have the same name as an existing file name. As long as the number of calls does not exceed tmp_max, The tmpnam function will generate a new name for each call.

8. File manipulation functions

1. There are two functions used to manipulate files without any input/output operations.

1> function prototype. If the execution is successful, both functions return zero values. If they fail, both return non-zero values.

Int remove (char const * filename );

Int Rename (char const * oldname, char const * newname );

2> the remove function deletes a specified object. If the file is open when the remove is called, The result depends on the compiler.

3> the rename function is used to change the name of a file from oldname to newname. If a file named newname already exists, the result depends on the compiler. If this function fails, the file can still be accessed using the original name.

 

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.