2014-10-01 103 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