Click here to Skip to main content
14,336,087 members
Rate this:
Please Sign up or sign in to vote.
See more:
Hey,
I am having a problem with file handling.


ofstream myfile("courses.txt");
myfile<<C_Semester.code<<"$" << C_Semester.name<<"$" << C_Semester.Pre_req<<"$" << C_Semester.time<<"$" << C_Semester.fee<<"$";
if(C_Semester.isUnderGraduate==true)
    myfile<<"UnderGraduate$!\n";
else 
    myfile<<"Graduate$!\n";
    for(int k=0;k<i;i++)
        myfile<<temp_store[k];	
        //this overwrites the previous data..Main Problem!
        myfile.close();
        delete[] temp_store;

Why the curose position in the file afer if-else condition of graduate or undergraduate move to beginning and then overwrites the data??

Posted
Updated 4-Dec-12 6:46am
v2
Comments
Richard MacCutchan 4-Dec-12 13:02pm
   
I just tested this (after correcting your errors) and it writes all the data to the file as required. I suspect there is some other code that you are not showing us.

1 solution

Rate this:
Please Sign up or sign in to vote.

Solution 1

I suspect that you may be running this code multiple times and forgetting that you need to seek to the end every time you open the file. Change the constructor in your code to:
ofstream myfile("courses.txt", ios::app);
   
Comments
saad_lah 4-Dec-12 13:58pm
   
Thank you...problem solved by appending file

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100