简体   繁体   中英

Reading Input From File and Storing into 2D Array

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.

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, 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.

 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

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.

You can make your life a lot simpler by using a std::vector<std::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:

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;
}

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> .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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