Thread: Why won't fstream file open?

  1. #1
    Registered User
    Join Date
    Aug 2001
    Posts
    154

    Why won't fstream file open?

    What is wrong with this code? The error message runs every time, and the file is never opened. It worked ok when it was ofstream instead of just fstream. I can't figure out what's wrong. Thanks!
    Code:
    #include "person.h"
    #include <iostream>
    #include <fstream>
    
    using namespace std;
    
    int main()
    {
    	fstream personOut("nameAge.txt", ios::in | ios::out);
    	if (!personOut) {cout << "Error!";}

  2. #2
    geek SilentStrike's Avatar
    Join Date
    Aug 2001
    Location
    NJ
    Posts
    1,141
    I am not the end-all source of knowledge of the I/O library, but how could you have something open for both read and write at the same time?
    Prove you can code in C++ or C# at TopCoder, referrer rrenaud
    Read my livejournal

  3. #3
    of Zen Hall zen's Avatar
    Join Date
    Aug 2001
    Posts
    1,007
    If nameAge.txt doesn't exist, any attempt to open it with for use with an input stream will fail.
    zen

  4. #4
    Registered User
    Join Date
    Aug 2001
    Posts
    154
    Well, I know fstream allows reading and writing from/to files. I think zen may be right - there is no nameAge.txt file when I start. If the file is opened by ofstream I know it's created ok. The rest of the code (not posted) creates the file in that case. I thought fstream would create the file as well; the first operation I do after that is initializing the file with values in a struct. But maybe fstream won't create the file by default like ofstream does.
    My original problem is that the first field in the struct is initialized to "unassigned" in a char[15]. That works ok, but when the records are overwritten with new data, if the data is less than 15 characters, say 5 characters, the new value is written over the first 5 characters of "unassigned", but the remaining characters are still there. That's the problem I was trying to fix first.
    Examples in the book show fstream the way I did it, but maybe they assume existance of the file.
    Thanks, I appreciate the help.

  5. #5
    of Zen Hall zen's Avatar
    Join Date
    Aug 2001
    Posts
    1,007
    Examples in the book show fstream the way I did it, but maybe they assume existance of the file.
    They may also be using old stream libraries, where a file is always created. You had to use ios::nocreate to prevent opening of files that didn't exist. But as opening a file for reading that doesn't exist doesn't make much sense (although you could argue that it would make sense if it was opened for both reading and writing), the newer standard libraries prevent this behaviour.
    zen

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. Basic text file encoder
    By Abda92 in forum C Programming
    Replies: 15
    Last Post: 05-22-2007, 01:19 PM
  3. Post...
    By maxorator in forum C++ Programming
    Replies: 12
    Last Post: 10-11-2005, 08:39 AM
  4. Simple File encryption
    By caroundw5h in forum C Programming
    Replies: 2
    Last Post: 10-13-2004, 10:51 PM
  5. file processing updating record error
    By uuser in forum C Programming
    Replies: 2
    Last Post: 04-27-2003, 12:13 AM