【发布时间】:2017-07-10 06:12:12
【问题描述】:
我有一个文本文件,我正在尝试使用 ifstream 从其中读取几行数据,然后我可以将它们添加到链接列表中。我正在使用老师提供给我的主要课程/功能,所以我没有亲自编写任何内容。我已经编写了一些其他的类来配合它,如果需要的话我可以提供,但是在调试时,一旦主函数开始尝试读取数据,问题就开始了。
#include <iostream>
#include <fstream>
#include "datalogger.h"
using namespace std;
int main(int argc, char** argv) {
datalogger dl;
if (argc != 2) {
cout << "Usage: " << argv[0] << " <datafile>" << endl;
exit(0);
}
// Read the data
char* datafile = argv[1];
ifstream infile(datafile);
int timestamp;
double temperature;
double windspeed;
while (!infile.eof()) {
infile >> timestamp;
infile >> temperature;
infile >> windspeed;
if (!infile.eof()) {
dl.addData(timestamp, temperature, windspeed);
}
}
// Output the report
dl.printReport();
return(0);
}
datafile(smallerdata.txt)的内容如下:
1480906168 -226 361
1480906168 -224 270
1480906175 -222 326
1480906179 -218 236
1480906187 -218 145
1480906189 -216 109
1480906189 -212 145
1480906190 -208 153
1480906197 -204 90
第一行的timestamp应该是1480906168,temperature和windspeed应该分别是-226和361。相反,我的调试器在第 24-27 行的断点处暂停时给了我这些值:
timestamp = 0
temperature = 2.1219889530967339e-314
windspeed = 3.184022971431627e-314
这些值到底是从哪里来的?为什么?
【问题讨论】:
-
您是否提供了有效路径作为参数。你能打印或检查
argv[1]的值吗? -
@moooeeeep 这是一个很好的观点。这个主要功能通常写得不好,但它仍然适用于示例数据。