[C crazy handouts] (16) C language file operations, C language handouts operations

Source: Internet
Author: User

[C crazy handouts] (16) C language file operations, C language handouts operations

1. Basic concepts of Files

 

Stores datasets on external media. A dataset name is the file name.

 

File category:

 

1) user perspective: common files and Device Files

2) Storage content:

 

Ascii file (Text File ):

 

Storage Process: Find the corresponding ascii code value based on the text ----> convert to binary -->

Writing to file

 

Read process: Binary --> 10 hexadecimal --> Find the corresponding character --> display

 

 

 

Binary file:

 

Binary data access

 

File Operation Process:

 

1) import the header file stdio. h

 

2) define the file pointer

 

3) open the file

4) operation files

 

5) close the file

 

File Operation Principle

 

 

 

2. File pointer

 

FILE struct

FILE * fp; struct pointer

 

Purpose: store the first address of a file, pointing to a file

 

 

3. Open and Close a file

 

 

Open fopen (file name, operation method );

 

Fopen ("a.txt", "r ");

// A.txt is found in the products directory by default./Users/apple/Library/Developer/Xcode/DerivedData/C15-Integrated Project-cmassyrpkwjxularuygrjqhojbwj/Build/Products/Debug

 

Fopen ("/Users/apple/Desktop/a.txt", "r ");

 

Operation Method:

 

R w a B + t

Read/write append level-2 read/write text

 

W -- when writing a file, if the file does not exist, create

'Overwrite if it exists

 

R + w + wb + a +

 

Close the file:

 

Fclose (File pointer );

 

 

4. file operation functions

 

1) Reading and Writing of bytes-type data

 

Write:

Fputc ('characters', file pointer );

Char c = 'a ';

Fputc (c, fp );

 

Read:

 

Char ch = fgetc (fp );

 

2) Reading and Writing string data

 

Write:

Fputs (array name/pointer name, fp); // fputs (array name, stdout );

 

Read:

Fgets (array name/pointer name, length, fp );

 

The actual number of characters read by fgets is-1.

 

1) The end of \ n and the end of EOF

 

3) read and write data blocks

 

Write:

 

Fwrite (write variable address, data block size, number of blocks, fp );

 

Int a = 123;

Fwrite (& a, sizeof (int), 1, fp );

 

Struct Student stu1;

Fwrite (& stu1, sizeof (struct Student), 1, fp );

 

Read:

Struct Student s1;

Fread (variable address, data block size, number of blocks, fp)

 

Fread (& s1, sizeof (struct Student), 1, fp );

// Use s1 for output

 

4) formatted read and write

 

Write Data and read data in a certain format

 

Write:

Fprintf (File pointer, "formatted string", variable list );

 

Fprintf (fp, "% d, % c, % d #", 10, 'A', 20 );

 

10, a, 20 #10, a, 20 #10, a, 20 #

 

Read

 

Fscanf (fp, "% d, % c, % d #", & a, & ch, & B );

 

Fprintf (fp, "% d, % s, % d", 20, "abc", 30 );

 

Fscanf (fp, "% d, % s, % d", & a, str, & B );

 

Str = abc, 30

 

Changed:

 

Fprintf (fp, "% d, % d, % s", 20, 20, "abc ");

Fscanf (fp, "% d, % d, % s", & a, & B, str );

 

 

5. Random File Reading

Set the position of the file's header pointer

 

The default fp is the first address pointing to the file.

 

1) rewind (fp); // fp points to the first address

 

2) fseek (fp, offset distance, starting point );

 

Fseek (fp, 32L, SEEK_SET); // offset 32 bytes from the file header

 

 

 

6. File Detection Functions

 

Feof () is used to check whether the file ends

 

If the end is not 0

To end 1

Disadvantage: One more execution

 

 

7. Introduction to other header files in C Language

 

Math. h

Stdio. h

Stdlib. h

Time. h

String. h

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.