关于文件输入流使用getline的问题。
我要做的是一个从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;
}