简体   繁体   English

需要帮助尝试在 C++ 中读取没有空格的文本文件到 2D 数组

[英]Need help in trying to read text file with no spaces to 2D array in C++

I'm trying to read a file with no spaces to an array.我正在尝试读取一个数组中没有空格的文件。 I have to input the file name, open the file, turn it into a 2D array, then print it out to a txt it later after editing it.我必须输入文件名,打开文件,将其转换为二维数组,然后在编辑后将其打印为 txt。 The array looks like this, although its number of rows and columns can change (with a max of 10x26):该数组看起来像这样,尽管它的行数和列数可以改变(最大为 10x26):

...A..B..C..
..ABC...AB..

And so on and so forth.等等等等。 What I've tried is:我试过的是:

string fileName;
   cout << "Enter the name of the input file: ";
   cin >> fileName;
   ifstream Scanner;
   Scanner.open(fileName);
   int fileArray[10][26];
   for(int rows = 0; rows < 10; rows++)
   {
      for(int seats = 0; seats < 26; seats++)
      {
         Scanner >> fileArray[rows][seats];
      }
   }

   for(int r = 0; r < 2; r++)
   {
      for(int c = 0; c < 5; c++)
    {
        cout << fileArray[r][c];
    }
   }

However, when I try to print the array it gives a bunch of numbers, not the characters I tried to place into it.但是,当我尝试打印数组时,它给出了一堆数字,而不是我试图放入其中的字符。 Any help would be appreciated.任何帮助,将不胜感激。 All i need is to get over this hurdle of putting the text file into a 2D array and then I think I can finish off the program.我所需要的只是克服将文本文件放入二维数组的障碍,然后我想我可以完成该程序。

The matrix is indeed a bunch of numbers because you declared it so.矩阵确实是一堆数字,因为你是这样声明的。

int fileArray[10][26]; this should be char fileArray[10][26]这应该是char fileArray[10][26]

OR或者

you could cast while printing like so你可以像这样打印时投射

std::cout << static_cast<char>(fileArray[r][j])

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM