- fgetc函数:
- 功能:在文件中读取一个字符;
- 具体内容如下:
c
#include <stdio.h> //头文件
int fgetc(FILE *stream);
/*
参数:
stream 文件指针
返回值:
成功 返回读取到的字符
失败OR文件结束 返回EOF
*/
- fputc函数:
- 功能:在文件中写入一个字符;
- 具体内容如下:
c
#include <stdio.h> //头文件
int fputc(int c, FILE *stream);
/*
参数:
c 要操作的字符
stream 文件指针
返回值:
成功 返回写入的字符的ASCII码
失败 返回EOF
*/
- perror函数:
- 功能:错误码被重置后,直接打印错误信息;
- 具体内容如下:
c
#include <stdio.h> //头文件
void perror(const char *s);
/*
参数:
s 用户附加的信息
最终打印的结果为:
附加的信息和错误码对应的错误信息以及换行符
返回值: 无
*/
- 示例代码:
c
//使用 fgetc 和 fputc 函数实现文件拷贝的功能
#include <stdio.h>
int main(int argc, const char *argv[])
{
if(3 != argc)
{
printf("Usage : %s src_file dest_file\n",argv[0]);
return -1;
}
FILE *fp1 = fopen(argv[1],"r");
if(NULL == fp1)
{
perror("fopen error");
return -1;
}
FILE *fp2 = fopen(argv[2],"w");
if(NULL == fp1)
{
perror("fopen error");
return -1;
}
int ret = 0;
while(EOF != (ret = fgetc(fp1)))\
{
fputc(ret,fp2);
}
return 0;
}