C + + file operations (i): File pointer __c++

Source: Internet
Author: User
Tags int size rewind stdin

The language file system is called a stream file (stream), text stream (body file), binary stream (binary file) buffer and non-buffered file sequence operation file and random operation file sequence file: Read/write the K block before reading/writing the 1th to K-1 data blocks; random file: Direct read/ Write the k block; the action of the body file is usually the sequential file; the binary files are all random files.

A general process of file operation

Definition file Pointer files *

Open File fopen

Read and write to a file

The system has defined the data structure which is related to the file operation all in stdio.h file structure body

FILE *FR,*FP,*FW;

file* pointer as a file handle, is the unique identification of file access, it is created by the fopen function, fopen Open the file succeeds, return a valid file* pointer, otherwise return NULL standard file pointer

FILE *stdin,*stdout,*stderr,

StdIn refers to keyboard input

STDOUT Finger Monitor

STDERR refers to the error output device, also refers to the monitor

These variables have been successfully initialized and can be used directly.

Three, commonly used operation function fopen

Format: File *fopen (filename string, open mode string)

Example: FILE *fr; Fr=fopen ("C://user//abc.txt", "R");

String manipulation:

1 "R" or "RT": The body file is read-only open. File does not exist, open failed (sequential read) "W" or "WT": The body file is write-only open. If the file does not exist, then create the file, if the file exists, delete the contents of the file, rebuild the empty file (sequential write); (intercept file length 0)

2 "A" or "at": Body file Add way. File does not exist, create file (sequentially add write)

3 "r+" or "rt++": the text file read and write open, the file does not exist, then open failed (sequential read/write, random read/write, including rewrite and add);

4 "w+" or "w++": Text file read and write mode open, file does not exist, then create a file; otherwise intercept file length is 0 (sequential read/write, random read/write, write content can be read or rewritten or added)

5) ~b: text file → binary file

sequential read → sequential/random Read ("RB")

Eg: "R" or "RT" → "RB"

Sequential write ("WB")

Add write Order ("AB")

Go with ..., with .... ("rb+")

Shun ..., add ("wb+")

Shun ..., add ("ab+")

The use of fopen, whenever the open way with r letters, must be judged, whether the file is open successfully, otherwise the program will not report errors, will run down.

such as: FILE *FR;

Fr=fopen ("Abc.txt", "R");

if (fr==null) {

printf ("File not open!/n");

Return /* or exit (1);

File shutdown

Fclose (FILE *FP)

Generally, fclose (FP) and fopen should be paired with, especially if the file contains a write method, if not closed will result in loss of file data.

Fcloseall (void): Closes all currently open files. One-byte input function

Can be applied to binary and body file operations

int fgetc (FILE *fp)

int FPUTC (char ch. FILE *FP)

FGETC return values are positive (0~255)

File has no readable byte returns-1 (EOF)

The difference between a body file and a binary file read and write:

The body file reads 13 10 o'clock, automatically skips 13, reads 10, and returns; the body file is written to 10 o'clock, first automatically written 13 and then 10. File pointer and file pointer operation function

The file pointer is a data pointer inside the file operating system data structure, which is used to label the current read and write location of the file, the C language, the file pointer in bytes, the file first byte position number is 0, if the file length is n bytes, then the last byte position number is N-1, A file with a length of N bytes valid read and write range is 0~n-1. The pointer position is read/write outside of this, then fails; read-write function returns-1 (EOF);

The C language file pointer takes a long value;

When a file read/write operation is performed, the file pointer automatically moves backwards, pointing to the new pending read/write position.

File pointer move function

Rewind (FILE *FP)

File pointer reset to 0

Fseek (FILE *fp,long off,int POS)

Starts at the POS position and moves off bytes.

pos:0 file Start

1 file Current pointer position

2 End of file (file length n, pointer position is N)

Example: Fseek (fp,0l,0); ←→rewind (FP)

Fseek (fp,-1l,2); /* Move pointer to last byte * *

Fseek (fp,-2l,1); /* Move the pointer to the top two positions in the current position * *

Long Ftell (FILE *FP)

Find the current pointer position

Example: ask for file length

Fseek (fp,ol,2);

Len=ftell (FP);

Len is the file length

The characteristics of the file pointer: it can move arbitrarily in "negative infinity" to "positive infinity"; Read fails to read outside 0~n-1, the feof function is true when the read fails, and writes from N position to add; starting from the 0~n position can also be, its behavior does not have to master, because almost useless; When you first open the file, the Ftell function returns a value of 0, a file containing a method, as long as a write (first write), the file pointer automatically moved to n position.

int feof (FILE *fp)

If the file read fails, it returns a value other than 0, otherwise it returns a value of 0 and is used only for reading to the end of the file.

Any fseek operation makes feof false, even if the file pointer is outside the 0~n-1. Body file read/write function

FSCANF (FP ...)

fprintf (FP ...)

Among them, ... Exactly the same as scanf and printf usage.

scanf (...) ←→FSCANF (stdin ...)

printf (...) ←→fprintf (stdout ...) binary file read/write function, i.e. byte block function

int fread (char *buf,int size,int count,file *FP)

int fwrite (char *buf,int&nbtp;size,int count,file *FP)

Reads the size x count bytes from the file to the memory block BUF, writes the size x count bytes to the file from the memory block BUF, and returns the number of bytes actually read/written.

Related Article

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.