【问题标题】:How do I read from a file into a 2d array?如何从文件读入二维数组?
【发布时间】:2021-12-29 20:54:12
【问题描述】:

我正在开发一个与用户玩配对游戏的程序。代码可以编译,但是当我尝试运行它时,出现的表中有垃圾值。

我的输入文件如下所示:

//p4data1.dat
A
F
B
D
C
E
F
B
D
E
A
C

//p4data2.dat
F
B
D
C
A
F
E
A
C
D
B
E


我当前的代码是:

void fileAndRandom(char letArr[][COLUMN], int rows, int columns) {
  int random = rand() % 2 + 1;
  ifstream readFile;
  if(random == 1) {
    readFile.open("p4data1.dat");
    for(int i = 0; i < rows; i++) {
      for(int k = 0; k < columns; k++) {
        readFile >> letArr[i][k];
      }
    }
    }else if(random == 2) {
      readFile.open("p4data2.dat");
      for(int b = 0; b < rows; b++) {
        for(int a = 0; a < columns; a++) {
          readFile >> letArr[b][a];
        }
      }
    }
  readFile.close();
}

我不知道如何阅读它。我也不能使用向量,因为我还没有被教导这样做。

【问题讨论】:

  • 将 dat 文件读取为字节,然后对字节执行任何操作。 std::string 和 std::vector 是包含字节的好选择。
  • 每行末尾是“\n”或“\r\n”,应该忽略它。
  • 带有垃圾值的表格是什么样子的?
  • 实际上我意识到它不起作用的原因是因为我从未在 main 中调用过该函数 lol

标签: c++ arrays


【解决方案1】:

您应该使用std::vector 而不是数组,因为std::vector 是一个可变大小的容器,您并不总是知道 input.txt 文件包含多少个元素。完整的工作程序below 展示了如何读取文本文件并将该信息存储在2D std::vector

#include <iostream>
#include <vector>
#include <string>
#include <sstream>
#include<fstream>
int main() {
    std::string line, word;

    
    std::ifstream inFile("input.txt");
    
    //create/use a std::vector instead of builit in array 
    std::vector<std::vector<std::string>> vec;
    
    if(inFile)
    {
        while(getline(inFile, line, '\n'))        
        {
            //create a temporary vector that will contain all the columns
            std::vector<std::string> tempVec;
            
            
            std::istringstream ss(line);
            
            //read word by word 
            while(ss >> word)
            {
                //std::cout<<"word:"<<word<<std::endl;
                //add the word to the temporary vector 
                tempVec.push_back(word);
            
            }      
            
            //now all the words from the current line has been added to the temporary vector 
            vec.emplace_back(tempVec);
        }    
    }
    
    else 
    {
        std::cout<<"file cannot be opened"<<std::endl;
    }
    
    inFile.close();
    
    //lets check out the elements of the 2D vector so the we can confirm if it contains all the right elements(rows and columns)
    for(std::vector<std::string> &newvec: vec)
    {
        for(const std::string &elem: newvec)
        {
            std::cout<<elem<<" ";
        }
        std::cout<<std::endl;
    }
    
    /*another way to print out the elements of the 2D vector would be as below 
    for(int row = 0; row < vec.size(); ++row)
    {
        for(int col = 0; col < vec.at(row).size(); ++col)
        {
            std::cout<<vec.at(row).at(col)<<" ";
        }
        std::cout<<std::endl;
    }
    */
    
    return 0;
}

上面程序的输出可以看到here。在我的程序结束时,我打印出了 2d 矢量 的元素,以便我们可以确认它是否正确包含所有元素。

使用 std::vector 的优势是您不必事先知道输入文件中的行数和列数,也无需传递(或为) rowcolumn

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-12-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多