Chapter 5 database functions

Source: Internet
Author: User

 

It took me some time to read chapter 5 today ~ Although I can barely understand it

 

It's not very profound. Besides, I think it's hard to express myself, and I think it's abstract.

 

1.Returns the integer getchar.Function

 

The getchar function generally returns the next character in the standard input file.

 

(Defined in stdio. H, the value is-1 ).

 

Example:

 

# Inclide <stdio. h>

 

Int main ()

 

{

 

Char
C;

 

While
(C = getchar ())! = EOF)

 

Putchar
(C );

 

Return
0;

 

}

 

When variable C is defined as char type, because the returned value is int type, this means that the variable cannot accommodate all possible

 

, Especially EOF. Therefore, there are three situations.

 

1. Some valid input characters are truncated so that the value of the variable is the same as that of EOF. The program will be halfway through File Replication

 

Termination

 

2. It is impossible for a variable to obtain this value. The program will be in an endless loop

 

3. The program "works normally", but this is a coincidence. The compiler truncates the return value of the getchar function and

 

The byte is assigned to variable C, but the comparison expression does not compare C and EOF, but the return value of getchar.

 

And EOF, so that the example can be "normal.

 

2.Update ordered files

 

Standard input/output libraries in many systems allow the program to open a file and write and read the file at the same time,

 

However, in order to maintain backward compatibility with the past, an input operation cannot be followed directly by an output operation, and vice versa.

 

To perform both input and output operations, you must insert a call to the fseek function. (Second parameter of fseek

 

Is of the long type; sizeof returns an unsigned value)

 

File * FP;

 

Struct record REC;

 

...

 

While (fread (char *) & rec, sizeof
(REC), 1, FP) = 1 ){

 

/*
Perform certain operations on Rec */

 

If
(/* Rec must be written again */){

 

Fseek
(FP,-(long) sizeof (REC), 1 );

 

Fwrite
(Char *) & rec, sizeof (REC), 1, FP );

 

Fseek
(FP, 0l, 1 );

 

}

 

}

 

The second fseek function does not seem to have done anything, but it changes the file state so that the file can now be normal.

 

.

 

3.Buffer output and Memory Allocation

 

Whether to immediately display the output generated by a program to the user depends on different programs. Program

 

There are two types of output: one is instant processing, and the other is temporary storage, and then large data writing. Previous

 

To cause a high system burden. Therefore, the C language usually allows programmers to control the generation

 

The output data volume, and this control capability is usually achieved through the library function setbuf. If you use a Buf Array

 

The buffer size is defined by the bufsize in stdio. h file. In addition

 

The program has cleared the array before returning the control to the system. You need to define the Buf array as the static type.

 

Or put it outside the main function.

 

4.Use errnoDetection Error

 

We should first check the returned value as an error indication, and then check errno after the program fails to be executed.

 

Chu error cause:

 

/* Call the library function */

 

If (returned error value)

 

Check errno and handle it

 

5.Library function signal

 

As a way to capture asynchronous events, signal must add the signal. h header file before use.

 

For security consideration, do not use this function to capture complicated asynchronous events of functions. At the same time, use the longjmp Function

 

It is not safe to exit the signal function. For arithmetic operation errors, the unique secure and portable signal function processing

 

The operation is to print an error message and exit the program using the longjmp or exit function.

 

The signal is very complex and tricky, and it has some characteristics that cannot be transplanted in essence. Solve this problem most

 

Good to adopt a "defensive", make the signal processing function as simple as possible, and organize them together.

 

 

 

 

This is the fifth chapter. Because I am not familiar with library functions, it is easy to write.

 

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.