急问,怎样从文件读数据

sunyuanzeng 2003-10-28 10:36:22
void main()
{
char a[100];

ifstream inPut("sun.txt");

inPut.getline(a,10,'\n');

for(int i=0;i<10;i++)
cout<<a[i]<<endl;
}

从文件中读入十个字符,为什么输出10个'?'啊,我晕
...全文
24 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
Andy84920 2003-10-28
  • 打赏
  • 举报
回复
看看你的文件中有什么内容!
CoolKin9 2003-10-28
  • 打赏
  • 举报
回复
你的sun.txt中有非法字符,
打开写字板,新建文本文件,
保存为xxx.txt,重新执行以上程序,
把sun.txt改为xxx.txt。
sunyuanzeng 2003-10-28
  • 打赏
  • 举报
回复
大哥,我怎么看不懂啊,能不能简单点,我只想知道我的程序哪里错了
Andy84920 2003-10-28
  • 打赏
  • 举报
回复
要看你的样子的?
Andy84920 2003-10-28
  • 打赏
  • 举报
回复
//

#include <fstream>
#include <string>
#include <vector>
#include <algorithm>
#include <iostream>
using namespace std;

int main()
{
string ifile;

cout << "Please enter file to sort: ";
cin >> ifile;

// constructor a ifstream input file object
ifstream infile( ifile.c_str());
if( !infile)
{
cerr << "error: unable to open input file: "
<< ifile << endl;
return -1;
}
string ofile = ifile + ".sort";
// constructor a ofstream ouput file object
ofstream outfile( ofile.c_str());
if( !outfile)
{
cerr << "error: unable to open output file: "
<< ofile << endl;
return -2;
}
string buffer;
vector< string> text;
int cnt = 1;
while( infile >> buffer )
{
text.push_back(buffer);
cout << buffer << (cnt++ % 8 ? " " : "\n");
}
sort( text.begin(), text.end());
// ok: print sorted words to outfile
vector< string>::iterator iter = text.begin();
for( cnt = 1; iter != text.end(); ++iter, ++cnt)
outfile << *iter
<< (cnt % 8 ? " " : "\n");

return 0;
}
sunyuanzeng 2003-10-28
  • 打赏
  • 举报
回复
谢谢各位

69,371

社区成员

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

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