我有從該被格式化這樣讀文件到對象陣列C++
ABCD(字符串)1(INT)2(INT)3(INT)
ABCDE(字符串)4的數據文件讀取( int)3(int)2(int)
。
。
。
我想執行一些函數,只使用同一行中的變量。但這是我的代碼。我是初學者,所以請糾正我的謝謝。
在實現文件#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;
對不起,我離開了文件讀取.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
如果我走在正確的道路上,我只想知道很多東西。謝謝
不妨使用'while(myIn >> calc [i] .name >> calc [i] .a >> calc [i] .b >> calc [i] .c){ totalNum ++;}' – chris 2013-03-20 20:50:56