1

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

 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

6
  • 1
    Is the blank line between each line containing a single number intentional ? if not I'll remove them. Separate note: there is zero-reason to use hand-rolled dynamic allocation for this. you will be likely shocked how little code is needed if the desired outcome is what I think it is. Commented Apr 25, 2014 at 18:41
  • It's accidental. For some reason I was unable to do just one on each line. Sorry, I'm new to stackoverflow Commented Apr 25, 2014 at 18:41
  • Ok. is that closer to what it should be? Hope so. Commented Apr 25, 2014 at 18:42
  • Great. Now, given the input you have above (which I assume is the tip of the iceberg) what exactly to you expect your 2D array to look like if properly formed? update the question with such a sample result, in table-form please. Doing so brings much clarity to understanding where your code goes afoul and what it should be doing instead. Commented Apr 25, 2014 at 20:16
  • Hi, sorry I don't know how to do tables, but I tried explaining as best as I could Commented Apr 25, 2014 at 21:05

2 Answers 2

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.

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;
}
Sign up to request clarification or add additional context in comments.

4 Comments

Thanks, but ldisk must be char
This gave me a segmentation fault. When I modified it a bit I was only getting the last integer in the textfile but it was being stored in the entire 2D array.
@user3529578 I am not sure why you are getting segmentation. Perhaps you are reading more than 64 lines of text. I am going to update the code to check for the number of lines read so you don't accidentally read more than 64 lines of text.
@R Sahu, add a check that strcpy does not overflow buffer.
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>.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.