0

我正在尝试编写一个将 txt 文件作为输入的程序,并使用文本文件中单词中的 ques 来生成一个 .h 文件(希望以后再生成一个 .cpp 文件)。这段代码编译得很好,只是它在获取输入文件后会出现 11 段错误。有人可以帮帮我吗?

#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;
            }
        }
    }
}
4

1 回答 1

1

您只推回了一个std::string,因此您的向量仅包含 1 个元素。要访问该元素,请使用Attr[0]not Attr[1](在代码中有几个地方可以这样做)。请记住,在 C++ 中,索引开始于0而不是1.

此外,在下面的代码中size()返回1,因此永远不会进入 while 循环。

int size = Attr.size();

while(size!= 1)
{
    fout << Attr[size] << endl;
    size--;
}
于 2012-04-25T22:48:05.627 回答