/*
程序功能:将文件“搬运”到指定的位置
步骤:
*step1 定义工程内容
*step2 打开源文件,文件须存在,如不存在须手动创建
*step3 打开目标文件,如不存在则通过此程序创建
*step4 读取源文件内容
*step5 写入目标文件
*step6 关闭源文件和目标文件
*step7 操作结束
*/
#include
#include
#include
#include
#include
#include
/*step1 定义工程内容*/
#define READ_SIZE 1024
#define FROM_FILE "fromthis.txt"
#define TO_FILE "tohere.txt"
int main()
{
int fromthis,tohere;
unsigned char buffer[READ_SIZE];
int read_state;
/*step2 打开源文件,文件须存在,如不存在须手动创建*/
fromthis = open(FROM_FILE,O_RDONLY);
/*step3 打开目标文件,如不存在则通过此程序创建*/
tohere = open(TO_FILE,O_RDWR|O_CREAT,0777);
/*健壮性判断*/
if(fromthis < 0)
{
printf("Open file error");
exit(1);
}
/*step4 读取源文件内容*/
while((read_state = read(fromthis,buffer,sizeof(buffer)))>0)
{
/*打印到控制台*/
printf("%s\n",buffer);
/*step5 写入目标文件*/
write(tohere,buffer,read_state);
}
/*step6 关闭源文件和目标文件*/
close(fromthis);
close(tohere);
/*step7 操作结束*/
return 0;
}
简单的IO操作示例(不带缓存方式)
最新推荐文章于 2025-08-01 15:28:39 发布