Linux application development (5): C library function File Operations

Source: Internet
Author: User

I. chicken ribs

File Operations of C-library functions are independent of specific operating system platforms. These functions are used in DOS, windows, Linux, and VxWorks, therefore, the file operating platform based on library C functions is more independent and portable.

II. c file operations library functions


1. open the file: file * fopen (const char * filename, const char * mode );

Fileame: name of the opened file (including the file name, which defaults to the current path)

Mode: open mode (B is used to distinguish between binary files and text files, but does not distinguish between binary files and text files in Linux, so R and Rb in Linux have the same effect; in addition, in the C library function, the file descriptor is not represented by an integer like Linux, but by a pointer of the file struct type)

(1) R, Rb read-only mode

(2) W, WB open only in write mode. If the file does not exist, create the file first.

(3) A and AB are appended to open the file. If the file does not exist, create the file first.

(4) R +, R + B, RB +, read/write Mode Enabled

(5) W +, W + B, Wh +, read/write mode. If the file does not exist, create the file first.

(6) Open A +, A + B, AB +, read and append. If the file does not exist, create the file first.

2. File Read operations: size_t fread (void * PTR, size_t size, size_t N, file * file );

How to use:

(1) read N * size bytes (N records, the size of each record is size) from the file to the file, then place the data in the character array pointed to by PTR.

(2) the return value is the number of actually read fields n.

3. file write operations: size_t fwrite (const void * PTR, size_t size, size_t N, file * file );

How to use:

(1) read N * size bytes from the array pointed to by PTR, and write them to the file pointed to by file.

(2) the return value is the number of actually written fields n.

4. Read character operation: int fgetc (File * file );

How to use:

(1) read a character from the file pointing

(2) The returned value is the read character.

5. Write characters: int fputc (File * file );

How to use:

(1) Write a character into the file pointing

(2) the return value is the character that is successfully written.

6. format the Read File: int fscanf (File * file, char * format, [arguments]);

How to use:

(1) read a piece of data in format from the file to the arguments

(2) Return Value

7. Format The Write File: int fprintf (File * file, char * format, [arguments]);

How to use:

(1) write the arments parameter in format to the file pointing

(2) Return Value

8. Locate the file: int fseek (File * file, long offset, int whence );

Whence: datum point,

(1) seek_set, File Header

(2) seek_cur, current position of the file pointer

(3) seek_end, end of the file

9. Path acquisition function: char * getcwd (char * buffer, size_t size );

How to use:

(1) copy the current path to the buffer to which the buffer points. If the buffer is too small,-1 is returned.

(2) introduce the header file: # include <unistd. h>

10. Create a directory function: int mkdir (char * Dir, int mode );

How to use:

(1) create a new directory dir

(2) 0 indicates success, and-1 indicates an error.

(3) introduce the header file: # include <sys/STAT. 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.