c++根据数据类型将输入文件中的数据读取到3个并行数组中

c++ reading data from input file into 3 parallel arrays based on data type

本文关键字:读取 数据 3个 数组 并行 c++ 数据类型 输入 文件      更新时间:2023-10-16

将输入文件中的数据排序到指定数组时遇到问题。输入文件包含一个字符串、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;
            }
    }