2017-03-21 194 views
0

該代碼應該創建一個txt文檔並在其中放置一些文本。但是,在第10,25和26行(我將在旁邊發表評論),我收到了錯誤。請幫助,並提前致謝!創建txt文檔C++

#include <iostream> 
#include <fstream> 
#include <stdlib.h> 
using namespace std; 

int main() 
{ 
    string textFileName = "SavedPasswords.txt"; 

    ofstream textFile(textFileName);//10 

    textFile << "Password1" << endl << "Password2" << endl; 

    textFile.close(); 

    string directory; 
    size_t path = textFileName.rfind("\\"); 

    if(string::npos != path) 
    { 
     directory = textFileName.substr(0, path); 
    } 

    system("cd\\"); 
    system("cd " + directory);//25 
    system("start " + textFileName);//26 
} 

下面是錯誤:

C:\Users\User\Desktop\SavePasswords\main.cpp|10|error: no matching function for call to 'std::basic_ofstream::basic_ofstream(std::string&)'| 

C:\Users\User\Desktop\SavePasswords\main.cpp|25|error: cannot convert 'std::basic_string' to 'const char*' for argument '1' to 'int system(const char*)'| 

C:\Users\User\Desktop\SavePasswords\main.cpp|26|error: cannot convert 'std::basic_string' to 'const char*' for argument '1' to 'int system(const char*)'| 
+0

編譯器錯誤?你收到什麼錯誤?如果你將其添加到你的問題,這將有所幫助。 – Tas

+0

在下面發佈了錯誤。 – onlineone22

+0

什麼是10,25和26行? –

回答

2

ofstream的構造函數需要一個char*而不是string。你可以很容易地從你的string一個char*c_str()成員函數:

ofstream textFile(textFileName.c_str()); 
+0

謝謝,錯誤消失了!我做了什麼系統的東西? – onlineone22

+1

@ onlineone22如果你還有其他問題,你應該發佈一個新問題。在網站允許的情況下,請務必接受我的回答。 –

+0

該問題是要求修復代碼中的所有錯誤,而不僅僅是像你這樣的第一個錯誤。 – onlineone22