【问题标题】:c++ reading data from input file into 3 parallel arrays based on data typec ++根据数据类型将输入文件中的数据读取到3个并行数组中
【发布时间】: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


    【解决方案1】:

    为什么不是一个循环? (见live here

    #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 22\nBB 3.48 48\nXX 2.03 172\n" };
      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";
    }
    

    【讨论】:

    • +1 优雅的代码。输入的格式是如此完美,以至于它可能意味着要像这样读取 =)
    【解决方案2】:
    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;
                }
        }
    

    【讨论】:

      猜你喜欢
      • 2021-12-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-09-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多