Click here to Skip to main content
15,881,248 members
Please Sign up or sign in to vote.
1.00/5 (1 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

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:
C++
ofstream myfile("courses.txt", ios::app);
 
Share this answer
 
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, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900