can't open input file

So im sure i have a few things wrong, but when I run this program it always fails at opening the file. what am i doing wrong? i mean i know that the file location is correct.





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
47
48
49
50
51
52
53
54
55
56
 #include <iostream>
#include <iomanip>
#include <fstream>

std::string playerNames[35];
int goals[35];
int assists[35];



using namespace std;


int main()
{

int gls;
int ats;
string playersName;

std::ifstream statFile;

statFile.open("C:\\Users\\DREWTOP\\Desktop\\School_Work\\CSCI\\hockey.txt");

int sub1=0;
int num;
int i;

if ( statFile.fail() )     
  {
  cout << "open for hockey.txt failed";
  exit(-1);
  }

while(statFile)
	{
	statFile>>playersName;
	statFile>>gls;
	statFile>>ats;
	playerNames[sub1]=playersName;
	goals[sub1]=gls;
	assists[sub1]=ats;

	sub1+=1;
	
	}
  statFile.close();
  
  for (i=0; i>=sub1; i++)
  {
  cout<<goals[i];
  }
  
  return 0;
}
 

Last edited on
Please edit your post and put the source inside code tags. It will make your post a lot more legible and folks here will be more likely to look at it.
i mean i know that the file location is correct.


It probably isn't.

Check it again.

It also might be a good idea to get rid of the full path and only use the "hockey.txt" file. Just make sure the hockey.txt file is in your project folder.
Last edited on
its not a project file, its just a C++ source file. the hockey.txt file is in the same folder as this programs .cpp file.
adoubek
the same folder as this programs .cpp file.


This works for Code::Blocks but not wxDev-C++. Which IDE are you using?

EDIT: Dev-C++ would be the same as wxDev-C++. They both expect to see files with relative directories in the same folder as the executable. This is because they change the PATH variable of the command shell for the instance that it is running from.
Last edited on
If you call to it locally it's from where ever the compiled executable file (.exe in windows) is, not the source file (.cpp) file.
@Mathhead200: Using relative paths has to do with the operating system. The only reason that wxDev-C++ (maybe M$VC? I'm not 100% on that) resets the path to match that of the executable is so that you don't get confused when you go to run it outside of the IDE. Otherwise the relative path is your current directory, that is the one that you are executing the compiler from or in other words the directory of your project.
Last edited on
Topic archived. No new replies allowed.