OK, so I've asked a question like this before and I thought I understood the answer (solved) but I am having problems again and I still do not understand why.

I am compiling with MinGW.

Here's the code:

    string filename="D:/__GNU analyze/test/bin/Debug/_ClusterFormat/Test2.6d .txt";
    fstream file, file2;
    string cluster="frame";
    string line;
    Matrix AAA;
    int position=0;

    file.open(filename.c_str(), ios:: in);
if (file) cout << "YES\n"; else cout << "NO\n";
    rulescheck6d(file);
    position=ClusterPosition6d(file, cluster);
    cout << "position: " << position << endl;
    file.close();

    filename="D:/_GNU analyze/test/bin/Debug/data/Test2.70cm.T01.A.6d.csv";
    file2.open(filename.c_str(), ios:: in);
if (file2) cout << "YES\n"; else cout << "NO\n";
    skipheader(file2);
    MYgetline(file2,line);
    AAA=read6dline(line, position);
    AAA.dump(6,10);
    file2.close();

I insert the if statements as debugging checks, which is why they are indented strangely (let's me find the lines to delete later).

When this code runs the first if statement returns YES, which means that "file" is open for reading. And, in fact, the functions that use this file operate properly and give the correct results.

The second if statement returns NO, which means that "file2" is not open for reading. This function that uses this file causes the program to crash.

WHY?

I originally wrote this code to reuse the variable name "file," but now I use "file" and "file2" without a difference in the outcome.
I am reusing the string "filename," but have tried this using "filename2" for the second instance without a difference in the outcome.
Yes, both files do indeed exist with the name and path as indicated. Neither file is empty.

You will see that I used exactly the same code to open both files -- file2.open(filename.c_str(), ios:: in);
But the first one opens and the second does not.

So, two questions:

Why is this happening?
How do I fix it?

Last time I asked a question like this I got more reponses to the "how do I fix it" question. Those responses worked for that particular application. But, since I still do not understand why this problem occurs, I am back again. So, please include an explanation of what is going on and what I am doing wrong. That way, I hope, I will not need to bother anyone again with this problem.

Thanks for your help.

Recommended Answers

All 5 Replies

When it answers "NO", you still fall through to process the invalid handle. You need a conditional to detect that and either emit an error message, or throw an exception.

Good advice on coding practice, but otherwise not a very helpful response.

You are telling me what I should be doing when the answer is "NO." I am asking why does it answer "NO"?

It looks to me that you have the path string wrong. In the first string you have this folder,"D:/__GNU analyze"(2 underrscore characters). But in the second string, it's this folder, "D:/_GNU analyze"(1 underscore character).

That is a mistake in the code I reported, but not in the code that I am using.

I guess I copied the code from an earlier version, but I caught that mistake and fixed it.

Still have the problem.

There must be something else in your code somewhere that is affecting this. Using your code and replicating the folder structure you're using, everything seems to work fine on 2 different compilers(VS and Cygwin). One possibility that you may or may not have thought of yet, is that the compiler might not be rebuilding when you run this. Try forcing a rebuild and see if that helps.

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.