Hi!

Below is a part of my code, and whenever I this part of code starts executing, an error ocurrs! I get this error: click here to get a error screenshot whenever I add the ios:app parameter after the userScore.csv part, except that my file isn't QueryLanguage.Lib.dll, but rather something else.
So the only change I make is having ofstream userScore ("userScore.csv", ios:app); instead of just ofstream userScore ("userScore.csv");. Strangely, the new addition to my file's content is appended! But I get the error anyway and I must quit the program.

CODE:

// Save the user score for a subject to a file
                 string userName = marshal_as<string>(lblStudent->Text);
                 string scoreS = to_string(score);
                 subject = subject;

                 ofstream userScore ("userScore.csv");

                 userScore << userName << "," << subject << "," << scoreS << endl;

                 userScore.close();

                 // Get other results
                 ifstream userScoreRead ("userScore.csv");

                 string userResult;

                 while (getline(userScoreRead, userResult))
                 {
                     if (subject == "General")
                     {
                         if (extractWord(1, userResult) == userName && extractWord(2, userResult) == "CS323")
                         {
                             lblCourse2->Text = "CS323: " + marshal_as<String^>(getLastWord(userResult));
                         }

                         if (extractWord(1, userResult) == userName && extractWord(2, userResult) == "IT250")
                         {
                             lblCourse3->Text = "IT250: " + marshal_as<String^>(getLastWord(userResult));
                         }

                         if (extractWord(1, userResult) == userName && extractWord(2, userResult) == "NT310")
                         {
                             lblCourse4->Text = "NT310: " + marshal_as<String^>(getLastWord(userResult));
                         }

                         // If a subject can't be found
                         istreambuf_iterator<char> eof;

                         string searchTerm = userName + ",CS323";

                         if (eof == search(istreambuf_iterator<char>(userScoreRead), eof, searchTerm.begin(), searchTerm.end()))
                         {
                             lblCourse2->Text = "CS323: N/A";
                         }

                         searchTerm = userName + ",IT250";

                         if (eof == search(istreambuf_iterator<char>(userScoreRead), eof, searchTerm.begin(), searchTerm.end()))
                         {
                             lblCourse3->Text = "IT250: N/A";
                         }

                         searchTerm = userName + ",NT310";

                         if (eof == search(istreambuf_iterator<char>(userScoreRead), eof, searchTerm.begin(), searchTerm.end()))
                         {
                             lblCourse4->Text = "NT310: N/A";
                         }
                     }

                     if (subject == "CS323")
                     {
                         if (extractWord(1, userResult) == userName && extractWord(2, userResult) == "General")
                         {
                             lblCourse2->Text = "General: " + marshal_as<String^>(getLastWord(userResult));
                         }

                         if (extractWord(1, userResult) == userName && extractWord(2, userResult) == "IT250")
                         {
                             lblCourse3->Text = "IT250: " + marshal_as<String^>(getLastWord(userResult));
                         }

                         if (extractWord(1, userResult) == userName && extractWord(2, userResult) == "NT310")
                         {
                             lblCourse4->Text = "NT310: " + marshal_as<String^>(getLastWord(userResult));
                         }

                         // If a subject can't be found
                         istreambuf_iterator<char> eof;

                         string searchTerm = userName + ",General";

                         if (eof == search(istreambuf_iterator<char>(userScoreRead), eof, searchTerm.begin(), searchTerm.end()))
                         {
                             lblCourse2->Text = "General: N/A";
                         }

                         searchTerm = userName + ",IT250";

                         if (eof == search(istreambuf_iterator<char>(userScoreRead), eof, searchTerm.begin(), searchTerm.end()))
                         {
                             lblCourse3->Text = "IT250: N/A";
                         }

                         searchTerm = userName + ",NT310";

                         if (eof == search(istreambuf_iterator<char>(userScoreRead), eof, searchTerm.begin(), searchTerm.end()))
                         {
                             lblCourse4->Text = "NT310: N/A";
                         }
                     }

                     if (subject == "IT250")
                     {
                         if (extractWord(1, userResult) == userName && extractWord(2, userResult) == "CS323")
                         {
                             lblCourse2->Text = "CS323: " + marshal_as<String^>(getLastWord(userResult));
                         }

                         if (extractWord(1, userResult) == userName && extractWord(2, userResult) == "General")
                         {
                             lblCourse3->Text = "General: " + marshal_as<String^>(getLastWord(userResult));
                         }

                         if (extractWord(1, userResult) == userName && extractWord(2, userResult) == "NT310")
                         {
                             lblCourse4->Text = "NT310: " + marshal_as<String^>(getLastWord(userResult));
                         }

                         // If a subject can't be found
                         istreambuf_iterator<char> eof;

                         string searchTerm = userName + ",CS323";

                         if (eof == search(istreambuf_iterator<char>(userScoreRead), eof, searchTerm.begin(), searchTerm.end()))
                         {
                             lblCourse2->Text = "CS323: N/A";
                         }

                         searchTerm = userName + ",IT250";

                         if (eof == search(istreambuf_iterator<char>(userScoreRead), eof, searchTerm.begin(), searchTerm.end()))
                         {
                             lblCourse3->Text = "General: N/A";
                         }

                         searchTerm = userName + ",NT310";

                         if (eof == search(istreambuf_iterator<char>(userScoreRead), eof, searchTerm.begin(), searchTerm.end()))
                         {
                             lblCourse4->Text = "NT310: N/A";
                         }
                     }

                     if (subject == "NT310")
                     {
                         if (extractWord(1, userResult) == userName && extractWord(2, userResult) == "CS323")
                         {
                             lblCourse2->Text = "CS323: " + marshal_as<String^>(getLastWord(userResult));
                         }

                         if (extractWord(1, userResult) == userName && extractWord(2, userResult) == "IT250")
                         {
                             lblCourse3->Text = "IT250: " + marshal_as<String^>(getLastWord(userResult));
                         }

                         if (extractWord(1, userResult) == userName && extractWord(2, userResult) == "General")
                         {
                             lblCourse4->Text = "General: " + marshal_as<String^>(getLastWord(userResult));
                         }

                         // If a subject can't be found
                         istreambuf_iterator<char> eof;

                         string searchTerm = userName + ",CS323";

                         if (eof == search(istreambuf_iterator<char>(userScoreRead), eof, searchTerm.begin(), searchTerm.end()))
                         {
                             lblCourse2->Text = "CS323: N/A";
                         }

                         searchTerm = userName + ",IT250";

                         if (eof == search(istreambuf_iterator<char>(userScoreRead), eof, searchTerm.begin(), searchTerm.end()))
                         {
                             lblCourse3->Text = "IT250: N/A";
                         }

                         searchTerm = userName + ",General";

                         if (eof == search(istreambuf_iterator<char>(userScoreRead), eof, searchTerm.begin(), searchTerm.end()))
                         {
                             lblCourse4->Text = "General: N/A";
                         }
                     }
                 }

                 userScoreRead.close();

                 lblCourse2->Visible = true;
                 lblCourse3->Visible = true;
                 lblCourse4->Visible = true;

                 showTabPage(tabResults);

Any solutions?

Thanks!

Thanks for the reply, Dragon! I already use ofstream in several places of my code and it works perfectly, so I didn't know why it wouldn't here, out of all places? Could you give me an example of how to use what you just suggested me, 'cause I'm not absolutely sure what you mean?

What I actually need is to append some data to the end of the file contents. But I already tried this and I got the exact same error (if this is what you meant):

System::IO::StreamWriter^ sw = gcnew System::IO::StreamWriter("userScore.csv", true);
                                 sw->WriteLine(userName + "," + subject + "," + scoreS);
                                 sw->Close();

... where I previously appropriately converted the userName, subject and scoreS from string to String^. This piece of System::IO::StreamWriter code worked perfectly in another file where it should have appended some other data, but it won't append here!

Any ideas why it won't work here? If StreamWriter isn't what you meant, I'd be grateful if you told me what it was.

Thanks!

You'd get a better response on a .NET/Managed C++ forum - this is a C++ forum and whilst some C++ coders are also familiar with .NET and managed C++ and all that, many are not.

@Dragon, thanks, but looks like I got the exact same error again! :(
I used this code you've sent me:

// This text is always added, making the file longer over time 
                 // if it is not deleted.
                 System::IO::StreamWriter^ sw = File::AppendText("userScore.csv");
                 try
                 {
                     sw->WriteLine(lblStudent->Text + "," + lblCourse->Text + "," + lblScore->Text);
                 }
                 finally
                 {
                     if (sw)
                     {
                         delete (IDisposable^)sw;
                     }
                 }

...but the result was the same error. In fact, both in my previous code versions and this one, the new content was indeed appended, but the problem was that I still had this nasty StackOverflowException error.

Every time I tell the program not to append, but just write in it instead, it writes the new content and overwrites the old one. Looks like my program gets confused whenever it has to go through more than 1 line of text (the while(getline()) part). The while-getline loop works perfectly in other parts of my program, but it looks as if it makes a problem here...

Damn, it's bugging me now... what could it be? :(

lIne 12: you shouldn't need to delete the streamwriter -- normal garbage collection will take care of that for you. Delete lines 8-14.

Edited 3 Years Ago by Ancient Dragon

OK, I deleted that part, but I'm still left with the previous problem. I'm going to try and reformulate the file reading part somehow, but I don't know how I could do it without using the while-getline part. I'd still need some kind of loop, right?

Looks like that part is making the problem, not the file writing part.

This article has been dead for over six months. Start a new discussion instead.