Ifstream issues

I am getting back into coding after a long break. Every time I run this code, the else block is always running. Yes, I have the correct pathing to the file, I tested it by writing to the file, and verified the file was indeed updated. What is going on here?

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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
#include <iostream>
#include <stdlib.h>
#include <stdio.h>
#include <time.h>
#include <windows.h>
#include <fstream>
#include <stdexcept>
using std::ifstream;
using std::ofstream;
using std::cin;
using std::cout;
using std::endl;
using std::string;
using std::invalid_argument;


struct Difficulty
{
    string difficultyName;
    int lowerRange;
    int upperRange;
};

void grab_difficulties()
{
    ifstream file("Game Data/Difficulties.txt");

    if (file.is_open())
    {

    }
    else
    {
        throw invalid_argument("Could not find file 'Difficulties.txt'.");
    }
}


int main()
{
    system("Color 1e");
    SetConsoleTitle("Number Guess 2000");
    grab_difficulties();
    return 0;
}

It seems to work for me?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
void grab_difficulties()
{
    ifstream file("Difficulties.txt");

    if (file.is_open())
    {
        cout << "FILE OPEN" << endl;
    }
    else
    {
        throw invalid_argument("Could not find file 'Difficulties.txt'.");
    }
}


int main()
{

    grab_difficulties();
    return 0;
}

Last edited on
It throws an error everytime for me. What the french toast! Lol.
1
2
3
4
5
6
7
8
terminate called after throwing an instance of 'std::invalid_argument'
  what():  Could not find file 'Difficulties.txt'.

This application has requested the Runtime to terminate it in an unusual way.
Please contact the application's support team for more information.

Process returned 3 (0x3)   execution time : 1.665 s
Press any key to continue. 
Anyone?
You marked this thread as solved, do you still need help or not?
Do you know that there are other reasons for the file to fail to open properly, other than not finding the file? Perhaps you should ask your system for more information as to why the open failed. Try using perror() to help determine the cause of the failure. http://www.cplusplus.com/reference/cstdio/perror/

Topic archived. No new replies allowed.