0

I'm trying to read text from a file into a char's matrix

I do it this way:

 char** crearMundo() 
 {
     ifstream input("C:\\Users\\JhonAlx\\Desktop\\file.txt");

     input >> filas;
     input >> columnas;

     filas += 2;
     columnas += 2;

     char** laberinto = crearMatriz(filas, columnas);

     //Initial fill
     for(int i = 0; i < filas; i++)
     {
         for(int j = 0; j < columnas; j++)
         {
             laberinto[i][j] = ' ';
         }
     }

     //Next two loops will fill only borders
     for(int i = 0; i < filas; i++)
     {
         laberinto[0][i] = '?';
         laberinto[filas - 1][i] = '?';
     }

     for(int i = 0; i < columnas; i++)
     {
         laberinto[i][0] = '?'; //VS throws error in this line
         laberinto[i][columnas - 1] = '?';
     }

     //Fill actual content of file, omitting borders
     for(int i = 1; i < filas - 1; i++)
     {
         for(int j = 1; j < columnas - 1; j++)
         {
             input >> laberinto[i][j];
         }
     }

     return laberinto; 
}

This morning when I programmed it, it was fine, but now it throws this error:

Access violation reading location 0xFDFDFDFD

Debugging with VS2012 and looking with Locals explorer, I get this text on the ifstream variable:

input{_Filebuffer={_Set_eback=0xcccccccc < Error reading characters of string.> _Set_egptr=0xcccccccc < Error reading characters of string.> ...} } std::basic_ifstream >

Any help will be apreciated.

2 Answers 2

0

In the first and last set of loops (the doubly-nested ones), your index limits are set up with respect to filas then columnas. However, the middle two (that set the borders) are different and probably wrong. Note the indices used by your 4 successive loops:

laberinto[0..filas-1][0..columnas-1]    -- fill with ' ' (ok: filas, then columnas)
laberinto[0,filas-1][0..filas-1]        -- first borders (bad: filas, then filas)
laberinto[0..columnas-1][0,columnas-1]  -- second borders(bad: columnas, then columnas)
laberinto[0..filas-2][0..columnas-2]    -- read from file(ok: filas, then columnas)

The likely cause of your error is overrunning your array bounds due to using the wrong index in the border loops; the likely fix is to correct that.

Sign up to request clarification or add additional context in comments.

3 Comments

Mmmm, I don't get why second loops are bad, would you mind if you explain a little more, please? Though I just tested my code with some fixed values for filas and columnas and I get the error again :(
Just fixed it with for(int i = 0; i < columnas - 1; i++) in the second loop, but still I don't really understand why. However, I'm marking your answer as accepted. P.S.: Again, would you mind if you explain me why, please?
It looks like the first index should be in 0..filas-1, while the second index should be in 0..columnas-1. However, in the second loop, the second index ranges from 0..filas-1 instead, so if filas>columnas, it should get an out of range error. Also, in the third loop, the first index ranges from 0..columnas-1, so if columnas>filas, it should also get an out of range error.
0

Alternative method - read the file into a string, and then use c_str() to get the char array. Example:

std::ifstream in("content.txt");
std::string contents((std::istreambuf_iterator<char>(in)), std::istreambuf_iterator<char>());
contents.c_str() // The char array

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.