2014-10-01 95 views
0

我有一個包含這些值的文件:在二維數組更換特定值

0 0 0 
0 0 0 
0 0 0 

二維陣列是由下面的代碼加載:

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++; 
    } 
} 

我現在想要做的是簡單找到一種方法來改變數組中的特定值。我已經使用

array[rows][cols] 

之前編輯這些值,但不會與一個文件,是外部的工作,我不似乎得到

seats << [rows][cols] 

工作,所以什麼是最好的如何做到這一點?

+1

在這段代碼中有很多錯誤,「while(!BookingFile.eof())」就是其中之一。 [**閱讀這個瞭解爲什麼**](http://stackoverflow.com/questions/5605125/why-is-iostreameof-inside-a-loop-condition-considered-wrong)。 – WhozCraig 2014-10-01 18:02:34

回答

0

將文件讀入數組,使單元格發生變化,然後將其寫回。

寫入文件需要像以前那樣打開它,但作爲ofstream(輸出文件STREAM),並以類似的方式將它們寫出來,方法與讀取它們的方式類似(一旦你閱讀它們正確)。

+0

請您詳細說明。我明白你的意思,文件進入數組,數組得到修改,然後保存在文件中,但我不知道如何執行保存功能。 – TheSwarre 2014-10-01 16:03:26