C programming notes day05 function declaration, file read/write, union type, enumeration type

Source: Internet
Author: User
Function declaration:
1. Implicit Declaration: If no declaration is made, the system can call the function based on the parameter type (errors may occur)
2. Explicit declaration: declaration before being called, for example, double add (double, double); function declaration parameters can only write types, and do not need to write function bodies.

File Operations:
Fopen -- open the file, file * pfile = fopen ("a.txt", "W ");
Fopen open mode:
R: only readable. The file must exist.
R +: readable and writable. The file must exist.
W: Write only. Create a file. If the file exists, delete the old file first.
W +: readable and writable. Create a file. If the file exists, delete the old file first.
A: It can only be written. If it does not exist, a file is created. If it exists, the file content is not modified and the content is appended.
A +: readable and writable. If the object does not exist, the file is created. If the object exists, the file content is not modified and the content is appended.
B: The file is operated in binary format and can be used in combination with the preceding two types.

Fwrite -- write content to the file, int arr [5] = {0}; fwrite (ARR, sizeof (INT), 5, pfile );
Fread -- read content from the file, int arr [5] = {0}; fread (ARR, sizeof (INT), 5, pfile );
Fclose -- close the file, fclose (pfile); pfile = NULL;
Fputs -- write content, char STR [20] = {'A', 'B'}; fputs (STR, pfile );
Fgets -- read content, char STR [20] = {0}; fgets (STR, 20, pfile); // read 19 characters from the file + '\ 0'
Ftell (File * pfile) -- get the distance from the start of the pointer. If the current pointer is the end of the file, you can get the file size.
Rewind (File * pfile) -- move the pointer position to the beginning

Fseek (File * pfile, int relativeposition, seek_flag flag) -- locate the read/write pointer
Flag:
Start with seek_set --
Seek_cur -- current location
Seek_end -- end of a file

Union: multiple variables share a piece of memory. At the same time, only one member variable has a valid value.
For example:
Union {
Int I;
Double D;
} Myunion;

Enumeration type:
Enum color {red, blue, green}; // The values converted to int are 0, 1, and 2 in sequence.
Enum color {Red = 1, Blue = 4, green}; // convert to int values in sequence:, 5
Usage:
Enum color {Red = 5, blue, green} mycolor, mycolor1; // defines two variables.
Mycolor = (Enum color) 7;
Cout <(INT) Blue <Endl; // directly use




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.