从文件读取输入并存储到2D数组中。

时间:2021-04-12 15:43:55

I need to read in a text file that contains only integers and each one is separated by new line. example would be:

我需要读取一个文本文件,它只包含整数,并且每一个都被新行分隔。的例子是:

0
1
2
...
64
repeating 0 to 64 64 times

Essentially the file is 64*64 lines long, containing an integer for each line.

本质上,该文件是64*64行,包含每个行的整数。

I need to store each integer (line) in ldisk, my 2D array, but am having serious problems doing so. I understand my code has an error because I am trying to store a string in a char, but I am not sure how to get around this. By the way, ldisk must be a 2-D array of chars. I would love some advice/feedback on my current code posted below, or an alternative solution. NOTE: I am a beginner at C++ PS: I know similar topics exist, but mine is more to the problem of getting around the type conversion or just converting it properly so I can store more than a single digit integer into my 2D array, because I have it working where I can store only the first digit where I want in my 2D array, but run into problems if there is more than 1 digit.

我需要在ldisk中存储每个整数(行),我的2D数组,但是这样做会有严重的问题。我理解我的代码有一个错误,因为我试图在char中存储一个字符串,但是我不知道如何处理它。顺便说一下,ldisk必须是一个2-D的字符数组。我喜欢下面的建议/反馈,或者是另一种解决方案。注意:我是一个初学者在c++ PS:我知道类似的主题存在,但我的是更多的问题在类型转换或者转换正确所以我可以存储超过一个单一的数字整数进我的二维数组,因为我有工作,我只能存储第一个数字,我希望在我的二维数组,但遇到问题如果有超过1位。

 int main(){
      char **ldisk;
      ldisk = new char*[64];
      for (int i = 0; i<64; i++)
        {
          ldisk[i]= new char[64];
        }
      int counter = 0;
      string line;
      ifstream inFile("example2.txt");
      while ( getline(inFile, line))
        {
          int first, second;
          first = counter/64;
          second = counter%64;
          cout << line;
          ldisk[first][second]= line;
        }
      return 0;
    }

EDIT: My apologies I have no idea how to do a table.

编辑:抱歉,我不知道怎么做桌子。

I want ldisk[0][0] to be 0,
 then ldisk[0][1] to be 1,
 then ldisk[0][2] to be 2,
 etc,
 etc,
 then ldisk[0][63] to be 64

Eventually it will fill up such that ldisk[63][63] = 64

最终它会填满ldisk[63][63] = 64。

2 个解决方案

#1


2  

This is the problem:

这就是问题所在:

ldisk[first][second]= line;

The type of ldisk[first][second] is char. You are trying to assign a std::string to it.

磁盘的类型[第一个][第二个]是char。您正在尝试分配一个std::字符串。

You can make your life a lot simpler by using a std::vector<std::string>.

使用std::vector <:string> ,可以使您的生活更简单。

#include <iostream>
#include <fstream>
#include <vector>
#include <string>

using namespace std;

int main(){

    vector<string> ldisk;

    int counter = 0;
    string line;
    ifstream inFile("example2.txt");
    while ( getline(inFile, line))
    {
       cout << line;
       ldisk.push_back(line);
    }
    return 0;
}

Update

更新

If you must have char** ldisk, you can change main to:

如果您必须有char** ldisk,您可以将main改为:

int main()
{
   char **ldisk;
   ldisk = new char*[64];
   for (int i = 0; i<64; i++)
   {
      ldisk[i]= new char[64];
   }
   int counter = 0;
   string line;
   ifstream inFile("example2.txt");
   while ( getline(inFile, line) && counter < 64 )
   {
      cout << line << endl;
      if ( line.size() >= 64 )
      {
         cout << "Line is longer than 63 characters. Copying only 63 characters from it.\n";
         strncpy(ldisk[counter], line.c_str(), 63);
         ldisk[counter][63] = '\0';
      }
      else
      {
         strcpy(ldisk[counter], line.c_str());
      }
      ++counter;
   }

   return 0;
}

#2


0  

Change your loop to:

改变你的循环:

for (counter = 0; counter < 64*64; ++counter)
{
    int item;

    if ( !(inFile >> item) )
    {
         cerr << "File only contained " << counter << "items.\n";
         return 1;
    }

    if ( item < CHAR_MIN || item > CHAR_MAX )
    {
         cerr << "Item " << counter << " invalid value " << item << "\n";
         return 2;
    }

    ldisk[counter/64][counter%64] = item;
}

The missing ingredient is that you were not trying to convert the string in your file into an integer value. You may need #include <climits>.

缺少的部分是您没有尝试将文件中的字符串转换为整数值。您可能需要#include

#1


2  

This is the problem:

这就是问题所在:

ldisk[first][second]= line;

The type of ldisk[first][second] is char. You are trying to assign a std::string to it.

磁盘的类型[第一个][第二个]是char。您正在尝试分配一个std::字符串。

You can make your life a lot simpler by using a std::vector<std::string>.

使用std::vector <:string> ,可以使您的生活更简单。

#include <iostream>
#include <fstream>
#include <vector>
#include <string>

using namespace std;

int main(){

    vector<string> ldisk;

    int counter = 0;
    string line;
    ifstream inFile("example2.txt");
    while ( getline(inFile, line))
    {
       cout << line;
       ldisk.push_back(line);
    }
    return 0;
}

Update

更新

If you must have char** ldisk, you can change main to:

如果您必须有char** ldisk,您可以将main改为:

int main()
{
   char **ldisk;
   ldisk = new char*[64];
   for (int i = 0; i<64; i++)
   {
      ldisk[i]= new char[64];
   }
   int counter = 0;
   string line;
   ifstream inFile("example2.txt");
   while ( getline(inFile, line) && counter < 64 )
   {
      cout << line << endl;
      if ( line.size() >= 64 )
      {
         cout << "Line is longer than 63 characters. Copying only 63 characters from it.\n";
         strncpy(ldisk[counter], line.c_str(), 63);
         ldisk[counter][63] = '\0';
      }
      else
      {
         strcpy(ldisk[counter], line.c_str());
      }
      ++counter;
   }

   return 0;
}

#2


0  

Change your loop to:

改变你的循环:

for (counter = 0; counter < 64*64; ++counter)
{
    int item;

    if ( !(inFile >> item) )
    {
         cerr << "File only contained " << counter << "items.\n";
         return 1;
    }

    if ( item < CHAR_MIN || item > CHAR_MAX )
    {
         cerr << "Item " << counter << " invalid value " << item << "\n";
         return 2;
    }

    ldisk[counter/64][counter%64] = item;
}

The missing ingredient is that you were not trying to convert the string in your file into an integer value. You may need #include <climits>.

缺少的部分是您没有尝试将文件中的字符串转换为整数值。您可能需要#include