Lesson 9 - way file programming library functions
9.1 core theory
1. Library
Edit the file based on the C language library is independent of the specific operating system platform, whether it is windows, linux or other operating systems, we are using these functions. Use functional programming library functions can improve the portability function.
- flow
For the standard C library, and their operations are carried out around the stream. Stream is an abstract concept, when the program needs to read data, it will open a stream leading to a data source, the data source can be a file, memory, or network connection. Similarly, when the program needs to write data, it will open a stream leading to the destination. At this time you can imagine data like "flow" move as in this one.
- File pointer
File access system calls in the manner of using the file descriptor (an integer) to point to a file. File access library functions in the way of using FILE type to represent an open file, the type of information contained in the management file stream. While pointing to pieces, this type of information contained in the management file stream. And a pointer pointing to the type referred to were FILE * file pointer.
It is the same as the role of file descriptors.
9.2 Function Learning
Learning methods: three no learning.
First found in "UNIX Advanced Programming Environment" in the relevant command; see the usage information function into the team and prototyping through the man command; hands-on writing code.
- open a file
(1) function name
fopen
(2) function prototype
FILE *fopen(const char *path,const char *mode);
(3) function-
open a file
(4) belongs to the header file
#include<stdio.h>
(5) Return value
Success: file pointer
Failed: NULL
(6) Parameter Description
path: Specifies the open file name (including path)
mode: Open the file mode
There are six ways: r, w, a, r +, w +, a +
(7) sample program
#include<stdio.h>
void main()
{
FILE *fd;
fd = fopen ( "/ home / test.txt", "w +"); // w + represents a file can be created when the file does not exist.
}
- Close the file
(1) function name
fclose
(2) function prototype
int fclose(FILE *fp);
(3) function-
Close the file
(4) belongs to the header file
#include<stdio.h>
(5) Return value
Success: 0
Failed: EOF
(6) Parameter Description
fp: a pointer file to be closed
- Reading file
(1) function name
fread
(2) function prototype
size_t fread(void *ptr, size_t size, size_t nmemb, FILE *stream);
(3) function-
Reading data from a file
(4) belongs to the header file
#include<stdio.h>
(5) Return value
Success: Success of the amount of data to be read
Failed: 0
(6) Parameter Description
stream: to the file to be read
ptr: save the location data points we read
nmemb: we need to read data into blocks nmemb
size: the data has been read into nmemb blocks, the size of each block is size.
(7) sample program
Initially we use the following procedure, by changing the file under the home /, we want to open the file gives some data. However, we will not find out the print data.
#include<stdio.h>
void main()
{
FILE *fd;
char c_buf[15];
fd = fopen("/home/test.txt","w+");
fread(c_buf,1,10,fd);
printf("now we read %s\n",c_buf);
fclose(fd);
}
W + problems on this parameter. We look at this parameter descriptions:. Open for reading and writing The file is craet if it does not exist, otherwise it is truncated The stream is positioned at the beginning of the fie into the following program on it..:
#include<stdio.h>
void main()
{
FILE *fd;
char c_buf[15];
fd = fopen("/home/test.txt","r+");
fread(c_buf,1,10,fd);
c_buf [10] = '\ 0'; // add terminator, to prevent distortion when printing.
printf("now we read %s\n",c_buf);
fclose(fd);
}
- Write file
(1) function name
fwrite
(2) function prototype
size_t fwrite(const void *ptr, size_t size, size_t nmemb, FILE *stream);
(3) function-
Write data to a file
(4) belongs to the header file
#include<stdio.h>
(5) Return value
Success: the amount of data written to success
Failed: 0
(6) Parameter Description
stream: To write the file pointer to the data
ptr: store the data file to be written to
nmemb: we want to write the data into blocks nmemb
size: nmemb write data has been divided into blocks, the size of each block size.
(7) sample program
#include<stdio.h>
void main()
{
FILE *fd;
char *c_buf = "987654321";
fd = fopen("/home/test.txt","r+");
fwrite(c_buf,5,1,fd);
fclose(fd);
}
- File Positioning
Each open file has an invisible pointer.
(1) function name
fseek
(2) function prototype
int fseek(FILE *stream, long offset, int whence);
(3) function-
Set file pointer position.
(4) belongs to the header file
#include<stdio.h>
(5) Return value
Success: 0
Failure: -1
(6) Parameter Description
stream: file pointed
whence: represents the start position of the pointer, the following can take three values.
SEEK_SET the beginning of the file plus offset (positive)
SEEK_CUR the current pointer position plus offset (positive or negative) of
SEEK_END from the end of the file plus offset (negative)
offset: pointer to increase the number.
(7) sample program
#include<stdio.h>
void main()
{
FILE *fd;
char *c_buf = "987654321";
fd = fopen("/home/test.txt","r+");
fseek(fd,8,SEEK_SET);
fwrite(c_buf,5,1,fd);
fclose(fd);
}