vendredi 19 décembre 2014

Using counter from for loop as condition

I am creating a program in C++ that takes a pdf and converts it into a epub. I am having trouble with my function that reads in the chapters and creates different files for each chapter. The input file will look something like this:



Chapter 1



blah blah blah ..... blah



Chapter 2



blah blah blah ..... blah




I am trying to use the for loop to check the string for the beginning of a chapter and if it is, then create a xhtml file with the name of 'chapter#.xhtml' in the correct directory. The problem I am having is checking for the text and creating the respective file.



for(int i=1;i<chapters;i++){
char j = i;
string chap = "book/OEBPS/chapters/chapter";
chap.append(1,j).append(".xhtml");
if(line == "Chapter "+j){
out << "</div>\n</body>\n</html>";
out.close();
out.open(chap.c_str());
break;
}
}


Essentially I am trying to create a counter that either is a string or can be added to a string to use in the if statement conditional and the out.open parameter. Below is the whole function in case that helps.



void readChapters(ofstream& out,int chapters){
string line,file;
char letter;
ifstream in;
cout << "Enter input file\n";
cin >> file;
in.open(file.c_str());
out.open("junk");
getline(in,line);
while(in) {
for(int i=1;i<chapters;i++){
char j = i;
string chap = "book/OEBPS/chapters/chapter";
chap.append(1,j).append(".xhtml");
if(line == "Chapter "+j){
out << "</div>\n</body>\n</html>";
out.close();
out.open(chap.c_str());
break;
}
}
getline(in,line);
if(line == "\n")
getline(in,line);
out << line << "\n</p>\n<p>\n";
}
out << "</div>\n</body>\n</html>";
out.close();
remove("junk");
}

Aucun commentaire:

Enregistrer un commentaire