Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C++
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.1K
Edited 4-Dec-12 6:46am
v2
Comments
Richard MacCutchan at 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 at 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
0 OriginalGriff 445
1 CHill60 410
2 Sergey Alexandrovich Kryukov 354
3 BillWoodruff 190
4 PIEBALDconsult 160


Advertise | Privacy | Mobile
Web01 | 2.8.141015.1 | Last Updated 4 Dec 2012
Copyright © CodeProject, 1999-2014
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