C++如何读取CSV文件并赋值到二维数组里

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了C++如何读取CSV文件并赋值到二维数组里相关的知识,希望对你有一定的参考价值。

参考技术A #include <string>#include <sstream>#include <iostream>int main() std::string str("123,56,34"); std::stringstream ss(str); while (std::getline(ss,str,',')) std::stringstream s(str); double num; s >> num; std::cout << num << std::endl;

你只要搞清楚 std::getline 就可以了

如何从文本文件中获取值并输入到二维数组中? C++

【中文标题】如何从文本文件中获取值并输入到二维数组中? C++【英文标题】:How do I take values from text file and input into a 2D array? c++ 【发布时间】:2017-09-17 04:28:44 【问题描述】:

输入:

Text File Values:  1 1 A 2 2 B 3 3 C 0 0 X

输出:

3 TILES on Scrabble Board

ROW COL LETTER
=== === ======
 1   1    A
 2   2    B
 3   3    C
=== === ======


4 x 4 SCRABBLE BOARD 


         1 2 3 4
       + - - - - +
row1=> | A       |
row2=> |   B     |
row3=> |     C   |
row4=> |         |
       + - - - - +
         1 2 3 4

(1) 将所有棋盘字符存入数组

char Board[9][9];   // Capacity is 9x9 = 81.

规则:一个板单元最多可以包含一个值; 在以下情况下显示错误消息: - 单元格已经包含一个标记 - 标记不是字母 - 该行超出范围 [1,boardsize] - col 超出范围 [1,boardsize]

ERROR:  REJECTED CELL <row> <col> <symbol> CELL MARKED
ERROR:  REJECTED CELL <row> <col> <symbol> NOT A LETTER
ERROR:  REJECTED CELL <row> <col> <symbol> BAD ROW
ERROR:  REJECTED CELL <row> <col> <symbol> BAD COL

(2) 读取输入文件后,显示 Board 数组。

(3) 在拼字板上显示文字(仅作图示):

HORIZONTAL: xxxx yyyyyyy zzzzz 3 WORDS
VERTICAL:   aaa bbb ccc ddd  4 WORDS
7 SCRABBLE WORDS

问题:如何将文件值例如:1 1 A 输入 char Board[9][9]?我希望 1 1 代表 row 和 col 并且符号 A 与 1 1 相关联。

例子:

Board[1][1] = A
Board[2][2] = B

新代码:

 int main()

   //-------------------------------------------- --------------------------
   //  Declare variables
   //----------------------------------------------------------------------
   char filename[80];
   int boardsize, row, col;
   char symbol;
   char Board[9][9];
   ifstream inF;

   //-| ----------------------------------------------------------------------
   //-| Print the copyright notice declaring authorship.
   //-| ----------------------------------------------------------------------
   cout << endl << "(c) 2017, twilson" << endl << endl; 


   //-| ----------------------------------------------------------------------
   //-| 1. Get file name and open file.
   //-| ---------------------------------------------------------------------- 
   cout << "Enter name of input file: "; 
   cin >> filename;
   cout << endl;

   inF.open(filename);
   if (inF.fail())
   
      cout << "FATAL ERROR: Can not open file " << "'" << filename << "'" << endl;
      exit(1);
   

   //-| ----------------------------------------------------------------------
   //-| 2. Get board size.
   //-| ----------------------------------------------------------------------
   cout << "Enter board size [1-9]: ";
   cin >> boardsize;
   cout << endl;

   //-| ----------------------------------------------------------------------
   //-3. Read in file values and output ROW, COL, and LETTER on scrabble board.
   //-| ----------------------------------------------------------------------
    int T = 0;
    int nextLine = 0;
    int Tiles = 0;


    // Read in file and count each tile

    int a = 0;
    while(inF >> row >> col >> symbol)
       
        if(row > 0 && col > 0)
        
            if( row == row && col == col)
            
                cout << "REJECTED CELL " << row << " " << col << " "
                     << symbol << " CELL MARKED" << endl;
            
            else if(!isalpha(symbol))
            
                cout << "REJECTED CELL " << row << " " << col << " "
                     << symbol << " NOT A LETTER" << endl;
            
            else if(row > boardsize)
            
                cout << "REJECTED CELL " << row << " " << col << " "
                     << symbol << " BAD ROW" << endl;
            
            else if(col > boardsize)
            
                cout << "REJECTED CELL " << row << " " << col << " "
                     << symbol << " BAD COL" << endl;
            

            else
                Tiles++;
        

        

【问题讨论】:

您阅读了int row, int col, char Val,验证行和列,检查单元格是否空闲,检查 Val 是一个字母。如果一切正常,您分配Board[row-1][col-1] = Val 到目前为止我的代码:while(!inF.eof()) inF >> row >> col >> symbol; if(row > 0 && col > 0) if(!isalpha(symbol)) cout boardsize) cout boardsize) cout 将您的代码放入您的问题中并阅读***.com/questions/5431941/… 或此***.com/a/5605159/8491726 谢谢。另外,我将代码添加到问题中并更改了 while 循环。 【参考方案1】:

在第 2 项和第 3 项之间,您必须初始化您的板(这样您以后可以检查该地点是否已标记

    for (int i = 0; i < boardsize; i++)
        for (int j = 0; j < boardsize; j++)
            Board[i][j] = ' ';

您的 while 循环应如下所示(实际错误打印已删除 - 只剩下条件)

    while (inF >> row >> col >> symbol)
    
        if(row == 0 && col == 0 && symbol == 'X')
            //end condition? - not clear from your explanation
            break;
        
        if (row < 1 || row >  boardsize)
        
            // BAD ROW error
        
        else if (col <1 || col > boardsize)
        
            // BAD COL error
        
        else if (!isalpha(symbol)) 
            //NOT A LETTER error
        
        else if (Board[row - 1][col - 1] != ' ')  //This check requires the Board to be properly initialized
        
            //CELL MARKED error
        
        else 
            //All is good
            Board[row - 1][col - 1] = symbol;
            Tiles++;
        
    

【讨论】:

非常感谢!

以上是关于C++如何读取CSV文件并赋值到二维数组里的主要内容,如果未能解决你的问题,请参考以下文章

C++二维数组给一维数组赋值

请问python中如何读取一个csv或者dat文件,并储存为一个二维数组?

delphi二维数组定义 赋值

c++ 用vector 定义二维数组

如何将 numpy 二维数组作为一种可以用 C++ 读取的二进制格式存储到磁盘上

Python遥感图像处理应用篇(十四):GDAL 读取多光谱数据为二维数组并存入csv文件