我需要读取一个格式如下的数据文件
abcd(string) 1(int) 2(int) 3(int)
abcde(string) 4(int) 3(int) 2(int)
.
.
.
我想要执行一些只使用同一行变量的函数。但这是我的代码。我是一个初学者,请帮我进行修正,谢谢。
在.h文件中
#include <string>
using namespace std;
#ifndef CALC_H
#define CALC_H
class Calc
{
public:
void readFile(string file);
private:
string name;
int a;
int b;
int c;
};
#endif
在实现文件中
#include "Vehicle.h"
#include iostream>
#include fstream>
#include string>
#include cstdlib>
#include cmath>
using namespace std;
void Vehicle::readFile(string filename)
{
ifstream myIn;
int totalNum=0;
myIn.open(filename.c_str());
if (!myIn)
{
cerr<<"Data file failed to open!\n";
exit (0);
}
for (int i=0; i<MAX; i++)
{
while (myIn.peek() != EOF)
{
myIn>>calc[i].name;
myIn>>calc[i].a;
myIn>>calc[i].b;
myIn>>calc[i].c;
totalNum++;
}
}
myIN.close();
然后我想显示刚刚从文件中读取的内容
for (int i = 0; i < MAX; i++)
cout << calc[i].name << calc[i].a << calc[i].b << calc[i].c << endl;
抱歉,我漏掉了很多东西,我只想知道我是否走在正确的道路上。谢谢。
while (myIn >> calc[i].name >> calc[i].a >> calc[i].b >> calc[i].c) {totalNum++;}
。 - chris