I wrote a program and created an object with fstream.
Eg.

fstream f1;
f1.open("s.dat",ios::out|ios::binary);
f1.close();

Generally if there's no file such as s.dat, then it creates a file by the name but it it's creating one instead it exits the program. I have to then manually create a file.
Why is it happening so and is there any way that it creates a file automatically if it's not suppose to creating it by itself.

What happens if you do this?

ofstream f1("s.dat", ios::binary);
//... output to file here
f1.close();


Generally if there's no file such as s.dat, then it creates a file by the name but it it's creating one instead it exits the program. I have to then manually create a file.
Why is it happening so and is there any way that it creates a file automatically if it's not suppose to creating it by itself.

That doesn't make any sense. Are you complaining because it DOES create the file, or that it doesn't. It created the file when I ran your program using vc++ 2010 express, which is just what it is supposed to do.

That doesn't make any sense. Are you complaining because it DOES create the file, or that it doesn't. It created the file when I ran your program using vc++ 2010 express, which is just what it is supposed to do.

I am complaining that it does not create a file, I have to manually create a file s.dat so that the program can run.

what compiler and operating system?

what compiler and operating system?

MS Visual C++ Express 2010 on Windows 7.

Are you looking in the debug subdirectory of the project?

Are you looking in the debug subdirectory of the project?

No.

No its not in the debug sub directory or no you are not looking there?

Try coding it this way:

//Include Library
#include <fstream>

You have to remember if you dont stream information to the file its not going to create the file. So try:

//We want to write to the file so I used 'ofstream' if you want to read from the file
//you would use 'ifstream'.

ofstream ofile( "s.dat" ); //Stream out to file
	if ( ofile.fail())
	{
		cout << "Failed Opening File!";
		exit(1);
	}

if the ofile.fail() code confuses you try:

ofstream ofile( "s.dat" ); //Stream out to file
ofile << "You just wrote to this file" << endl;
ofile.close()

Now open the new file, also a heads up, instead of s.dat, try using s.txt, its less of a hassle when opening in notepad.

Try coding it this way:

//Include Library
#include <fstream>

You have to remember if you dont stream information to the file its not going to create the file. So try:

//We want to write to the file so I used 'ofstream' if you want to read from the file
//you would use 'ifstream'.

ofstream ofile( "s.dat" ); //Stream out to file
	if ( ofile.fail())
	{
		cout << "Failed Opening File!";
		exit(1);
	}

if the ofile.fail() code confuses you try:

ofstream ofile( "s.dat" ); //Stream out to file
ofile << "You just wrote to this file" << endl;
ofile.close()

Now open the new file, also a heads up, instead of s.dat, try using s.txt, its less of a hassle when opening in notepad.

You didn't understand my problem, I want the file s.dat to be created on it's own when the program runs, but it's being not, I have to manually create a file s.dat to make the program work.

I know you want a binary file but just to make sure it's going where you think it is write out a text file with a particular name and find it in your file structure. Either that or try to write something into the binary file before you close it, there may be a mechanism in place not to create it if empty.

thats basically what i told him, but he's not getting it, =\

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.