Unable to create file

This is a discussion on Unable to create file within the C++ Programming forums, part of the General Programming Boards category; Why doesn't the following code create a new file Code: int main() { std::fstream file; file.open("something.bin",std::ios::binary|std::ios::in|std::ios::out); if(!outfile) std::cout<< "unable to ...

  1. #1
    Registered User
    Join Date
    Aug 2011
    Posts
    385

    Unable to create file

    Why doesn't the following code create a new file

    Code:
    int main()
    {
    	std::fstream file;
    	file.open("something.bin",std::ios::binary|std::ios::in|std::ios::out);
    	if(!outfile)
    		std::cout<< "unable to open\n";		
    }

  2. #2
    Registered User
    Join Date
    May 2010
    Posts
    2,804
    The following code:
    Code:
    file.open("something.bin",std::ios::binary|std::ios::in|std::ios::out);
    Will open a file if it exists, it will fail to open the file if it does not exist. You will need to use one of the other std::ios flags to get the file to be created, app, or trunc, depending on how whether you want to keep the data if the file exists, or erase the data.

    Jim

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Unable to create thread
    By Leojeen in forum C Programming
    Replies: 5
    Last Post: 02-17-2011, 10:06 AM
  2. Unable to create a UDP client from thread?
    By sehang in forum C Programming
    Replies: 9
    Last Post: 12-14-2010, 07:38 AM
  3. unable to create pthreads for chat ap
    By Fillis52 in forum Networking/Device Communication
    Replies: 10
    Last Post: 11-03-2010, 10:11 AM
  4. Unable to create a simple function
    By garg29om in forum C Programming
    Replies: 3
    Last Post: 02-27-2010, 04:17 PM
  5. "mi_cmd_var_create: unable to create variable object"
    By jiapei100 in forum C++ Programming
    Replies: 0
    Last Post: 01-18-2010, 03:24 AM

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