2

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.

Community
  • 1
  • 1
Gaurav
  • 398
  • 8
  • 23
  • 4
    Maybe the file is created, but not in the location you expect it to? – Some programmer dude Mar 03 '15 at 14:03
  • Isn't the file supposed to be created in the same folder in which the program exists? – Gaurav Mar 03 '15 at 14:03
  • 2
    If you run the program from a command line then the file is created in the current directory. If you run it from an IDE then the current directory is set in the project settings. – Some programmer dude Mar 03 '15 at 14:06
  • Your program works just fine when replacing `errorno` with `errno` and including `` or ``. The file is supposed to be created in the folder from which the program is executed. – jplatte Mar 03 '15 at 14:06
  • 1
    @Gaurav, there is no guarantee that iostreams set `errno` on error. And the file is created in the current working directory, which isn't necessarily the directory where the program lives (if you run `/bin/ls` it doesn't default to showing you the contents of `/bin`!) – Jonathan Wakely Mar 03 '15 at 14:07
  • Also, `fstream::out` is redundant when using `ofstream`, because `ofstream` defaults to output anyway. – Jonathan Wakely Mar 03 '15 at 14:08
  • 2
    There are some things wrong about it though: You shouldn't write fflush to the file. It's a function from the C library that you use as `fflush(FILEstruct);`. When executing your code, it simply wrote a 1 to the file. Also you shouldn't write to the file unconditionally. Check whether the file could be opened first, then if that's the case write to the file and also only close it in that case. – jplatte Mar 03 '15 at 14:09
  • @JoachimPileborg: @jPlatte: @JonathanWakely: Thank you all for your kind suggestions. I forgot to mention the header file for `errno`. This time I entered a weird name for the file and sought help of `search` facility for Windows 8.1. I was amazed to see that the files were being saved at a location `C:\VTRoot\HarddiskVolume8\...`. The so called bug has been found. – Gaurav Mar 03 '15 at 14:36
  • @Gaurav It seems that you are using an IDE that has quite special paths for build directories or creates seperate directories to run the program in. Usually, IDE's just run the program from the build directory, which usually is a subdirectory of your project directory. Which IDE are you using? – jplatte Mar 03 '15 at 14:41
  • @jPlatte, Yes, you are right. I am using CodeBlocks 13.12. After getting this issue, I have just reinstalled the software so as to refresh any garbage path. And it irritates me when I see that it is still saving the files on the same older path !!!! – Gaurav Mar 03 '15 at 14:48
  • 1
    @Gaurav Well, uninstalling doesn't necessarily mean removing configuration files. In any case, ask your favorite search engine about Code::Blocks build paths instead of just reinstalling it if you want to change this behaviour. – jplatte Mar 03 '15 at 14:51

4 Answers4

5

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.

jplatte
  • 1,121
  • 11
  • 21
0
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;
}
codekiddy
  • 5,897
  • 9
  • 50
  • 80
0

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.

TheAhmad
  • 810
  • 1
  • 9
  • 21
-1
#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).

CinchBlue
  • 6,046
  • 1
  • 27
  • 58
  • Well, you "fixed" the namespace thing, but your program is as broken as the original one which didn't include the errno header. You fixed the missing errno include, but you're missing `` for `std::fflush` or `` for `fflush` in the global namespace. I'm aware that this does compile, but you're relying on the compiler including a header that defines `fflush` in the `std` namespace as part of one of the other headers included and that's no good thing. – jplatte Mar 03 '15 at 14:47
  • I would also recommend you to compile with `-Wall -Wextra -pedantic`, it will tell you that `file << std::fflush` is not a good idea and that there is no reason to use `int main (int argc, char* argv[])` instead of `int main()` when you don't use the command line arguments in any way. – jplatte Mar 03 '15 at 14:49
  • By the way, std::flush is defined in , so I don't get what's wrong about it? I also just like to use (int argc, char* argv[]) to remind myself of a standard implementation (so I don't start getting lazy and end up with just main()). It's also good to remind myself that I can create command line apps and it also educates others that it's possible as well. – CinchBlue Mar 03 '15 at 15:12
  • Where did you get the information that `std::fflush` is defined in ``? – jplatte Mar 03 '15 at 16:12