"Original" C language file programming

Source: Internet
Author: User
Tags fread

The concept of a file

1, Program Files: source program files (such as. c), target files (such as. obj), executables (such as. exe), and so on, the contents of these files are program code.

2, data file: For the program to read and write files when running.

A file is usually a collection of data stored on external media, and the operating system treats all devices as files uniformly.

The process of entering and outputting data, such as flowing water, flows from one place to another, so the input output is visually called a stream, or a stream of data. Flow represents the flow of information from the source to the destination.

Filename

Each file requires a unique label-the file ID, which is the file name we usually say. File identification (filename) consists of three parts: (1) file path, (2) file name backbone; (3) file suffix.

For example, this is a file name: E:\C\example \ file . C

1 2 3

1 is the file path, 2 is the filename trunk, and 3 is the file suffix.

Classification of files

Data files can be divided into (1) ASCII files, (2) binary files.

1. ascii File: Text file (file.txt);

2. Binary files: In-memory data output to external memory is a binary file without conversion. Because the data is also stored in binary form in memory, the binary file is also known as the image file.

File buffer system: The system automatically creates a file buffer in the memory area for each file being used in the program. Output data from memory to disk must first be sent to an in-memory buffer before being sent to disk with full buffers loaded.

Program data area, output file buffer, disk, input file buffer, and program data area

File pointers and file operations

1, Preparation: All the file operation functions and definition types are in "Stdlib.h"

2, the file pointer: Files *FP, that is, defines a pointer to the file, the operation of the file needs to rely on the file pointer to do

System-defined 3 file pointer variables: stdin/stdout/stderr

3. File Operation function

fopen ("filename", "Optinon")

Option Meaning If the specified file does not exist

R (Read only)

To enter data, open a text file Error

W (write only)

To output data, open a text file (empty all content before writing new data) New file

A (Append)

Add data to the end of a text file Error

RB (Read only)

To enter data, open a binary file Error

WB (write only)

To enter the data, create a new binary file New file

AB (Append)

Add data to the end of a binary file Error

r+ (Read and write)

To read and write, open a text file Error

w+ (Read and write)

To write the data first, after reading, create a new text file New file

A + (read and write)

To read and write, open a text file Error

rb+ (Read and write)

To read and write, open a binary file Error

wb+ (Read and write)

To write the data first, after reading, create a new binary file New file

ab+ (Read and write)

To read and write, open a binary file Error

When successfully opened, returns the file pointer address to the file, failing to null.

Fclose (FP)

You should close the program after you have finished using it, and if you do not close the file, it will usually result in data loss.

A successful shutdown returns 0, and a failure returns EOF (-1).

FGETC (FP)

Read a character. The read character is returned successfully, and the failure returns EOF (-1).

FPUTC (FP)

Write a character. Successfully returns the output character, and the failure returns EOF (-1).

Fgets (STR,N,FP)

Reads a string of length n into the character array str. The address of STR was successfully returned, and the failure returned null.

Fputs (STR,FP)

Writes the string in the STR array to the file that the FP points to. A successful return of 0 fails to return a value other than 0.

fprintf (FP, "string", output table column)

FSCANF (FP, "string", Input table column)

The printf/scanf is preceded by the FP to indicate the output file, the other is consistent with printf/scanf.

Fread (BUFFER,SIZE,COUNT,FP)

Fwrite (BUFFER,SIZE,COUNT,FP)

Buffer: The address of the store where the data is read from the file (Fread), and the data from the storage that starts in buffer to the file output (fwrite).

Size: The number of bytes to read and write.

Count: The number of data items to read and write, and the length of each data size.

Because both functions need to specify size, Fread/fwrite is usually used for input and output of binary files.

Feof (FP): detects whether the file location tag reaches the end of the file.

Rewind (FP): Resets the file location tag to the beginning of the file.

Fseek (FP, (number) L,0/1/2): (number) L represents the amount of displacement, in bytes, + forward,-backwards; 0: File start, 1: Current position, 2: End of file.

These three functions can change the location of the file tag, so it can be used to read and write random text.

This article is the reference rectification C program design fourth version of the content written out, if you need to reprint please indicate the source.

"original" C language file programming

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.