【发布时间】:2016-10-20 05:03:56
【问题描述】:
我的代码如下:
string file_name;
int weight;
int distance;
char slash = '/';
string line = "";
ifstream myfile(file_name);
cout << "Enter the path of the file you want to read from. \n";
cin >> file_name;
ifstream inFile (file_name);
inFile.open(file_name.c_str());
if (inFile.is_open())
{
while (getline (inFile,line) )
{
inFile >> setw(7) >> weight >> setw(7) >> distance;
cout << "\n" << setw(4) << weight << setw(4) << distance;
}
inFile.close();
}
else cout << "Unable to open file";
我正在尝试遍历用户输入路径的文件的每一行。我正在使用的文件有数百行数据,我想遍历每一行并分离每个元素(每行具有相同的元素集)然后移动到下一行并做同样的事情。然而,没有任何东西被提取,也没有任何东西被计算出来。有没有人知道为什么这没有按预期运行?
【问题讨论】:
-
还有其他错误信息吗?你能粘贴你的数据样本吗?
-
@roelofs nope 没有其他错误消息
-
数据样本:2016/3/16 2016/3/23 f 581 3980 3 n n 15 Ken Jones x232@gmail.com
-
@roelofs 数据样本:2016/5/28 2016/11/3 s 248 17 3 ny 20 Katy Perry kperr@gmail.com 2016/2/13 2016/8/8 w 79 1123 2 ny 21 Betty White bwhite@gmail.com 2016/2/22 2016/4/14 f 641 162 2 nn 22 Earl Gray earlgrey@gmail.com
-
你为什么不用
std::strtok?可能会更简单。还有一件事,getline (inFile,line)和infile >>是矛盾的。你只需要其中一个。
标签: c++ text-files fstream