Opening a file

Nov 14, 2016 at 2:09am
I am trying to open and display a file and I cant figure out why it wont open the file. It just displays the exit "Fail"

1
2
3
4
5
6
7
8
9
10
11
12
13
14
   ifstream _wildeq;
    _wildeq.open("wildlifeequations.txt");
    if(!_wildeq.is_open());
    cout<<"Fail"<<endl;

    string read;
if(_wildeq.is_open())
{
while(_wildeq.good())
getline(_wldlifes, read);
cout<<read<<endl;


}
Nov 14, 2016 at 2:18am
Did you make sure the file "wildlifeequations.txt" existed?
Nov 14, 2016 at 2:22am
closed account (48T7M4Gy)
If the file exists and you can't open it then it is probably a good bet it is in the wrong directory and/or the path to it needs to be setup.

Also why test whether the file is open twice when once should be enough.
Nov 14, 2016 at 2:31am
That's the issue I'm having. It is in the correct directory.
The only reason I have it to where it checks if its open twice is due to instructions.
Nov 14, 2016 at 2:40am
closed account (48T7M4Gy)
That's the issue I'm having.

What issue?

It is in the correct directory.

Doesn't sound like it to me. That is, if your program runs.

The only reason I have it to where it checks if its open twice is due to instructions.
Try it as below and ignore the bad instructions for a while.

http://www.cplusplus.com/doc/tutorial/files/

Try the samples here as a model.

It might be a good idea if you post a short but complete piece of runnable code with sample text file of a couple of lines.
Last edited on Nov 14, 2016 at 2:42am
Nov 14, 2016 at 6:26am
closed account (48T7M4Gy)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <iostream>
#include <fstream>

using namespace std;

int main()
{
    ifstream _wildeq;
    _wildeq.open("data.txt");
    if(!_wildeq.is_open())
        cout << "Fail" << endl;
    else
    {
        string read;
        
        while(_wildeq.good() )
        {
            getline(_wildeq, read);
            cout << read;
        }
    }
    
    return 0;
}
Last edited on Nov 14, 2016 at 6:36am
Topic archived. No new replies allowed.