//c 语言写法
#include<stdio.h>
#include<string,h>
int main()
{
char szTest[1000]={0};
int len=0;
FILE *fp=fopen("1.txt","r");
if (NULL == fp)
{
printf("fail to open dos.txt \n");
return 1;
}
while (!feof(fp))
{
memset(szTest,0,sizeof(szTest));
fgets(szTest,sizeof(szTest)-1,fp);//包含了\n
printf("%s",szTest);
}
fclose(fp);
printf("\n");
return 0;
}
//c++ 写法(读取并输出到另一个文件中)
#include <fstream>
#include <iostream>
#include <string>
using namespace std;
int main()
{
ifstream in("1.txt");
ofstream ou("2.txt");
string line;
if (in)
{
while (getline(in,line)) //line 中不包括每行的换行符
{
cout<<line<<endl;
ou<<line<<endl;
}
}
else //没有该文件
{
cout<<"no such file!"<<endl;
}
return 0;
}