0

This is a function I created for creating a course folder inside a section folder which already exists. However, when I run this function my console app just crashes. I need help trouble shooting.

The first part is simply an authentication for a correctly entered degree program. The course ID has a validation of four integers on it. I omitted that part of the code since it was error free.

In the next part I retrieve a name of sections from a file containing their name "Sectionlist.csv" and pass it on to strings to create directories and then create quizzes and assignments.csv files. However, this crashes the console app exactly after the message "Course has been successfully created".

Here is my code for that last part. What could be the possible reasons here? Any help will be appreciated.

string sectiondir="./data/"+program_entered+"/Sectionlist.csv";//directory of section list
string secname[3];//array for sections
ifstream sectionlist(sectiondir.c_str()); // reading section file of the degree programme
int iterator=0;

if (sectionlist.fail())
  cerr << "File couldnt be opened" << endl;
else
  while (!(sectionlist.eof())) { // reading name of sections and storing in section array
    getline(sectionlist,secname[iterator],'\n');
    iterator++;
  }

for (int i=0;i<iterator;i++) { // loop for running the section array
  string newdir="./data/"  +program_entered + "/" + secname[i] + "/" + course_entered; // directory for new course in each section
  _mkdir(newdir.c_str()); // make directory function
  string quizdir = newdir + "/quizes.csv"; // directory for resective quizes
  string assigndir = newdir + "/assignments.csv"; // directory for respective assignments
  ofstream quizes(quizdir.c_str(),ios::app); // creating a file of quizes.csv
  ofstream assignments(assigndir.c_str(),ios::app); // creating an assignment file
}
ocrdu
  • 2,172
  • 6
  • 15
  • 22
  • 1
    Can you run it in a debugger? Can you get a stack trace somehow? Can you step through the program and check what happens? That would help a lot. – U. W. Jan 21 '21 at 13:10
  • 1
    [Why is iostream::eof inside a loop condition (i.e. `while (!stream.eof())`) considered wrong?](https://stackoverflow.com/questions/5605125/why-is-iostreameof-inside-a-loop-condition-i-e-while-stream-eof-cons) should take care of one bug. Whether that fixes everything is a different matter. – molbdnilo Jan 21 '21 at 13:10
  • So i replaced !(sectionlist.eof()) with sectionlist.peek()!=EOF and it worked like a charm – Syed Tallay Haidar Jan 21 '21 at 13:21
  • Thanks.Thats saved me so much trouble while i am already very short of time on my project. – Syed Tallay Haidar Jan 21 '21 at 13:21
  • The preferred form is `while(getline(sectionlist,secname[iterator],'\n'))...`. – molbdnilo Jan 21 '21 at 15:58

0 Answers0