【问题标题】:How to read in integers, separated by commas and spaces, into a 2D array?如何将用逗号和空格分隔的整数读入二维数组?
【发布时间】:2020-02-16 00:49:17
【问题描述】:

我有一个如下所示的输入文件:

3, 2, 5
2, 4, 9
6, 5, 9

我已经创建了一个array[3][3]。

我的问题是:如何在跳过空格和逗号的同时将其读入我的数组?

另外:我不能使用stoi(没有 C++11)而且我还没有涵盖向量。

我尝试了下面的代码块,但结果数组全是零。

    string sNum;                     // Current number (as string)
    int num = 0;                     // Current number (as int)
    ifstream inFile;                 // Make input file stream
    inFile.open("Numbers.txt");      // Open my input file of 3x3 numbers

     while (getline(inFile, sNum, ',')) {                                 // Gets 'line' from infile ..
                                                                             // puts it in sNum .. 
                                                                             // delimiter is comma (the problem?).
        for (int rowCount=0; rowCount<3; rowCount++) {                    // Go through the rows
                for (int columnCount=0; columnCount<3; columnCount++) {   // Go through the columns

                        num = atoi(sNum.c_str());                         // String 'sNum' to int 'num'
                        array[rowCount][columnCount] = num;               // Put 'num' in array


                } // end columnCount for
        } // end rowCount for
    } // end while

我认为它是在空间中阅读。如何忽略获取整数之间的空格?

【问题讨论】:

  • 这能回答你的问题吗? Parsing a comma-delimited std::string
  • 阅读空格是您最少的问题。当显示的内部循环将二维数组中的 every 值设置为 same时,您认为会发生什么> sNum?显示的代码从文件中读取一个值(无论它是否正确读取它——它不是——不是重点),然后愉快地循环整个二维数组,将数组中的每个值设置为sNum。然后它读取文件中的下一个值(再次,不正确,但这仍然不是重点),然后它又开始了,将 2D 数组中的每个值都设置为它。这显然不是你想做的。
  • 类似的问题在此之前已经被问过很多次了,甚至不认为人们在问这个问题之前会先浏览一下网络......
  • @cwbusacker 是的,我之前看过那个帖子,但我想我毕竟需要使用 vect
  • 既然您意识到了这个错误,这里还有一个错误:您忘记了计算机编程的黄金法则:您的计算机总是完全按照您的指示执行,而不是您希望它执行的操作。你告诉你的电脑:getline(inFile, sNum, ','),或者:“电脑,我命令你从inFile读到下一个逗号”。因此,您的计算机将完全做到这一点。具体来说,第三次执行此操作时,它会根据您的输入将以下内容准确地读入sNum:" 5\n2",或者:空格、字符“5”、换行符和字符“2”。这是您希望您的计算机执行的操作吗?

标签: c++ arrays windows fstream spaces


【解决方案1】:

这里有一个解决方案:

string sNum;                                                                                                                                                                     
int array[3][3];
int num = 0;                                                                                                                                                                        
    ifstream inFile;                                                                                                                                                                   
    stringstream ss;
    inFile.open("Numbers.txt");      // Open the input file of 3x3 numbers                                                                                                                                       
    int row = 0;
    int col = 0;
     while (getline(inFile, sNum)) {  // Gets entire line from inFile and stores in sNum                                                                                                             
        ss.str(sNum); //puts the line into a stringstream

        while(ss >> num)
        {
           if(col == 3) //handles our 2D array indexes.
                        //when col reaches 3, set it back to 0 and increment the row.
           {
              row++;
              col = 0;
           }

           array[row][col] = num;
           col++;

           //eliminate any commas and spaces between each number.
           while(ss.peek() == ',' || ss.peek() == ' ')
              ss.ignore();
        }
        ss.clear();
     } //        end while                                                                                                                                                                                      

}

【讨论】:

    猜你喜欢
    • 2021-06-05
    • 2023-03-15
    • 2012-12-29
    • 1970-01-01
    • 2013-01-16
    • 1970-01-01
    • 2019-05-15
    • 1970-01-01
    相关资源
    最近更新 更多