0

can anyone recognize mistake of using fstream variable fFile here? because at function Save() writing to disk process (by function WriteTo()) is always fail. But if I declare new local fstream variable instead of fFile, save is ok. (please see part of the code below)

thank you


class CardCollection{
public:
    CardCollection();
    int Open(const char filename[]);    
    void Close();   
    void Close();   
int NumCards()const;    
void ReportStatus()const;   
void AddCards();    
void DeleteCard(int cardnum);   
void ShowCard(int cardnum)const;    
void ChangeCard(int cardnum);   
void DoFind();  
void DoFindAgain(); 
void DoView();
private:
int GetField(int anyallowed);   
void Load();    
int fNumCards;
char *fFileName;    
std::fstream fFile;
DynamicArray fStore;
char *fFindString;
int fFindPos;
int fSearchField;
};



int CardCollection::Open(const char filename[])
{
    //Keep copy of filename
fFileName = new char[strlen(filename)+1];
strcpy(fFileName, filename);

fFile.open(fFileName, std::ios::out | std::ios::in);
if(!fFile.good())
    return -1;
Load();
return 0;
}

void CardCollection::Save()
{
for(int i = fNumCards ; i > 0; i--){
    RefCard *r = (RefCard*) fStore.Nth(i);
    r->WriteTo(fFile);      // If I declare a new fstream here
                    // instead of using fFile, save is ok
}
if(fFile.good()){
    std::cout << "Saving completed";
}
else{
    std::cout << "Saving error";
}
}
10
  • Did you call Open before Save? Did it succeed?
    – Domi
    Nov 29, 2013 at 10:45
  • Is there a reason you don't use std::string for strings, instead of pointers? (Like for example fFileName.) The same with bool, why not use it for example as the return of the Open function? Nov 29, 2013 at 10:45
  • Did you make sure that filename and fFileName are correct?
    – Domi
    Nov 29, 2013 at 10:45
  • No mistake in the code here. The mistake is somewhere else (like probably inside Open or Save).
    – john
    Nov 29, 2013 at 10:46
  • When has <!-- language: c++ --> been a comment in C++?
    – Ed Heal
    Nov 29, 2013 at 10:47

1 Answer 1

1

I'm willing to bet that you don't reset the fstream after you have loaded the data.

Add this to the beginning of Save

fFile.clear();
fFile.seekp(0);
3
  • 1
    Given the use, he probably shouldn't keep the file open all this time anyway. The best solution would be to open the file (uniquely for input) in Load, and to open it again (this time only for output) in Save. Nov 29, 2013 at 10:51
  • @JamesKanze This is true. In fact there appear to be any number of data members of CardCollection that shouldn't be there. But learning OOP is another task.
    – john
    Nov 29, 2013 at 10:52
  • yes it solve by add: fFile.clear(); fFile.seekp(0); @john thank you
    – dja
    Nov 29, 2013 at 12:17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.