0

I have a file that contains these values:

0 0 0
0 0 0
0 0 0

The 2D array is loaded by the following code:

string Seats[10][10]; // creates array to hold strings 

short loop1 = 0; //short for loop for input
short loop2 = 0; //short for loop for input

string line; //this will contain the data read from the file
ifstream BookingFile("test.txt"); //opening the file.

if (BookingFile.is_open()) //if the file is open
{
    cout << "These are the available seats: \n" << endl;
    while (!BookingFile.eof()) //while the end of file is NOT reached
    {
        getline(BookingFile, line); //get one line from the file
        Seats[loop1][loop2] = line;
        cout << Seats[loop1][loop2] << endl; //and output it
        loop1++;
        loop2++;
    }
}

What i now want to do is simply find a way to alter a specific value in the array. I have used

array[rows][cols]

to edit these values before, but that doesn't work with a file that is external, and i dont seem to get

seats << [rows][cols]

to work, so what is the best way to do this?

  • 1
    Among the multiple things wrong in this code, `while (!BookingFile.eof())` ranks among them. [**Read this to understand why**](http://stackoverflow.com/questions/5605125/why-is-iostreameof-inside-a-loop-condition-considered-wrong). – WhozCraig Oct 01 '14 at 18:02

1 Answers1

0

Read the file into an array, make the cell changes you wish, and then write it back out.

Writing to the file would involve opening it as you did before, but as an ofstream (Output File STREAM), and write them out in a manner similar manner to how you would read them in (once you get to reading them correctly).

Scott Hunter
  • 48,888
  • 12
  • 60
  • 101
  • Could you please elaborate. I understand what you mean, file goes into array, array gets modified and then saved in file, but i don know how to perform the save function. –  Oct 01 '14 at 16:03