Thread: file i/o and classes

  1. #1
    Registered User
    Join Date
    Aug 2002
    Posts
    28

    file i/o and classes

    i have a program that it a little wacked (cause i suck at programming)...anyway, i need some clarification on input and output to/from a file.

    if you declare:
    //code
    fstream myfile;
    myfile=open("myfile.txt", ios::in);
    //endcode

    i understand that this will open "myfile.txt", but does it create that file if it doesn't already exist?

    *sigh*...tonight ends the night of the endless summer.
    eat, drink and be merry. for tomorrow: we party.

  2. #2
    Registered User
    Join Date
    Aug 2002
    Posts
    28

    also...

    the thing that's kind of messing me up is that i have file being opened and read by the constructor in one class, and then being written to by the deconstructor in that same class...so i dont think that the deconstructor is being called...it could also be that i just dont understand deconstructors.
    eat, drink and be merry. for tomorrow: we party.

  3. #3
    Registered User
    Join Date
    Apr 2002
    Posts
    1,571
    I'm pretty sure that it will not create one. I think because you are looking to get input FROM the file that it will return an invalid handle if it doesn't exist. Here are some other things to consider when dealing with files. If you just want to get input from a file you can do this.

    ifstream fin( "data.txt" );

    or for output

    ofstream fout( "data.txt" );

    When you specify fstream you are saying that you may do input AND output.

    Also, if you do not wish that the file be created when attempting to output to a file, you can specify ios::nocreate and if the file doesn't exist it will not be created.
    "...the results are undefined, and we all know what "undefined" means: it means it works during development, it works during testing, and it blows up in your most important customers' faces." --Scott Meyers

  4. #4
    Registered User
    Join Date
    Aug 2002
    Posts
    28

    hmm

    so, i actually want to be using a fstream...like this:

    //code
    fstream myfile;
    myfile=open("myfile.txt");
    //

    will this create the file if that file is not found?
    eat, drink and be merry. for tomorrow: we party.

  5. #5
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    >i understand that this will open "myfile.txt", but does it create that file if it doesn't already exist?

    No. To create it if it doesn't exist use either:

    Code:
    myfile.open("myfile.txt", ios::in | ios::out);
    
    Or:
    
    myfile.open("myfile.txt", ios::in);
    if (!myfile.is_open())
       myfile.open("myfile.txt", ios::out);
    
    And there's also:
    myfile.open("myfile.txt", ios::app);
    to append to a file

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Defining multiple classes in the same header file
    By Stonehambey in forum C++ Programming
    Replies: 2
    Last Post: 08-14-2008, 10:36 AM
  2. #include header files or .cpp files?
    By DoctorX in forum C++ Programming
    Replies: 3
    Last Post: 12-23-2006, 12:21 PM
  3. File I/O, classes
    By w274v in forum C++ Programming
    Replies: 7
    Last Post: 03-23-2006, 02:59 PM
  4. Classes inheretance problem...
    By NANO in forum C++ Programming
    Replies: 12
    Last Post: 12-09-2002, 03:23 PM
  5. classes in an exe. objects from text file.
    By davebaggott in forum Windows Programming
    Replies: 0
    Last Post: 10-08-2001, 02:55 AM