Cannot open binary file

This is a discussion on Cannot open binary file within the C++ Programming forums, part of the General Programming Boards category; I am working through the book C++ Without Fear and just got to the part where binary files are introduced. ...

  1. #1
    Registered User
    Join Date
    Jan 2008
    Posts
    19

    Cannot open binary file

    I am working through the book C++ Without Fear and just got to the part where binary files are introduced. I created a program using binary files, but it can never successfully open the file. I tried some code straight from the CD that comes with the book and that had the same problem.

    This is the line of code used to open the file:
    Code:
    fstream fileout(filename, ios::binary | ios::in | ios::out);
    Any idea what is going on?

    The only thing I can think of is that the file needs to already exist, but that is not true of text files, so I doubt that is the problem. If it is the problem, how would I create a binary file?

    Thanks!

    Below is my code, which is very similar to the code in the book.
    Code:
    #include <iostream>
    #include <string.h>
    #include <fstream>
    
    using namespace std;
    
    int get_int(int def);
    
    main(){
           int recsize, recnum;
           char model[20], make[20], year[5];
           int miles;
           char filename[81];
           
           recsize = sizeof(model) + sizeof(make) + sizeof(year) + sizeof(int);
           
           cout << "Enter filename and press ENTER: ";
           cin.getline(filename, 80);
           
           fstream fileout(filename, ios::binary | ios::in | ios::out);
           
           if(! fileout){
                cout << "Error opening file.";
                getchar();
                return -1;
                }
                
           while(true){
                       cout << "Enter record to write to: ";
                       recnum = get_int(0);
                       
                       fileout.seekp(recnum * recsize);
                       
                       fileout.write(model, 20);
                       fileout.write(make, 20);
                       fileout.write(year, 5);
                       fileout.write(reinterpret_cast<char*>(&miles), sizeof(int));
                       }
           fileout.close();
           getchar();
           return 0;
           }
                       
    int get_int(int def){
        char inpt_str[81];
        int inpt;
        
        cin.getline(inpt_str, 80);
        if(strlen(inpt_str) == 0){
                            inpt = def;
                            }else{
                                  inpt = atoi(inpt_str);
                                  }
        
        return inpt;
    }

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Try printing the filename to ensure that it's got the content you expect.

    What compiler are you using? What file are you trying to open?

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,892
    And purely conceptually, why do you call an fstream "fileout" and then open the file for writing and reading?
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  4. #4
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Katy, Texas
    Posts
    2,309
    Fails for me too if the file does not exist, and if I create the file ("touch" on unix), it opens fine.

    I suspect it's because you are opening for both input and output, and the file not existing for the ios::in trumps the file not existing for the ios::out.

    Seems obvious, but I haven't found this stated in any doc anywhere yet.

    Todd
    Mac and Windows cross platform programmer. Ruby lover.

    Quote of the Day
    12/20: Mario F.:I never was, am not, and never will be, one to shut up in the face of something I think is fundamentally wrong.

    Amen brother!

  5. #5
    Registered User
    Join Date
    Jan 2008
    Posts
    19
    Thank you! Making the file in advanced solved the problem. I used notepad and choose encoding Unicode, instead of ASCII.

  6. #6
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,892
    That doesn't matter, if the file is empty.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Newbie homework help
    By fossage in forum C Programming
    Replies: 3
    Last Post: 04-30-2009, 04:27 PM
  2. Need Help Fixing My C Program. Deals with File I/O
    By Matus in forum C Programming
    Replies: 7
    Last Post: 04-29-2008, 07:51 PM
  3. Inventory records
    By jsbeckton in forum C Programming
    Replies: 23
    Last Post: 06-28-2007, 04:14 AM
  4. Replies: 3
    Last Post: 03-04-2005, 01:46 PM
  5. Possible circular definition with singleton objects
    By techrolla in forum C++ Programming
    Replies: 3
    Last Post: 12-26-2004, 09:46 AM

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