1 基于文件指针的数据读写
基于文件指针的数据读写,通常为标准函数,在Windows与Linux下,均可以使用。
数据块读写
NAME fread, fwrite - binary stream input/output SYNOPSIS #include <stdio.h> size_t fread(void *ptr, size_t size, size_t nmemb, FILE *stream); size_t fwrite(const void *ptr, size_t size, size_t nmemb, FILE *stream);
DESCRIPTION The function fread() reads nmemb elements of data, each size bytes long, from the stream pointed to by stream, storing them at the loca- tion given by ptr. The function fwrite() writes nmemb elements of data, each size bytes long, to the stream pointed to by stream, obtaining them from the loca- tion given by ptr.
RETURN VALUE fread() and fwrite() return the number of items successfully read or written (i.e., not the number of characters). If an error occurs, or the end-of-file is reached, the return value is a short item count (or zero).
fread从文件流stream 中读取nmemb个元素,写到ptr指向的内存中,每个元素的大小为size个字节。
fwrite从ptr指向的内存中读取nmemb个元素,写到文件流stream中,每个元素size个字节。
所有的文件读写函数都从文件的当前读写点开始读写,读写完以后,当前读写点自动往后移动size*nmemb个字节。
字符串读写
SYNOPSIS
#include <stdio.h>
char *fgets(char *s, int size, FILE *stream); int fputs(const char *s, FILE *stream); char *gets(char *s); 等同于 fgets(const char *s, int size, stdin); int puts(const char *s); 等同于 fputs(const char *s, stdout);
DESCRIPTION fgets() reads in at most one less than size characters from stream and stores them into the buffer pointed to by s. Reading stops after an EOF or a newline. If a new- line is read, it is stored into the buffer. A '