Hi all

I have a function writetofile() which save a record to a file but the problem is when its writting to a file it doesnt write the record in a correct order, here is the code for WriteToFile()

void StudentC::WriteToFile()
{
       ofstream Outfile;
       Outfile.open("File\\Student.txt", ios::app);

       Outfile << Count << endl;
       Outfile << StudentNo << endl;
       Outfile << Name << endl;
       Outfile << Surname << endl;
       Outfile << Gender << endl;
       Outfile << DateOfBirth << endl;
       Outfile << Mark << endl;
       
       Outfile.Close();
}

Now the problem is when it is saving a record to a file it 1st write the Count which is correct, from there it writes the Name instead of StudentNo, followed by Surname, DateofBirth, Gender, Mark and StudentNo, if u know why it is messing up my record please I need ur help urgent, coz the order should be like the way it is on the WriteToFile() method but it isnt, I hope this make sense

Thanks in Advance

Recommended Answers

All 3 Replies

You're opening the file in append mode.
Are you sure you're not looking at the data from previous runs, where you might have had a different order to the data?

The append mode is just there to avoid the overwrite of the existing records

Im not sure if I understand what ur saying can u ellaborate a bit

Thanks

Let me tell you something. You cut a part of your code and put it here, and when we try to figure out what's wrong, we are totally lost. hehehe, seriously!

But, first of all, it's "Outfile.close" not "Outfile.Close".

Second of all, There is nothing wrong with your code. But here are some pointers:

* Try to delete the Student.txt from your folder and run the program that I copied here and see the result.

* You explained why you used app, so beware of the outcome.

* You don't write your variables name like the way you do in human language. What I mean is, there are some conventions that you should follow. As far as I know, System Programmers and Unix programmers, use "_" kind of format, Like:

int pointer_to_file;

But Object oriented programmers and framework programmers use FIRST SMALL, REST BIG(I just made it up!!!). Means it's gonna be like:

int pointerToFile;

It's up to you how to use it, but I think you'd better at least follow a convention to give some professional look to your program. Professionals, please correct me if I'm wrong.

And after all the story here goes the code, just copy and paste it and compile...

#include <fstream>
using namespace std;


void WriteToFile();


int main()
{
    WriteToFile();

    return 0;
}


void WriteToFile()
{
    int Count = 10, StudentNo = 102211;
    string Name = "Tom", Surname = "Hanks", Gender = "Male", DateOfBirth = "8/14/2009";
    double Mark = 15.5;

       ofstream Outfile;
       Outfile.open("Student.txt", ios::app);

       Outfile << Count << endl;
       Outfile << StudentNo << endl;
       Outfile << Name << endl;
       Outfile << Surname << endl;
       Outfile << Gender << endl;
       Outfile << DateOfBirth << endl;
       Outfile << Mark << endl;
       
       Outfile.close();
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.