c++根据数据类型将输入文件中的数据读取到3个并行数组中
c++ reading data from input file into 3 parallel arrays based on data type
将输入文件中的数据排序到指定数组时遇到问题。输入文件包含一个字符串、double和int。
- TN 54.5 7
- KY 65.6 23
- 第123.3页30
- 等等
有14行类型为string、double和int的数据类型。数据将以与文件相同的格式打印到屏幕上。我已经能够使用一个"字符串数组"将数据打印到屏幕上,但数据没有按照上面指定的顺序打印。我尝试过使用getline(),通过研究发现许多人都在谈论简单地使用"input>>variable[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++;
}
}
为什么不使用单个循环?(现场直播)
#include <iostream>
#include <sstream>
#include <vector>
void read_data(std::istream& input, std::vector<std::string>& strings, std::vector<double>& doubles, std::vector<int>& ints) {
std::string s; double d; int i;
while( input >> s >> d >> i ) {
strings.push_back(s);
doubles.push_back(d);
ints.push_back(i);
}
}
int main() {
std::istringstream i { "FN 3.2 22nBB 3.48 48nXX 2.03 172n" };
std::vector<std::string> strings;
std::vector<double> doubles;
std::vector<int> ints;
read_data(i, strings, doubles, ints);
std::cout << "strings:n";
for(auto s: strings) std::cout << " " << s << "n";
std::cout << "doubles:n";
for(auto d: doubles) std::cout << " " << d << "n";
std::cout << "ints:n";
for(auto i: ints) std::cout << " " << i << "n";
}
ifile.open(filename);
if (ifile.is_open()){
int indexValue = -1;
while ( ifile.getline(buffer, 100) ){
indexValue++;
counter = 0;
token = strtok(buffer, " ");
while (token){
if(counter == 0){
strcpy(stringArray[indexValue],token);
cout<< "string " <<stringArray[indexValue] << endl;
}
counter++;
stringstream ss;
int value =0;
if(counter == 1){
token = strtok(NULL, " ");
ss << token;
ss >> value;
intArray[indexValue] = value;
cout << "int " << intArray[indexValue] << endl;
}
}
counter++;
stringstream ss;
double value1 =0.0;
if(counter == 2){
token = strtok(NULL, " ");
ss << token;
ss >> value;
doubleArray[indexValue] = value1;
cout << "Score " << doubleArray[indexValue] << endl;
}
token = strtok(NULL, " ");
counter = 0;
}
}
相关文章:
- 如何在ros2中从rosbag2读取数据
- OpenCV 和从 OutputArrays 或 (Mat) 读取数据
- 使用C++从磁卡读卡器MT188读取数据
- HDF5 简单读取数据集失败
- 嵌套结构,从由空行分隔的文件中读取数据
- 不要从输入队列套接字读取数据
- 从文件中读取数据,操作员>>
- 如何从excel文件中读取数据并存储到变量中?
- C++:从文件中读取x y数据会产生无限循环吗
- 无法通过FT232RL读取数据
- 如何从QNetworkReply读取数据?
- C++ 逐行从文件(包含空格)读取数据
- 通过显示一行星号来创建条形图,该条形图将读取数据的输入文件
- 我们可以从 TXT 文件中读取数据并使用 C 和 C++ 将其保存到 SQL 数据库吗?
- 从多个 tcp 连接读取数据
- 从文本文件中读取数据并删除所有换行符空格,并在 C++ 控制台中显示
- Qt TCP服务器不从客户端读取数据
- 累积从 C++11 中的文件中读取数据的整数
- 从 txt 文件中读取数据的最简单方法
- 从 txt 文件中读取数据的 Getline