0

So as the title says I'm trying to read from a file and save the characters into a 2d array with spaces but it gets stuck in the do while loop and last char of the file. any suggestions on how to fix this? i thought about using the eofbit but I couldn't figure it out then i tried exiting the do while loop by || it with eof that didnt work. it seems to just hang on the last letter of the file. thank you in advance for your advice

char ch[100][100];
int row_count =0;
int col_count = 0;
int col[100];
char temp; 
bool exit = false;
ifstream infile("data.txt");
if( infile.is_open())
{
    while(!infile.eof())
    {
        do
        {
            infile.get(temp);
            char[row_count][col_count] = temp;
            col_count++;

        }while(temp != '\n' || !infile.eof());
        col[row_count] = col_count;
        row_count++;
        col_count= 0;
    }
}

for(int i = 0; i <= 2; i++)
{
    for(int j=0; i <= col[i]; j++)
    {
        cout << ch[i][j];
    }
    cout << endl;
}

return 0;

}

  • Don't use while (!infile.eof()) see here: http://stackoverflow.com/questions/5605125/why-is-iostreameof-inside-a-loop-condition-considered-wrong Try while(infile.get(temp)) instead. Also why the two nested loops with similar conditions? just put an if statement – Khalil Khalaf Apr 07 '16 at 01:43
  • This would be considerably simpler with your stream's [`getline`](http://en.cppreference.com/w/cpp/io/basic_istream/getline) method – WhozCraig Apr 07 '16 at 01:50
  • Please rate and mark the answer that helped you the most. – Khalil Khalaf Apr 07 '16 at 12:12

3 Answers3

0

The loop is because of the temp != '\n', at the end of the file this condition is always true, so the second condition is never verified because you check it with a or. If you want to keep your code, bring the !infile.eof() at the first place.

But if you want a best way and an easy way to read a file. You should use getline.

std::string line;
while(std::getline (infile,line))
{
    //do something for every char in the string...
}
Captain Wise
  • 480
  • 3
  • 13
0

I didn't compile this but this should work: Read the comments and learn from it.

#include <iostream> // for cout
#include <fstream> // for infile     

int main()
{
    const int RowSize = 100, ColSize = 100;
    char ch[RowSize][ColSize];
    int row_count = 0;
    int col_count = 0;
    int col[ColSize];
    char temp;
    bool exit = false;

    std::ifstream infile;

    infile.open("data.txt"); // open the file

    if (!infile.is_open()) // check if opened succseffuly
    {
        std::cout << "File didn't load successfully!" << std::endl; // prompt the user
        getchar(); // wait for any input
        return 0; // terminate
    }

    while (infile.get(temp)) // while reading succesffuly
    {
        // if you are here, means you have valid data

        if (temp == '\n') // check if end of the line
        {
            col_count = 0; // reset cols
            row_count++; // increment rows
            ch[RowSize][ColSize] = NULL; // lock char array
            continue; // continue the loop iteration
        }
        else
        {
            ch[row_count][col_count] = temp; // store temp in your array
        }
        col_count++; // increment cols on every read
    }

    for (int i = 0; i <= 2; i++) // this will display junk if your line is less than 100 characters (check NULL if you don't want that)
    {
        for (int j = 0; i <= col[i]; j++)
        {
            std::cout << ch[i][j];
        }
        std::cout << std::endl;
    }

    return 0;
}
Khalil Khalaf
  • 8,448
  • 7
  • 47
  • 92
  • I thank you for your response and taking the time. It didn't compile but I was able to debug it to make it work for me. More importantly it helped me understand my mistakes and reminded me of proper techniques I was not using. Thank you. @FirstStep – Devil Graceland Apr 07 '16 at 05:00
  • @DevilGraceland glad that it helped :) could you please state why it did not compile? Also make sure to mark it as an answer if it answered your question – Khalil Khalaf Apr 07 '16 at 10:56
0

iostream::eof will only return true after reading the end of the stream. It does not indicate, that the next read will be the end of the stream. Ref this. So, the modified code would look like the following code snippet.

char ch[100][100];
int row_count =0;
int col_count = 0;
int col[100];
char temp; 
bool exit = false;
ifstream infile("data.txt");
if( infile.is_open())
{
    while(!(infile>>temp).eof())
    {

        ch[row_count][col_count] = temp;
        col_count++;

        if(temp == '\n'){
            col[row_count] = col_count;
            row_count++;
            col_count= 0;
        }
    }
}

for(int i = 0; i <= 2; i++)
{
    for(int j=0; i <= col[i]; j++)
    {
        cout << ch[i][j];
    }
    cout << endl;
}

return 0;

}
Community
  • 1
  • 1