Hello I am looking to read a file into a 2d array to show which spaces have been taken/ haven't taken whiey will show a # if not taken and a T if taken, so far I just have a text file I created which shows like this:

T # # T
T T T #

All I am trying to do at the moment is to read this into a 2d array and display it as above, later on I will allow users to choose a position and take a space of the layout (changing a T to a #) and then I will want to save it to the file to make the changes.

But at the moment all I want to do is show it, this is what I have so far, I appologize if it's messy, I have made so many changes now to try and get it to work.

I hope someone can offer som guideance.

void Layout::layoutset(void)
    {   

ifstream myFile ("layout.txt", ios::in);
myFile.precision(2);
myFile.setf(ios::fixed, ios::showpoint);

myFile >> spaceLayout[2][5];
while (!myFile.eof())


myFile >> //Don't know what to put here
while (!myFile.eof())
{
for (int r = 0; r<2; r++)
{
for (int c = 0; c < 5; c++)
{
spaceLayout[r][c] = //?Don't know what to put here
}
}
}
myFile.close();
} 

Recommended Answers

All 6 Replies

Delete lines 5 and 6. There are no floating point values anywhere in this problem.

Line 8 - I assume you are trying to declare a 2 x 5 array? Shouldn't this be 2 x 4? That's what you have in your example. Change line 8 to this.

char spaceLayout[2][4];

Lines 9 and 13 - Having a while (!myFile.eof()) loop condition doesn't make sense to me here. You know your size. 2 x 4. You already have a nested for-loop. Use it. You know the size in advance, so don't bother with any end-of-file tests.

This should do..

for (int r = 0; r<2; r++)
{
    for (int c = 0; c < 4; c++)
    {
        myFile >> spaceLayout[r][c];
    }
}

Thanks for your help, I have ammended my code and nothing seems to be displaying at all, here is what I have now, I have now made the text file a direct directory just incase it was looking in a different location and still no luck.

My example was bad, it was meant to be:

T # # T #
T T T # T

(I obviously can't count)

Here is what I have now:

void Layout::layoutset(void)
    {    
ifstream myFile ("F:\MyApp\Debug\layout.txt", ios::in);
myFile >> spaceLayout[2][5];
{
for (int r = 0; r<2; r++)
{
for (int c = 0; c < 5; c++)
{
myFile >> spaceLayout[r][c];

}
}
myFile.close();
} 

} 

Oh and the text file is formatted exactly like the text file on two rows and with a space after each character, is this correct formatting?

Line 4 needs to be this...

char spaceLayout[2][5];

The rest of it looks OK. It won't display anything though. To display, after line 16, you can add this.

for(int r = 0; r < 2; r++)
{
    for(int c = 0; c < 5; c++)
    {
        cout << spaceLayout[r][c] << " ";
    }
    cout << "\n";
}

This still isn't showing the data it now shows something which looks like this
|[ |[ |[ |[ |[
|[ |[ |[ |[ |[

Here is what I have:

void Seats::layoutset(void)


    {    
ifstream myFile ("F:\MyApp\Debug\layout.txt", ios::in);
char spaceLayout [2] [5];   
{
for (int r = 0; r<2; r++)
{
for (int c = 0; c < 5; c++)
{
myFile >> spaceLayout[r][c];
}
}
myFile.close();
} 
for(int r = 0; r < 2; r++)
{
    for(int c = 0; c < 5; c++)
    {
        cout << spaceLayout[r][c] << " ";
    }
    cout << "\n";
}
} 

Line 5. You are not escaping the '\' character. You need to. You should be getting warnings. Try changing it to this...

ifstream myFile ("F:\\MyApp\\Debug\\layout.txt", ios::in);

Note that there are two, not one slashes between each directory. Since '\' is the escape character, if you want to the program to treat a '\' as backslash and NOT as the escape token, you need to put two of them in, not one. See if that change does anything.

Dude.. dont look into the text file.. It'll probably show weird symbols and characters.. Its the computers way of storing data... U'd probably have entered stuff like : 12, Jake is a monster, I dont like him ... etc But wt u'll see in the text file when u see it manually is !@#$%^&*.. Its the computers way of minimising data storage memory !! Also.. if u want to read something which was stored in a file by using a structure or class or array, it cant be read out just like a normal text file.. U need to know the contents of the structure or the array... Thats probably where ur going wrong..

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.