• Advertisement
Sign in to follow this  

why is the code failing?

This topic is 3633 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I am using C++ fstream for file i/o. Trying to read a binary file...
#include <iostream>
#include <fstream>
#include <string>
using namespace std;

int main()
{
    fstream fin;
    string fName = "data.qfx";
    fin.open(fName.c_str(), ios::in | ios::binary)
    if(!fin.is_open())
        cout << "File failed to open.";
    int age;
    fin >> age;
    // etc.
    fin.close()
}

The code compiles, but the file never opens (I get the 'File failed...' message). The file 'data.qfx' is in the same directory as the executable. I am at a loss here...

Share this post


Link to post
Share on other sites
Advertisement
Maybe this will help out...

fin.fail() returns true, but fin.bad() returns false. This means that only the failbit is being set.

According to cplusplus.com, failbit usually gets set when there is "an internal logic problem with the input operation itself." But this description means nothing to me. Hopefully this helps anybody out...

Thanks,
ply

Share this post


Link to post
Share on other sites
The file needs to be in the current path, the directory the exe is in isn't one of them unless you run the exe from explorer.

Either move the file into your solution directory, or change the working directory in your solution properties to the directory the exe is in.

Share this post


Link to post
Share on other sites
Evil Steve,

I appreciate you helping me out here, but the terms:
(1) current path
(2) solution directory
(3) working directory

Just graze on past my little peanut brain. haha. What do you mean by these terms? thanks again

Share this post


Link to post
Share on other sites
A little basic cleanup...

1. using namespace std is ok here, but I always try to avoid it, even in simple programs.

2. You can use the ifstream and ofstream classes when you only intend to perform input or output (but not both).

3. fName is only used to provide open() with its 'file path' argument, so we can just skip it (unless you're using it in the // etc. section).

4. You can open the file by invoking the appropriate constructor; you don't need to call open() separately.

5. Stream classes offer an (indirect) implicit conversion to a Boolean value, so you can just write 'if (!fin)' (and, I believe, it's idiomatic to do so).

6. The file stream classes will close themselves when they go out of scope, so you don't have to do it manually.

7. If the file fails to open, you won't want the code that follows to execute (I just added a return statement here, but you might prefer to do something else).

8. At least in what you've shown you're only performing formatted input, so I'm not sure that you want the binary flag here.

The resulting code:

#include <iostream>
#include <fstream>

int main()
{
std::ifstream fin("data.qfx");
if (!fin) {
std::cout << "File failed to open." << std::endl;
return 1;
}
int age;
fin >> age;
}

As for your actual question, I don't know :) Are you sure the file name/case is right? What OS are you using (Linux, Windows, etc.)? How are you launching the program?

Share this post


Link to post
Share on other sites
First, lets rewrite that:

include <iostream>
#include <fstream>
#include <string>

using namespace std;

int main()
{
string fName = "data.qfx";
ifstream fin(fName.c_str(),ios::binary)

if(!fin)
{
cout << "File failed to open.";
return 1;
}

int age;
fin >> age;
// etc.
}




The main changes here are:
0) Use an ifstream for input. Use ofstream for output. You rarely need to use fstream itself.
1) Open the file in the constructor call. No need to write two lines and risk forgetting to open the file.
2) To properly handle the error, you should write your code such that the error handling code and the normal code are mutually exclusive. Here, I used a simple return statement. Alternatively, putting the reading code in an "else" block would have worked.
3) C++ file streams close themselves.

As for your problem, the programs directory can change. IDEs often change the working directory of the executable in order to allow for having different build configurations. If you use the system command line it should work. For example, on windows, start->run (type "cmd") and use the "cd" command to change to the executable directory: "cd C:\path\to\program". Then run the program: example.exe. Your code should succeed.

A simple way to discover your working directory might be (again, on windows) using the cd command in your program with no arguments:
std::system("cd");
This will print a path.
On linux:
std::system("pwd");

Note: this is a quick hack to show you where to move the file. Don't use system() in your own program if you can avoid it.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement