.is_open() lies!

hi folks,

Right, I've been stuck on this for a couple of days now. I'm trying to load a file by dragging and dropping a file onto the .exe, extract the data & trim it, then write to another file.

The original problem is here http://www.cplusplus.com/forum/beginner/59633/

I've since reduced the code to test the read and write streams...

I've run the following code twice, once with running the .exe on it's own and once by dragging and dropping a file. Results are below the code.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
#include <iostream>
#include <fstream>

using namespace std;

int main(int argc, char *argv[])
{
    ifstream InputFile;
    ifstream InputFile2;
    ofstream OutputFile; 

    InputFile.open (argv[1]);
    cout<<"Drag'n'Drop is "<<InputFile.is_open()<<"\n";
    
    InputFile2.open ("DAP.csv");
    cout<<"DAP.csv is "<<InputFile2.is_open()<<"\n";
    
    OutputFile.open("FileWrite.txt");
    cout<<"Output file is "<<OutputFile.is_open()<<"\n";
    OutputFile<<"writting data to file \n";
    
    InputFile.close();
    InputFile2.close();
    OutputFile.close();   

    system("PAUSE");
    return EXIT_SUCCESS;
}


Open as stand alone .exe
RESULTS from cmdwindow
Drag'n'Drop is 0
DAP.csv is 1
Output file is 1
Press any key to continue . . .

output IS created and written to

------------------------------------------------------------------------

Running .exe from Drag n Drop of a .txt file
RESULTS from cmdwindow
Drag'n'Drop is 1
DAP.csv is 0
Output file is 1
Press any key to continue . . .

output IS NOT created and written to even though it's status is 1

------------------------------------------------------------------------

Can anyone shed some light or direct me to some reading material on the subject/problem?

EDIT: All the input files are in the same directory as the .exe
Last edited on
.is_open() lies!

Nope

Do yourself a favor and don't even consider that basic function like this do anything wrong. It's up to you to use them right though.

At least you followed my suggestion. And that provides a strong hint what the problem might be.

It looks very much like that drag and drop mechanism changes the current path. Try the whole thing with absolute pathes like so:

Windows:
1
2
3
    InputFile2.open ("c:\\test\\DAP.csv"); // Notice the double \
...
    OutputFile.open("c:\\test\\FileWrite.txt"); // Notice the double \ 

Or Linux
1
2
3
    InputFile2.open ("/test/DAP.csv");
...
    OutputFile.open("/test/FileWrite.txt");


You may use any other path as long as it is complete (i.e. begins with drive letter / slash)
ah thanks very much, that was driving me nuts, I didn't know about the \\ doh!

I assumed that if everything was in the same dir it would be fine

thanks again!
Topic archived. No new replies allowed.