C language-style file manipulation functions

Source: Internet
Author: User

Using the C-style file manipulation function, you need to include the stdio.h header file.

1. function to open the file:

1 // The value of Oflag is "W" or "R", which means to open it in either write or read 2 file* fd = fopen (Filename.c_str (), oflag);

2. Function prototypes for writing files:

size_t fwrite (constvoid* buffer, size_t size, size_t count, file* Stream);

Where buffer is the first address pointer that needs to be written to the file in memory, size is the number of bytes per element to write to, count is how many elements to write, stream is the file pointer, and the example is as follows:

 1  FILE * FP;  2  fp = fopen ( filename  , "  WB   " )  int  a[3 ] = {1 , 2 , 3  };  4  fwrite (A, sizeof  (int ), 3  , FP);  5  fclose (FP); 

3. The prototype of a read file function is very similar to writing a file:

size_t fread (constvoid* buffer, size_t size, size_t count, file* Stream);

4. Functions to obtain file information:

1 struct stat statbuf; 2 Fstat (Fileno (FP), &statbuf);

The resulting statbuf is the structure that contains the file information, such as Statbuf.st_size is the size of the Get file

5. Move the file location pointer function prototype:

int fseek (FILE *fp,long offset,int origin);

The first parameter is the file pointer, the second argument moves the offset, and the third parameter moves from there, with 3 macro seek_set (0) representing the beginning of the document, Seek_cur (1) representing the file's current position, and seek_end (2) indicating the end of the file (not recommended for numbers, Preferably with a macro)

6. Do not forget to close the file oh:

Fclose (FP);

C language-style file manipulation functions

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.