我试图通过C++编辑文本文件中的特定值。我的方法是将所有文本值复制到一个数组编辑我想要的然后覆盖旧文件。一切顺利,直到:编辑文本C++中的文件
int nm=0;
while (nm < arraysize) {
myfile << array1[nm];
nm++;
}
该文件不会更改或添加新的值。这是全功能
void receptionist::edit_room(int pre1) {
fstream myfile ("booked.txt");
int newroom_num;
cout << "Please Enter The New Room Number You wish : " << endl;
cin >> newroom_num;
const int arraysize=20;
int array1[arraysize];
int i=0;
if(myfile.is_open()) {
while(i < arraysize && myfile >> array1[i]) {
myfile >> array1[i];
i++;
}
myfile.clear();
int x=0;
while (x < arraysize) {
if (pre1 == array1[x]) {
array1[x] = newroom_num;
break;
}
x++;
}
int nm=0;
while (nm < arraysize) {
if (array1[nm]> -1) { // this is how i fixed the garbage values
myfile << array1[nm] << endl;
nm++;
}
else {
break;
}
}
myfile.close();
}
else {
cout << "Couldn't Open " << endl ;
}
};
,使其更容易阅读,请格式化代码 - 没有人喜欢通过格式化代码来读取。 – Rndm
很抱歉,但我不知道如何? – BishoyM