a simple file stream question

This is a discussion on a simple file stream question within the C++ Programming forums, part of the General Programming Boards category; hi i just want to know: fstream obj("file", ios: ut | ios::in) is better for working with binary files, tham ...

  1. #1
    Registered User
    Join Date
    Mar 2004
    Posts
    113

    a simple file stream question

    hi
    i just want to know:

    fstream obj("file", ios:ut | ios::in)
    is better for working with binary files, tham ofstream and ifstream?

    i ask this because i noticed that if i write to a file in binary mode
    with ofstrem all the previous data stored in the binary data is deleted
    , if i use ios::app all the data is stored at the end of file, and i cant write data in any place of the file that i want.

    if i try to make an aplication like a database, the ofstrem dont work well.

    so with fstream i can write to any place in the file with no problem.

    one more question:
    why fstream obj("file",ios:ut | ios::in) dont create the file if the file dont exists? how to deal with this? or i need to work with ofstream to create the file and then work with fstream to deal with the file i/o?

    im rigth? any ideas?

    thanks for any help, and please excuse my poor english.
    Last edited by terracota; 07-09-2004 at 07:24 PM.

  2. #2
    Sweet
    Join Date
    Aug 2002
    Location
    Tucson, Arizona
    Posts
    1,806
    check out this tutorial on file i/o it should help you alot
    Clicky
    Woop?

  3. #3
    Registered User
    Join Date
    Mar 2004
    Posts
    113
    good tutorial thanks
    but my question is not answered there

  4. #4
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    >fstream obj("file", ios:ut | ios::in)
    >is better for working with binary files, tham ofstream and ifstream?
    If you are reading and writing to the file, then I would say yes. But if you only writing to the file, then use ofstream.

    >why fstream obj("file",ios:ut | ios::in) dont create the file if the file dont exists?
    It should. Are you using the new style header files?
    #include <fstream>
    using namespace std;

  5. #5
    Carnivore ('-'v) Hunter2's Avatar
    Join Date
    May 2002
    Posts
    2,879
    You can use ofstream.
    Code:
    ofstream obj("file", ios:: out | ios::binary | ios::ate); //open in binary mode, don't delete, for output
    obj.seekp(0); //go to start of file
    Last edited by Hunter2; 07-10-2004 at 12:12 PM. Reason: Oops, wrong flag and function... lol
    Just Google It. √

    (\ /)
    ( . .)
    c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination.

  6. #6
    Registered User
    Join Date
    Mar 2004
    Posts
    113
    Quote Originally Posted by swoopy
    >fstream obj("file", ios:ut | ios::in)
    >is better for working with binary files, tham ofstream and ifstream?
    If you are reading and writing to the file, then I would say yes. But if you only writing to the file, then use ofstream.

    >why fstream obj("file",ios:ut | ios::in) dont create the file if the file dont exists?
    It should. Are you using the new style header files?
    #include <fstream>
    using namespace std;
    dont know why but "fstream obj("file.dat", ios:ut | ios::in);
    dont create the file if dont exists
    and the (!myObjFile) is true, so the error message is displayed

    heres the code:

    Code:
    #include <iostream>
    #include <stdlib.h>
    
    using namespace std;
    
    #include<fstream>
    using std::fstream;
    
    int main()
    {
     fstream myObjFile("test.dat", ios::out | ios::in);
      
      //cant create file 
      if(!myObjFile){
        cerr << "error opening file!.\n";
        system("PAUSE");
        exit(1);
      }
      
      cout << "File created\n";
        
      system("PAUSE");	
      return 0;
    }
    thanks all for reply
    by the way im using dev-c++ and vc++ 6.0 and have the same problem on both
    Last edited by terracota; 07-10-2004 at 12:25 PM.

  7. #7
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    I'm using the free borland compiler at the moment. I don't have dev-c++ installed yet, may do that later tonight.

    > fstream myObjFile("test.dat", ios::out | ios::in);

    Try:
    Code:
     fstream myObjFile;
     myObjFile.open("test.dat", ios::out | ios::in);
    And see if you have the same problem. You might even try:

    myObjFile.open("test.dat", ios::in | ios::out);

    though it would be really weird if that made any difference.

  8. #8
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    By the way, Hunter has a good suggestion. But use fstream instead of ofstream.

  9. #9
    Carnivore ('-'v) Hunter2's Avatar
    Join Date
    May 2002
    Posts
    2,879
    >>though it would be really weird if that made any difference.
    In other words, if ios::in and ios:ut are just enums like they should be, it shouldn't make a difference
    Just Google It. √

    (\ /)
    ( . .)
    c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination.

  10. #10
    Registered User
    Join Date
    Mar 2004
    Posts
    113
    Quote Originally Posted by swoopy
    I'm using the free borland compiler at the moment. I don't have dev-c++ installed yet, may do that later tonight.

    > fstream myObjFile("test.dat", ios:ut | ios::in);

    Try:
    Code:
     fstream myObjFile;
     myObjFile.open("test.dat", ios::out | ios::in);
    And see if you have the same problem. You might even try:

    myObjFile.open("test.dat", ios::in | ios:ut);

    though it would be really weird if that made any difference.
    nope swoopy, still have the same problem but if i use:
    Code:
    fstream myObjFile;
     myObjFile.open("test.dat", ios::out | ios::in | ios::trunc);
    as you see using truncate the file is created without problem, i dont know why but it works
    thanks for help
    Last edited by terracota; 07-10-2004 at 07:47 PM.

  11. #11
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    Quote Originally Posted by Hunter2
    >>though it would be really weird if that made any difference.
    In other words, if ios::in and ios:ut are just enums like they should be, it shouldn't make a difference
    You know, I don't know why I posted that. There's no way I can think of it would make a difference.

  12. #12
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    Quote Originally Posted by terracota
    nope swoopy, still have the same problem but if i use:
    Code:
    fstream myObjFile;
     myObjFile.open("test.dat", ios::out | ios::in | ios::trunc);
    as you see using truncate the file is created without problem, i dont know why but it works
    thanks for help
    I'm glad it works, but I hope it doesn't truncate the file if it already exists. Unless of course that's the behaviour you want.

  13. #13
    Registered User
    Join Date
    Mar 2004
    Posts
    113
    yes thats exactly what i want
    thanks for all the great help

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Writting in a file (simple question...)
    By C_ntua in forum C Programming
    Replies: 6
    Last Post: 06-17-2008, 02:00 PM
  2. Can we have vector of vector?
    By ketu1 in forum C++ Programming
    Replies: 24
    Last Post: 01-03-2008, 04:02 AM
  3. help with text input
    By Alphawaves in forum C Programming
    Replies: 8
    Last Post: 04-08-2007, 04:54 PM
  4. simple File I/O question
    By bobnet in forum C++ Programming
    Replies: 4
    Last Post: 10-25-2003, 06:53 AM
  5. Simple file I/O question
    By Wanted420 in forum C++ Programming
    Replies: 4
    Last Post: 10-08-2003, 11:01 PM

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