关于文件输入流使用getline的问题。

伽利略与蛐蛐 2014-06-22 11:01:33
我要做的是一个从txt文件里按行提取数据到指定的结构体中,然后在对写好的结构体按规则输出,现在问题出现了输入数据的过程中,我定义了文件输入流ifstream的对象inFile,也关联好了文件。先在问题出现在了 n=inFile.getline();和(p+i)->name=inFile.getline();和(p+i)->money=inFile.getline(); 而p是我定义的一个指向结构体的指针,编译器的报错是
error C2661: “std::basic_istream<_Elem,_Traits>::getline”: 没有重载函数接受 0 个参数
1> with
1> [
1> _Elem=char,
1> _Traits=std::char_traits<char>
1> ]

按我的理解是是cin调用getline的用法一致才对,但是这样就不对。怎么回事,恳请大神帮忙。
下面是我写的完整代码:



#include "stdafx.h"


#include <iostream>
#include <fstream>
#include <cstdlib>

struct hello
{
char name[15];
double money;
};

int main()
{
using namespace std;

ifstream inFile;
char filename[15];
cout<<"Enter name of data file: ";
cin.getline(filename,15);
inFile.open(filename);
if(!inFile.is_open())
{
cout<<"Could not open the file"<<filename<<endl;
cout<<"Program terminating.\n";
if(inFile.fail())
cout<<"reason is failbit"<<endl;
else cout<<"reason is badbit"<<endl;
exit(EXIT_FAILURE);
}
int n;
n=inFile.getline(); //问题出现在这里,难道是我的getline用法用错了?
struct hello * p =new hello[n];
for(int i=0;i<n;i++)
{
(p+i)->name=inFile.getline(); //问题出现在这里,难道是我的getline用法用错了?
(p+i)->money=inFile.getline(); //问题出现在这里,难道是我的getline用法用错了?
}
int w,e;
w=0;e=0;
cout<<"Grand Patrons: "<<endl;
for(int i=0;i<n;i++)
{
if((p+i)->money>=10000)
{
cout<<(p+i)->name<<": "<<(p+i)->money<<endl;
++w;
}
}
if(w==0)
cout<<"None"<<endl;
cout<<"Patrons: "<<endl;
for(int i=0;i<n;i++)
{
if((p+i)->money<10000)
{
cout<<(p+i)->name<<": "<<(p+i)->money<<endl;
++e;
}
}
if(e==0)
cout<<"None"<<endl;
cout<<"Bye! "<<endl;
delete [] p;
inFile.close();
return 0;
}
...全文
189 1 打赏 收藏 转发到动态 举报
写回复
用AI写文章
1 条回复
切换为时间正序
请发表友善的回复…
发表回复
anshiny 2014-06-23
  • 打赏
  • 举报
回复
inFile.getline((p+i)->name, size);  

65,210

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

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