C + + file operation--fopen/fseek/ftell/fread/fwrite/fclose and other function usage __jquery

Source: Internet
Author: User
Tags fread function prototype int size

1. fopen

FILE * fopen (const char * path,const char * mode); function Description

The parameter path string contains the file path and filename you want to open, and the parameter mode string represents the flow pattern.

Mode has the following morphological strings:

R to open a read-only file, the file must exist.

r+ open a file that can be read and write, the file must exist.

rb+ Read and write to open a binary file, allowing only read and write data.

rt+ read-Write opens a text file that allows reading and writing.

W Open Write-only file, if the file exists, the file length is 0, that is, the file content will disappear. If the file does not exist, the file is created.

w+ open a writable file, if the file exists, the file length is zero, that is, the file content will disappear. If the file does not exist, the file is created.

A opens the write-only file in an additional way. If the file does not exist, the file will be created, and if the file exists, the written data will be added to the end of the file, that is, the original contents of the file will be retained. (The EOF character is reserved)

A + opens the writable file in an additional way. If the file does not exist, the file will be created, and if the file exists, the written data will be added to the end of the file, that is, the original contents of the file will be retained. (the original EOF character is not retained)

WB write-only open or create a binary file;

wb+ Read and write to open or create a binary file that allows reading and writing.

wt+ Read and write to open or create a text file;

at+ read-Write opens a text file that allows you to read or append data at the end of the text.

ab+ read-write opens a binary file that allows you to read or append data at the end of a file.

The above morphological strings can be added to a B-character, such as RB, W+b or ab+, add a B-character to tell the function library to open the file as a binary file, not a plain text file. However, in the POSIX system, including Linux ignores the character. The new file created by fopen () will have a s_irusr| s_iwusr| s_irgrp| s_iwgrp| s_iroth| S_iwoth (0666) permission, this file permission will also refer to the Umask value.

Some c-compiler systems may not fully provide all of these functions, some C version without "r+", "w+", "A +", and with "RW", "WR", "AR" and so on, readers pay attention to the provisions of the system. return value

When the file is opened successfully, the file pointer to the stream is returned. Returns null if the file fails to open, and the error code exists in errno. Additional Instructions

Generally speaking, the file will be opened after some files read or write the action, if the file failed to open, the next reading and writing can not be smoothly, so in the fopen () after the error judgment and processing.

2. fseek

function reposition pointer on the file's internal location stream (data stream/file)

Note: Instead of locating the file pointer, the file pointer points to the file/stream. The position pointer points to the byte position inside the file, and as the file reads, the file pointer does not change to a different file if it is not assigned a value. usage

int fseek (FILE *stream, long offset, int fromwhere); Fromwhere--//seek_set opening -0,seek_cur Current position -1,seek_end End-2 description

function to set the location of the file pointer stream. If the execution succeeds, the stream points to the position of offset (pointer offset) in Fromwhere (offset start: file header 0, current position 1, File tail 2) as the base. If execution fails (for example, offset exceeds the size of the file itself), do not change the location where the stream points. return value

Succeeds, returns 0, otherwise returns another value.

Fseek position the file position (location) pointer (pointer) for the "file referenced by stream to" the byte location calculated by Offset.

3.ftell

Function Name: Ftell

Function: Returns the current file pointer

Function prototype: Long Ftell (FILE *stream);

function function: function Ftell () is used to get the number of offset bytes at the current position of the file position pointer relative to the top of the file. When accessing files randomly, the program is not easy to determine the current location of the file because of the frequent movement of the file location. Calling the function Ftell () makes it very easy to determine the current location of the file.

Invocation Example: Ftell (FP); using function Ftell () also makes it easy to know the length of a file. such as the following statement sequence: Fseek (FP, 0l,seek_end); Len =ftell (FP); First move the file's current position to the end of the file, and then call the function Ftell () to get the current position relative to the top of the file, which is equal to the number of bytes the file contains.

4.fread

Function: Read data from a stream

function prototypes: int fread (void *ptr, int size, int nitems, FILE *stream);

Parameters: Address (pointer) (PTR) used to receive data

Size of individual elements: bytes rather than bits, for example, reading an integer value is 4

Number of elements (Nitems)

File pointer to provide data (stream)

Return value: Number of elements successfully read

5. Fwrite

Write to a file read into a block usage

Fwrite (const void*buffer,size_t size,size_t count,file*stream);

(1) Buffer: is a pointer, for fwrite, is to output the address of the data.

(2) Size: The number of bytes to write;

(3) Count: The number of data items to be written to the size byte;

(4) Stream: Target file pointer.

Description: Where to write to the file. This is related to the open mode of the file, if it is r+, it starts with the address pointed to by file pointer, replaces the content, the length of the file is unchanged, and if it is a +, it is added from the end of the file, the length of the file is increased, and the fseek function has no effect on the function.

2).

Fwrite (File,string,length)

  

Parameters Describe
File Necessary. Specify the open file to write to.
String Necessary. A string that stipulates the file to be written to.
Length Optional. Specify the maximum number of bytes to write.
Description: Fwrite () writes the contents of the string to the file pointer files. If length is specified, when the length byte is written or a string is written, the write stops, depending on which case is first encountered.

Fwrite () returns the number of characters written and false when an error occurs.


6.fclose

Close a Stream

Usage:
int fclose (FILE *stream);


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.