2012-10-24 48 views
0

我的程序需要兩個參數,第一個參數是要更改的名稱,第二個參數是更改它的名稱。從文本文件改變一行中的一個字段,存儲到數組中比重新寫入文件

執行後,我的文本文件丟失了第二行,它假設從5月更改爲MayMay。我不確定我的if else聲明是否有問題,或while(!file.fail)有問題。

這是我的原始文本文件。

user; pass; 1234; John;  1111 
user1; pass1; 2345; May;  2222 
user2; pass2; 3456; Mary;  3333 
user3; pass3; 4567; Andy;  4444 
hr;  hr;  5678; Jonathan; 5555 
admin; admin; 6789; Aili;  6666 
user10; pass10; 7890; eggy;  9999 
user11; pass11; 9807; Mary;  7777 

這是我的程序執行後的輸出文本文件。

user; pass; 1111; John; 

user2; pass2; 3333; Mary; 
user3; pass3; 4444; Andy; 
hr;  hr;  5555; Jonathan; 
admin; admin; 6666; Aili; 
user10; pass10; 9999; eggy; 
user11; pass11; 7777; Mary; 

     pass11; 7777; Mary; 

這是我的代碼:

bool Employee::changeName(string nnn, string mmm) 
{ 
    int i = 0; 
    ifstream file("login1.txt"); // to open the file 
    string name, empty, empty2, empty3, empty4; // fusername is use to store the first parameter in textfile,empty is use to store the rest of the line after the ';' 
    string store[100]; // initialize a array to store textfile contents 

    while (!file.fail()) // loop if file didn't fail 
    { 
     getline(file, empty, ';'); // use ; as delimiter 
     getline(file, empty2, ';'); // use ; as delimiter 
     getline(file, empty3, ';'); // use ; as delimiter 
     getline(file, name, ';'); // use ; as delimiter 
     getline(file, empty3); // use line end as delimiter, and to skip the rest of the information 
     string add = ""; //initialize add string to nothing when it loops 

     if(name != nnn) // to check if the username in textfile do not match the user input name 
     { 
      add += empty + ';' + empty2 + ';' + empty3 + ';' + name + ';' + empty4; // adds back the username and rest of the line together back  
      store[i] = add; // store into an array 
      cout << "i is: " << i << endl; // debugging. 
      cout << "store array[] = " << store[i] << endl; // debugging.. 
      i++; 
     } 
     else if(name == nnn) 
     { 
      add += empty + ';' + empty2 + ';' + empty3 + ';' + mmm + ';' + empty4; // adds back the name and rest of the line together back 
      store[i]; 
      cout << "i is: " << i <<endl; // debugging. 
      cout << "store array[] = " << store[i] << endl; // debugging.. 
      i++; 
     } 
     else{} 
    } 

    remove("login1.txt"); //remove the textfile 
    ofstream pwd2_file ; // initilize a outputstream textfile 
    pwd2_file.open("login1.txt"); // create a new file call login1.txt 

    for (int x = 0; x < i; x++)//for loop to store store[] array into login1.txt 
    { 
     pwd2_file << store[x] << endl; // storing into textfile 
    } 
    pwd2_file.close(); // close the output stream 
} 

回答

3

導致空行的問題是,當你在更換名稱否則-if語句你有行

add += empty+';'+empty2+';'... 
store[i]; 

我估計應該是

store[i] = add; 
+0

你的權利,改變,當我看到它感謝:D –

1

而留下empty4空的您正在閱讀到empty3每兩次循環:

getline(file, empty, ';'); 
getline(file, empty2, ';'); 
getline(file, empty3, ';'); // <--- here 
getline(file, name, ';'); //  
getline(file, empty3);  // <--- and here (this is supposed to be empty4, right?) 

那麼你有沒有影響的說明(已經指出的查德 - 坎貝爾):

store[i]; 

最後,一個不正確的條件:while(!file.fail()) - 你輸入最後一次迭代時,流仍然是好的 - 你已經讀取了所有的數據,但還沒有試圖讀取結束會使流處於不良狀態 - 接下來的讀取失敗,但你不不檢查它,並靜靜地重用前一次迭代中的數據。

總是在某些布爾上下文中使用輸入操作(例如while(getline(...)))或至少在之後檢查流您嘗試讀取。在你的情況下,這樣的事情:

while (true) 
{ 
    getline(file, empty, ';'); 
    getline(file, empty2, ';'); 
    getline(file, empty3, ';'); 
    getline(file, name, ';'); 
    getline(file, empty3); 
    if (!file) break; 

    // do stuff with data 
} 
相關問題