8th Lesson-Library function mode file programming

Source: Internet
Author: User
Tags fread

1. The C function library-based program is independent of the operating system and is more portable. Programming based on system calls relies on the kernel and is not well ported.
2. Flow: A data channel is created between the program and the source file to access the data in the file, so the data in the channel becomes an abstract dynamic stream.
File pointer: The file descriptor (integer) that corresponds to the system call. is a pointer to a file type, usually a variable that defines a file* FP that describes a pointer to a document that contains data information about the management flow information.
3. Function Learning:

3 . 1 File Open

Fopen (file pointer defaults to file header)
3.1.2 Function prototype
FILE * (const char *path, const char *mode);


3.1.3 Function functionfopen
opens a file in the specified mode from the specified path


3.1.4 Owning header file
<stdio.h>


3.1.5 return value
success: Returns the file pointer
failure: NULL


3.1.6 Parameter Description
Path:file name (with path)
Mode:How to open a file (two parameters are strings)
(1)R,RB-----------Read-only
(2)W,WB----------Write only
(3)a,ab----------Append, create if not present
(4)r+,r+b,rb+----reading and writing
(5)w+,w+b,wb+---Read-write, create if not present, empty content if it exists
(6)a+,a+b,ab+----Read and append, created if not present
3.2 Close File
3.2. 1 Name of function
Fclose
3.2.2 Function prototype
int fclose (FILE *fp);
3.2.3 Function function
Close File
3.2.4 Owning header file
<stdio.h>


3.2.5 return value
success: return 0
failure: EOF


3.2.6 Parameter Description
Fp: File pointer to a file to be closed


3.3 Read the file
3.3. 1 Name of function
Fread
3.3.2 Function prototype
size_t fread (void *ptr, size_t size, size_t nmemb, FILE *stream);


3.3.3 function function
reading data from a file
3.3.4 Owning header file
<stdio.h>
3.3.5 return value
success: Returns the amount of data read (in bytes)
failed: 0
3.3.6 Parameter Description
PTR: Point to the location where you want to save the data
Size: Determines the number of bytes per block
NMEMB: Determine the number of data blocks to read
Stream: Pointing to the file to be read


3.4 Write a file3.4. 1 Name of function
Fwrite3.4.2 Function prototype
size_t fwrite (const void *ptr, size_t size, size_t nmemb,file *stream);3.4.3 function function
writes data to a file that is stored in a specified location beforehand3.4.4 Owning header file
<stdio.h>3.4.5 return value
success: Returns the number of bytes successfully writtenfailed: 0

3.4.6 Parameter Description
PTR: Point to the location where you want to write dataSize: Determines the number of bytes per block
NMEMB: Determines the number of data blocks to writeStream: Pointing to the file to be written



3.5 Locating Files3.5. 1 Name of functionFseek
3.5.2 Function prototypeint fseek (FILE *stream, long offset, int whence);
3.5.3 function functionset the location pointer of the file (invisible pointer)
3.5.4 Owning header file<stdio.h>
3.5.5 return valuesuccess: return 0failed: -1
3.5.6 Parameter DescriptionStream: Point to the file where you want to move the pointerOffset: The Distance traveled (bytes)whence: Determines where the pointer starts moving as a datumseek_set: The file header is the base, at which point offset can only be an integerSeek_cur: With the current position as the benchmark, at this time the offset can be negative, positive move backward, negative numbers move forward. seek_end: Moves at the end of the file, at which point the offset can be negative.
3.6 int fgetc (file* fp)reads a character from the specified file and returns EOF when reading to the Terminator
3.7 int FPUTC (int count,file* fp)writes a character to the specified file (converts c to unsigned char type write)
3.8 fscanf (file* fp,char* format[,argument])format input from a stream (formatted read)
3.9fprintf (file* fp,char* Format[,argument])format output to a stream (formatted write)
3.10 char* getcwd (char* buf,size_t size)gets the current path, stores it in buf, if BUF is too small, returns-1
3.11 int mkdir (char* buf,int mode)
Created path, successfully returned 0, failed to return-1

8th Lesson-Library function mode 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.