如何一次读取一行数据文件并将整行放入变量中

时间:2016-10-30 17:57:44

标签: c++

我正在尝试使用c++读取每个行上具有不同数据类型的dat文件。地址,描述,平方英尺,卧室,浴室。

这是dat文件:

123 N Main Avenue, Albiny, NY 12206
This is the first house description.
1200
3
1.1
456 S Lake Street, Los Angeles, CA 90057
This is the second house description
1000
4
2.0

这是我的代码:

char address[255]     = { '\0' };
char description[255] = { '\0' };
int sqft              = 0;
int bedrooms          = 0;
float bathrooms       = 0.0;

ifstream dataFile;
dataFile.open(fileName);

while (dataFile >> address >> description >> sqft >> bedrooms >> bathrooms)
{
    House.setAddress(address);
    House.setDescription(description);
    House.setSqft(sqft);
    House.setBedrooms(bedrooms);
    House.setBathrooms(bathrooms);
    cout << "Debug tool: check to see if it made it in this while loop." << endl;
}

它没有读取文件。我的猜测是它试图将123(第一个地址的门牌号)放入address并且它不起作用,因为123是一个int而不是一个字符串。如何将每一行放入变量中以便完成此操作?

0 个答案:

没有答案