您的位置:首页 > 编程语言 > C语言/C++

[C/C++] C/C++语言文件读写函数

2017-09-10 22:30 260 查看

学习自“知道蚂蚁” 点击打开链接
#include <stdlib.h>
#include <stdio.h>

using namespace std;

int main()
{
FILE *fpSrc, *fpDes;
fpSrc = fopen("execute.stdin", "r");   // 打开[没有则创建]文件读取数据
fpDes = fopen("destfile.stdin", "w");  // 打开文件写入数据
if (fpSrc == NULL || fpDes == NULL)
{
printf("文件打开错误!");
exit(-1); // 非正常退出
}

char cVal;
while ((cVal = fgetc(fpSrc)) != EOF)  // 判断是否读完文件
{
fputc(cVal, fpDes);				  // 将源文件中内容复制大目标文件
}

fclose(fpSrc);
fclose(fpDes); // fcloseall(); 关闭多个文件

system("pause");
return 0;
}
/*C++代码如下:*/#include <iostream>#include <fstream>using namespace std;int main(){ifstream ifs("execute.stdin");ofstream ofs("destfile.stdin");int iVal;while ( ifs >> iVal )  // 判断是否读完文件{ofs << iVal;				  // 将源文件中内容复制大目标文件}ifs.close();ofs.close();system("pause");return 0;}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: