请问这个程序错在哪里啊?

肖尧19 2002-11-05 03:29:28
#include <iostream.h>
#include <fstream>
#include <string>
using namespace std;
void main()
{
string filename;
cout<<"please input the filename"<<endl;
cin>>filename;
cout<<"now type the content of the file"<<endl;
ifstream infile("filename");
if(!infile)
{
cout<<"sorry,file not found"<<endl;
exit(-1);
}
string lineContent;
int lineNumber=1;
while(getline(infile,lineContent,'\n')
{
cout<<lineNumber<<" "<<lineContent<<endl;
lineNumber++;
}
}
...全文
58 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
muche 2002-11-05
  • 打赏
  • 举报
回复
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
void main()
{
string filename;
cout<<"please input the filename"<<endl;
cin>>filename;
cout<<"now type the content of the file"<<endl;
ifstream infile(filename.c_str());
if(!infile)
{
cout<<"sorry,file not found"<<endl;
exit(-1);
}
string lineContent;
int lineNumber=1;
while(getline(infile,lineContent))
{
cout<<lineNumber++
<<" "<<lineContent<<endl;
}
}
Yssss1980 2002-11-05
  • 打赏
  • 举报
回复
修改如下:

#include <iostream> //必须改成不带.h的头文件。(全部为STL)
#include <fstream>
#include <string>
using namespace std;
void main()
{
string filename;
cout<<"please input the filename"<<endl;
cin>>filename;
cout<<"now type the content of the file"<<endl;
ifstream infile(filename.c_str()); //不能是"filename"或filename
//必须将其转换到字符串
if(!infile)
{
cout<<"sorry,file not found"<<endl;
exit(-1);
}
string lineContent;
int lineNumber=1;
while(getline(infile,lineContent,'\n'))
{
cout<<lineNumber<<" "<<lineContent<<endl;
lineNumber++;
}
}
Yssss1980 2002-11-05
  • 打赏
  • 举报
回复
修改如下:

#include <iostream> //必须改成不带.h的头文件。(全部为STL)
#include <fstream>
#include <string>
using namespace std;
void main()
{
string filename;
cout<<"please input the filename"<<endl;
cin>>filename;
cout<<"now type the content of the file"<<endl;
ifstream infile(filename.c_str()); //不能是"filename"或filename,必须将其转换到字符串
if(!infile)
{
cout<<"sorry,file not found"<<endl;
exit(-1);
}
string lineContent;
int lineNumber=1;
while(getline(infile,lineContent,'\n'))
{
cout<<lineNumber<<" "<<lineContent<<endl;
lineNumber++;
}
}
肖尧19 2002-11-05
  • 打赏
  • 举报
回复
fangrk(加把油,伙计!) 还是有错把,我用的VC6,你编译没有错误吗
sea_lover 2002-11-05
  • 打赏
  • 举报
回复
你是不是想将一个文件的内容输出到另一个文件里啊??
如果是:
#include <fstream.h>
#include <stdlib.h>
void main()
{
ifstream=inFile("in.txt")
ofstream=outFile("out.txt")
if!inFile || !outFile
{
cerr << "can't not open the file!" <<endl;
exit(1)
}
inFile >> filename;
outFile << filename;

}
fangrk 2002-11-05
  • 打赏
  • 举报
回复
#include <iostream.h>
#include <fstream>
#include <string>
using namespace std;
void main()
{
string filename;
cout<<"please input the filename"<<endl;
cin>>filename;
cout<<"now type the content of the file"<<endl;
ifstream infile(filename.c_str());
if(!infile)
{
cout<<"sorry,file not found"<<endl;
exit(-1);
}
string lineContent;
int lineNumber=1;
while(getline(infile,lineContent))
{
cout<<lineNumber++
<<" "<<lineContent<<endl;
}
}
肖尧19 2002-11-05
  • 打赏
  • 举报
回复
不,我想将一个文件的内容输出
awanghero 2002-11-05
  • 打赏
  • 举报
回复
cout<<lineNumber<<" "<<lineContent<<endl;

你是不是想把打进的文字输入文件
infile<<lineNumber<<" "<<lineContent<<endl;

69,371

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧