2017-07-17 74 views
0

我在嘗試替換文件中的文本時遇到問題。C++爲什麼不將此代碼寫入文件

#include <iostream> 
#include <fstream> 
void Place(string& seek) 
{ 
    fstream myfile; 
    string holder = ""; 
    myfile.open("testfile.txt"); 
    while (seek != holder) 
    { 
     myfile >> holder; 
    } 
    myfile << "300"; 

    myfile.close(); 
} 

使用此我能夠讀取我正在嘗試更改的行,但它不適用於寫入。

string Pull(string& seek) 
{ 
    fstream myfile; 
    string holder = ""; 
    myfile.open("test.txt"); 

    while (seek != holder) 
    { 
     myfile >> holder; 
    } 
    myfile >> holder; 
    myfile.close(); 
    return holder; 
} 
+0

([使用fstream的和刪除文件的其餘部分覆蓋在一個文件中的一些文本]的可能的複製https://stackoverflow.com/questions/ 13112026 /覆蓋文件中的某些文本使用fstream和刪除文件的其餘部分) – Ari0nhh

+0

'myfile >>持有者;'將空字符串寫入文件。 –

+0

沒有將選擇從文件夾入持有者。它用於在文件中移動,在第二個示例中從中提取信息。目前,如果我試圖在while循環之後再次發生任何事情,它並不會令人畏懼。某種程度上while循環會產生干擾嗎? – Lugh

回答

0

追加模式打開文件

myfile.open("testfile.txt",ios::app); 
+0

這似乎阻止它從閱讀並導致while循環無止境。 :/ 追加模式做了什麼? 即使沒有追加模式,只要在while循環之前執行,我就可以使用該文件。 – Lugh

相關問題