ofststream cannot open existing file

This is a discussion on ofststream cannot open existing file within the C++ Programming forums, part of the General Programming Boards category; hi, I am not able to open ofstream exisiting files.Though it can create new files.Here's my code Code: void source_station::save_station(char* ...

  1. #1
    Registered User
    Join Date
    Aug 2005
    Posts
    113

    ofststream cannot open existing file

    hi,
    I am not able to open ofstream exisiting files.Though it can create new files.Here's my code
    Code:
    void source_station::save_station(char* source)
    {
      ofstream station;
      station.open(source,ios::app|ios::noreplace|ios::binary);
      if(!station)
        cout<<"Could not open file";
    
      else
        station.write((char*)this,sizeof(*this));
      station.close();
    }

  2. #2
    Registered User
    Join Date
    Jan 2005
    Posts
    7,328
    Are you sure the file exists there. Did you try a full path instead of a relative path, since often the "current directory" is different than what you'd expect.

  3. #3
    Registered User
    Join Date
    May 2004
    Posts
    164
    Well, I see your passing it a variable "source" might output source on screen to make sure its valid, also you have a syntax error on the ios::binary there is a bad space, I might try this:

    Code:
    source.open(source, ios::app);
    if (source.fail())
    {
        cout<<"source file failed to open"<<endl;
    }
    else
    {
        //do your work here
    }
    I don't know you need the ios::noreplace if your using ios::app since it will only create the file if it did not exist before, otherwise just adds to it without over writing. Also, not sure about the syntax of using (ios::app | ios:: noreplace) I am not the most avid user of iostream or fstream but I don't recall ever using the "|" symbol as a separator, I've always used ",". Hope that helps-

  4. #4
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,802
    Quote Originally Posted by WaterNut
    also you have a syntax error on the ios::binary there is a bad space, I might try this:
    That's something the board automatically does based on length of the string. If there aren't spaces every so often, it puts them in for you whether you want it to or not.

    Example #1 (I did not enter in a space between X and Y when posting):
    abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWX YZ

    Example #2 (I did not enter in a space between X and Y when posting):
    Code:
    abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ
    So the limit seems to be 50 characters without spaces which is how long station.open(source,ios::app|ios::noreplace|ios::b is.

    Quote Originally Posted by WaterNut
    Also, not sure about the syntax of using (ios::app | ios:: noreplace) I am not the most avid user of iostream or fstream but I don't recall ever using the "|" symbol as a separator, I've always used ",".
    The ios::app and ios::noreplace values are being binary OR'ed together (they are like flags). When the function opens the file it checks to which bits (flags) have been set to see exactly how the user wants to open the file.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  5. #5
    Registered User
    Join Date
    Jan 2005
    Posts
    7,328
    Also note that ios::noreplace is non-standard, you should probably switch to <fstream>.

  6. #6
    Registered User
    Join Date
    Feb 2006
    Posts
    312
    One "gotcha" which often catches unsuspecting coders out opening files in C++ if they're not paying attention: if you want to open up a file in DOS/windows, on the path

    C:\MyDir\somefile.txt

    then the string name for this path is

    "C:\\MyDir\\somefile.txt"

    You need the double-backslashes, else it thinks you are trying to use an escape character (eg, \n, \t, \0, etc).

  7. #7
    Registered User
    Join Date
    Aug 2005
    Posts
    113
    I think I must write the full code
    Code:
    class source_station
    {
    private:
      char station_name[8];
      int train_no;
      float distance;
    
    public:
      void getstation_info();
      void save_station(char *source);
      void search_station();
      void read_station(char *source);
    };
    
    void source_station::getstation_info()
    {
    
      cout<<"\nEnter station name:";
      cin.get(station_name,8);
      cout<<"Train num:";
      cin>>train_no;
      cout<<"Distance:";
      cin>>distance;
    }
    
    void source_station::save_station(char* source)
    {
      ofstream station;
      station.open(source,ios::app|ios::noreplace|ios::binary);
      if(!station)
        cout<<"Could not open file";
    
      else
        station.write((char*)this,sizeof(*this));
      station.close();
    }
    
    int main()
    {
    source_station obj;
    obj.getstation_info();
    obj.save_station("c:\\tc\\project\\agra.txt");
    getch();
    return(0);
    }
    Though above code works well if it creates new file but it is not able to open alredy existing file.

  8. #8
    Registered User
    Join Date
    Aug 2005
    Posts
    113
    By removing ios::noreplace the code works absolutely fine.But can anyone explain why?

  9. #9
    Registered User
    Join Date
    Jan 2005
    Posts
    7,328
    Wouldn't noreplace mean that it doesn't open an existing file? So even though you are appending, the noreplace stops you from opening an existing file. This is just a guess, though since you are using the non-standard streams I don't know what the expected behavior should be.

    >> I think I must write the full code
    FYI, that isn't the full source code. The headers you include are an important detail.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. opening empty file causes access violation
    By trevordunstan in forum C Programming
    Replies: 10
    Last Post: 10-21-2008, 11:19 PM
  2. Replies: 12
    Last Post: 03-10-2005, 06:48 PM
  3. file processing updating record error
    By uuser in forum C Programming
    Replies: 2
    Last Post: 04-27-2003, 12:13 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21