标准 I/O – 按对象读写
下列函数用来从流中读写若干个对象:
#include <stdio.h>
size_t fread(void *ptr, size_t size, size_t n, FILE *fp);
size_t fwrite(const void *ptr, size_t size, size_t n, FILE *fp);
成功返回读写的对象个数;出错时返回EOF;既可以读写文本文件,也可以读写数据文件;效率高.
所需头文件 | #include <stdio.h> |
---|---|
函数原型 | size_t fread(void *ptr, size_t size, size_t nmemb, FILE *fp); |
函数参数 | ptr : 存放读入记录的缓冲区 ; size: 读取的每个记录的大小; nmemb: 读取的记录数; stream: 要读取的文件流 |
函数返回值 | 成功: 返回实际读取到的 nmemb的数目 ; 失败:EOF |
所需头文件 | #include <stdio.h> |
---|---|
函数原型 | size_t fwrite(void *ptr, size_t size, size_t nmemb, FILE *fp); |
函数参数 | ptr : 存放写入记录的缓冲区 ; size: 写入的每个记录的大小; nmemb: 写入的记录数; stream: 要写入的文件流 |
函数返回值 | 成功: 返回实际写入的 nmemb的数目 ; 失败:EOF |
利用fread / fwrite实现文件的复制
通过命令行参数传递源文件和目标文件名
通过fread返回值判断是否读到文件末尾
算法思路:
检查参数->打开源文件->打开目标文件->循环读写文件->关闭文件
#include <stdio.h>
#include <unistd.h>
#include <time.h>
#include <string.h>
#define N 64
int main(int argc, const char *argv[])
{
FILE *fp_R, *fp_W;
char buf[N];
int n;
if(argc < 3)
{
printf("Input Error: %s <src--File> <dest--File>",argv[0]);
return -1;
}
if((fp_R = fopen(argv[1],"r")) == NULL)
{
perror("fopen src file");
return -1;
}
if((fp_W = fopen(argv[2],"w")) == NULL)
{
perror("fopen dest file");
return -1;
}
while((n=fread(buf, sizeof(char), N, fp_R)) > 0 )
{
fwrite(buf, sizeof(char), n, fp_W);
}
fclose(fp_R);
fclose(fp_W);
return 0;
}
标准 I/O – 刷新流
#include <stdio.h>
int fflush(FILE *fp);
成功时返回0;出错时返回EOF
将流缓冲区中的数据写入实际的文件
Linux下只能刷新输出缓冲区
定义:
int fflush(FILE* stream);
表头文件:
加粗样式
#include<stdio.h>
说明:
fflush()会强迫将缓冲区内的数据写回参数stream指定的文件中。如果参数stream为NULL, fflush()会将所有打开的文件数据更新。
返回值:
成功返回0, 失败返回EOF, 错误代码存于errno中。
定位流 – ftell/ / fseek /rewind
#include <stdio.h>
long ftell(FILE *stream);
long fseek(FILE *stream, long offset, int whence);
void rewind(FILE *stream);
ftell() 成功时返回流的当前读写位置,出错时返回EOF
fseek()定位一个流,成功时返回0,出错时返回EOF
whence参数:SEEK_SET/SEEK_CUR/SEEK_END
offset参数:偏移量,可正可负
rewind()将流定位到文件开始位置
读写流时,当前读写位置自动后移
所需头文件 | #include <stdio.h> |
---|---|
函数原型 | long ftell(FILE *stream); |
函数参数 | stream: 要定位的文件流; |
函数返回值 | 成功: 返回当前读写位置 ; 失败:EOF |
/-----------------------------------------------------------------------------------------------------------------------------------------------------------------------/
所需头文件 | #include <stdio.h> |
---|---|
函数原型 | int fseek(FILE * stream,long offset,int whence);; |
函数参数 | stream: 要定位的文件流; offset: 相对于基准值的偏移量; |
whence:基准值 ; SEEK_SET (代表文件起始位置) SEEK_END(文件结束位置) SEEK_CUR(文件的当前位置) | |
函数返回值 | 成功: 0 ; 失败:EOF |
下列是较特别的使用方式:
- 欲将读写位置移动到文件开头时:fseek(FILE *stream,0,SEEK_SET);
- 欲将读写位置移动到文件尾时:fseek(FILE *stream,0,0SEEK_END);
返回值:
当调用成功时则返回0, 若有错误则返回-1, errno会存放错误代码。
定义:
void rewind(FILE * stream);
表头文件:
#include<stdio.h>
说明:
rewind()用来把文件流的读写位置移至文件开头。参数stream为已打开的文件指针。此函数相当于调用fseek(stream,0,SEEK_SET)。
附加说明:
fseek()不像lseek()会返回读写位置, 因此必须使用ftell()来取得目前读写的位置。
获取文件长度
#include <stdio.h>
#include <unistd.h>
#include <time.h>
#include <string.h>
int main(int argc, const char *argv[])
{
FILE *fp;
if ((fp = fopen("abc.txt", "r+")) == NULL)
{
perror("fopen");
return -1;
}
fseek(fp, 0, SEEK_END);
printf("length is %ld\n", ftell(fp));
return 0;
}
标准 I/O – 判断流是否出错和结束
#include <stdio.h>
int ferror(FILE *stream);
int feof(FILE *stream);
ferror()返回1表示流出错;否则返回0
feof()返回1表示文件已到末尾;否则返回0
定义:
int ferror(FILE *stream);
表头文件:
#include<stdio.h>
说明:
ferror()用来检查参数stream所指定的文件流是否发生了错误情况, 如有错误发生则返回非0值。
返回值:
如果文件流有错误发生则返回非0值。
定义:
int feof(FILE * stream);
表头文件:
#include<stdio.h>
说明:
feof()用来侦测是否读取到了文件尾, 尾数stream为fopen()所返回之文件指针。如果已到文件尾则返回非零值, 其他情况返回0。
返回值:
返回非零值代表已到达文件尾。
标准 I/O – 格式化输出
#include <stdio.h>
int printf(const char *fmt, …);
int fprintf(FILE *stream, const char *fmt, …);
int sprintf(char *buf, const char *fmt, …);
所需头文件 | #include <stdio.h> |
---|---|
函数原型 | int printf(const char *fmt, …); int fprintf(FILE *stream, const char *fmt, …);int sprintf(char *s, const char *fmt, …); |
函数参数 | fmt: 输出的格式 ; stream: 接收输出的流; buf: 接收输出的缓冲区; |
函数返回值 | 成功: 输出字符串(sprintf返回存入数组中的字符数); 失败:EOF |
成功时返回输出的字符个数;出错时返回EOF
每隔1秒向文件test.txt中写入当前系统时间,格式如下:
1, 2014-10-15 15:16:42
2, 2014-10-15 15:16:43
该程序无限循环,直到按Ctrl-C中断程序
每次执行程序时,系统时间追加到文件末尾,序号递增
time()用来获取系统时间(秒数)
localtime()将系统时间转换成本地时间
sleep()实现程序睡眠
算法思路:
打开文件->获取系统时间->写入文件->延时1S->返回第二步(获取系统时间)
#include <stdio.h>
#include <unistd.h>
#include <time.h>
#include <string.h>
//题目一:每隔1秒向文件test.txt中写如当前系统时间
#define N 64
int main(int argc, const char *argv[])
{
FILE *fp;
int line = 0;
char buf[N];
time_t Time;
struct tm *tp;
if((fp = fopen("abc.txt","a+")) == NULL)
{
perror("fopen");
return -1;
}
while(fgets(buf, N, fp) != NULL)
{
if(buf[strlen(buf)-1] == '\n')
{
line++;
}
}
while(1)
{
time(&Time);
tp = localtime(&Time);
fprintf(fp, "%02d, %d-%02d-%02d %02d:%02d:%02d\n", ++line,\
tp->tm_year+1900, tp->tm_mon+1,\
tp->tm_mday, tp->tm_hour,\
tp->tm_min, tp->tm_sec);
fflush(fp);
sleep(1);
}
return 0;
}