2012-04-25 55 views
0

我試圖編寫一個程序,它將一個txt文件作爲輸入,並使用文本文件中的單詞中的ques生成一個.h文件(以及後來希望的).cpp文件。這段代碼編譯得很好,只是它一旦得到輸入文件就會出現段錯誤11。有人可以幫我嗎?代碼中使用向量和fstream的Segfault 11? C++

#include <iostream> 
#include <fstream> 
#include "12337756_Library.cpp" 
#include <string> 
#include <vector> 

using namespace std; 

int main() 
{ 
    bool a; 
    string filename; 
    string line; 
    vector<string> Attr; 

    cout << "Enter input file name:"; 
    getline(cin, filename); 

    ifstream fin(filename.c_str()); 

    if (fin) 
    { 
     while(!fin.eof()) 
     { 
      getline(fin, line); 
      createNewFile(line); 
      Attr.push_back(line); 
     } 
     if(Attr[1]=="Movie.h") 
     { 
      bool x,y; 
      //x=createNewFile(Attr[0]); 
      y=createNewFile(Attr[1]); 
      if(x) 
      { 
       ofstream fout(Attr[1].c_str()); 
       fout << "#ifndef HEADER_H_" << endl << "#define HEADER_H_" << endl; 
       fout << "#include <iostream>" << endl << "#include <vector>" << endl; 
       fout << "using namespace std;" << endl; 
       fout << "enum Movie_Rating {G,PG,PG13,R,NC17,NR} ;" << endl; 
       fout << endl << endl << endl; 
       fout << "class Movie" << endl << "{"<< endl; 
       fout << "public: " << endl; 
       fout << "// ------------------------------------------------------" << endl; 
       fout << "// ----- Constructors -----------------------------------" << endl; 
       fout << "// ------------------------------------------------------" << endl; 
       fout << endl << "Movie();" << endl << "Movie(const string& title);" << endl; 
       fout << "Movie(const string& title, const string& director, Movie_Rating rating,unsigned int year,const string& path,const string& actor); " << endl; 
       fout << endl; 
       fout << "// ------------------------------------------------------" << endl; 
       fout << "// ----- Destructor -------------------------------------" << endl; 
       fout << "// ------------------------------------------------------" << endl; 
       fout << endl; 
       fout << "~Movie();" << endl << endl; 
       fout << "// ------------------------------------------------------" << endl; 
       fout << "// ----- Inspectors -------------------------------------" << endl; 
       fout << "// ------------------------------------------------------" << endl; 
       fout << endl; 
       fout << "string getTitle() const;" << endl; 
       fout << "string getDirector() const;" << endl; 
       fout << "Movie_Rating getRating() const ;" << endl; 
       fout << "unsigned int getYear() const ;" << endl; 
       fout << "string getURL() const ;" << endl; 
       fout << "string getActor(unsigned int i) const ;" << endl; 
       fout << "int getActorNumber() const ;" << endl; 
       fout << endl; 
       fout << "// ------------------------------------------------------" << endl; 
       fout << "// ----- Mutators ---------------------------------------" << endl; 
       fout << "// ------------------------------------------------------" << endl; 
       fout << endl; 
       fout << "void setTitle(const string& title);" << endl; 
       fout << "void setDirector(const string& director) ;" << endl; 
       fout << "void setRating(Movie_Rating rating) ;" << endl; 
       fout << "void setYear(unsigned int year) ;" << endl; 
       fout << "void setURL(const string& path) ;" << endl; 
       fout << "void setActor(const string& actor);" << endl; 
       fout << endl; 
       fout << "//-----------------------------------------------------------" << endl; 
       fout << "//------- Facilitators --------------------------------------" << endl; 
       fout << "//-----------------------------------------------------------" << endl; 
       fout << "void output(ostream & out);" << endl; 
       fout <<"// ----------------------------------------------------------" << endl; 
       fout <<"// ----------------------------------------------------------" << endl; 
       int size = Attr.size(); 

       while(size!= 1) 
       { 
        fout << Attr[size] << endl; 
        size--; 
       } 
       fout << "};" << endl; 
      } 
     } 
    } 
} 
+6

'while(!fin.eof())',最流行的現代C++反模式。 – ildjarn 2012-04-25 22:41:29

+4

這看起來很可疑:'#include「12337756_Library.cpp」'。當'bool x'永遠不會初始化時'if(x)'顯然是不可靠的。 – Johnsyweb 2012-04-25 22:43:18

+1

你的調試器在段錯誤時在堆棧的頂部表示什麼?如果它是'createNewFile()',我們不能幫你。 – Johnsyweb 2012-04-25 22:51:27

回答

1

你只推後一個std::string所以你的載體只包含1個元素。要訪問該元素,請使用Attr[0]而不是Attr[1](在代碼中有幾個地方可以這樣做)。記住在C++索引開始於0而不是1

此外,在下面的代碼size()返回1,所以while循環從不輸入。

int size = Attr.size(); 

while(size!= 1) 
{ 
    fout << Attr[size] << endl; 
    size--; 
} 
+0

如果size()返回1,那麼您甚至不會輸入循環嗎? – Benj 2012-04-25 23:03:58

+0

雖然我感到矢量訪問關閉了一個。 – Benj 2012-04-25 23:05:51

+0

@Benj:謝謝你指出。改變了我的答案。 – 2012-04-25 23:08:10