Click here to Skip to main content
13,149,186 members (51,107 online)
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 4-Dec-12 6:34am
saad_lah1.2K
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: bad
 
good
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);
  Permalink  
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)

  Print Answers RSS
Top Experts
Last 24hrsThis month


Advertise | Privacy |
Web02 | 2.8.170924.2 | Last Updated 4 Dec 2012
Copyright © CodeProject, 1999-2017
All Rights Reserved. Terms of Service
Layout: fixed | fluid

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