【发布时间】:2014-04-09 22:43:03
【问题描述】:
无法将输入文件中的数据排序到指定数组中。输入文件包含一个字符串、双精度和整数。
- TN 54.5 7
- 肯塔基州 65.6 23
- PA 123.3 30
- 等
string、double、int 数据类型共有 14 行。数据将以与文件相同的格式打印到屏幕上。我已经能够使用一个“字符串数组”将数据打印到屏幕上,但是数据没有按照上面指定的顺序打印。我尝试过使用 getline() 并且通过研究看到很多人在谈论简单地使用“输入 >> 变量 [max];”对于每个单独的变量。这只会打印一个巨大的数字,而输入文件中也不包含数字 53。我觉得我让这变得比现在更难。我知道我的数组很小,无法读取所需的数据量(我计划修复)。不要求有人为我解决这个问题。只需要指出正确的方向。有没有更简单的方法将数据排序到所需的数组?
下面的代码是我用一个数组读取所有数据类型的代码。
#include <iostream>
#include <fstream>
using namespace std;
void readData(ifstream& input, string []);
int main()
{
string data[14];
char filename[256];
fstream input;
cout << "Enter file name: ";
cint >> filename;
input.open(filename);
if (input.fail())
{
cout << "opening file fail." << endl;
}
readData(input, data);
input.close();
return(0);
}
void readData(ifstream& input, string data[14])
{
int count;
count = 0;
while (count < 14 && !input.eof())
{
input >> data[count];
count << data[count] << endl;
count++;
}
}
【问题讨论】:
标签: c++ arrays input fstream parallel-arrays