Gaurav
Gaurav

Reputation: 397

fstream is not creating a file in C++

I have checked several such questions on SO like : Link 1 and Link 2

But none of their answers is helping me. After spending so many hours in debugging, I am unable to detect the bug. So, I am asking it here, again.

The code for my program is :

#include<iostream>
#include<fstream>
#include<string.h>

using namespace std;

int main(){
    ofstream file;
    file.open("data.dat",fstream::out);
    file<<fflush;
    if(!file)
        cout<<"error"<<strerror(errorno);
    file.close();
    return 0;
}

This is the main stem of the program dealing with file handling. The remaining portion of the program deals with processing some data and writing it to the file which, I think, is neither relevant nor affecting the file handling.

The interesting thing is the program is not flashing any error.

Upvotes: 2

Views: 4610

Answers (4)

TheAhmad
TheAhmad

Reputation: 940

If the file is opened, you can find its location with the help of GDB and procfs. Just put a breakpoint where the file is opened and not closed, yet. Run the program on the debugger until the breakpoint is triggered. Then use the following command:

ls -l /proc/<pid>/fd

where <pid> is the PID of the program. The complete path of the file should be somewhere in the output.

Upvotes: 0

CinchBlue
CinchBlue

Reputation: 6180

#include <iostream>
#include <fstream>
#include <string.h>
#include <errno.h>

int main (int argc, char* argv[])
{
        std::ofstream file;
        file.open("data.dat", std::fstream::out);
        file << "Hello" << std::endl;

        if (!file)
                std::cout << "error" << strerror(errno);
        file.close();
        return 0;
}

Main differences:

  • You didn't #include <errno.h>.
  • You're passing "errorno" to strerror(), not "errno," which is the correct number.

This works on Ubuntu 14.04 (64-bit). I compiled using g++ with no flags.

Also, I suggest you don't ever use using namespace std; It can be very annoying once you start integrating with other libraries, such as Boost (Boost's libraries can overlap with each C++ standard library features).

Upvotes: -1

jplatte
jplatte

Reputation: 1141

Your code generally works with small changes, the file is just created in the current working directory from which your program is ran, not in the directory the executable is in. There are a lot of other things you might want to address though:

#include <iostream>
#include <fstream>
// if including things from the C standard library in a C++ program,
// use c[header] instead of [header].h; you don't need any here though.

using namespace std;

int main()
{
    // no need to call open(), the constructor is overloaded
    // to directly open a file so this does the same thing
    ofstream file("data.dat");

    if(!file)
    {
        cout << "Couldn't open file" << endl;
        return 1;
    }

    file.close();

    // return 0; is not needed, your program will automatically
    // do this when there is no return statement
}

For detailed information on why opening the file didn't work, you could have a look at std::basic_ios::bad() and std::basic_ios::fail(). Checking errno is nothing you want to do when using C++ streams for file handling.

Upvotes: 5

codekiddy
codekiddy

Reputation: 6137

int main(){
    ofstream file;
    file.open("data.dat") // no need to call for ofstream ->fstream::out
    // file<<fflush; fflush won't work since there is nothing unwriten
    if(!file.is_open()) // use is_open();
        cout<<"error"<<strerror(errorno);
    // write something to file
    file << " ";
    file.close();
    return 0;
}

Upvotes: 0

Related Questions