2012-04-25 13 views
0

私はtxtファイルを入力として受け取り、テキストファイルの単語からquesを使用して.hファイル(とうまくいけば).cppファイルを生成するプログラムを作成しようとしています。このコードは、入力ファイルを取得した後、segfault 11のコードをコンパイルします。誰かが私を助けることができますか?Segfault 11でベクトルとfstreamを使用していますか? 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

+4

これは疑わしいです: '#include" 12337756_Library.cpp "'。また、 'bool x'が決して初期化されないときの' if(x) 'ははっきりとdodgyです。 – Johnsyweb

+1

デバッグ担当者は、segfaultの時点でスタックの一番上に何を言っていますか? 'createNewFile()'の場合、私たちはあなたを助けることはできません。 – Johnsyweb

答えて

1

あなただけので、あなたのベクトルが1つのだけの要素が含まれているバック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

+0

私はベクトルアクセスが1つだけオフになっていますが。 – Benj

+0

@Benj:それを指摘してくれてありがとう。私の答えを変更しました。 –

関連する問題