fstream Problem

Hello, I am very new to c++ and all I am trying to do is use the fstream to open a .dat file and interpret the 14 different lengths to the program. The problem is, it won't open the file. What am I doing wrong? I've looked in my books - I've tried different permutations - nothing is working. Can I get some help?

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
#include <iostream>
#include <fstream>
#include <string>
using namespace std;

int main()
{
	ifstream inFile ("C:lengths.dat");
	int length1 ;
	int length2 ;
	int length3 ;
	int length4 ;
	int length5 ;
	int length6 ;
	int length7 ;
	int length8 ;
	int length9 ;
	int length10 ;
	int length11 ;
	int length12 ;
	int length13 ;
	int length14 ;
	
	if (!inFile)
	{
		cout << "Can't find the file, dude. My bad.";
		cin.get();
		cin.get();
		return 1;
	}

	inFile >> length1 >> length2 >> length3 >> length4 >> length5 >> length6 >> length7 >> length8 >> length9 >> length10 >> length11 >> length12 >> length13 >> length14;

		cout << "The seconds: " << endl;
		cout << length1 << endl;
		cout << length2 << endl;
		cout << length3 << endl;
		cout << length4 << endl;
		cout << length5 << endl;
		cout << length6 << endl;
		cout << length7 << endl;
		cout << length8 << endl;
		cout << length9 << endl;
		cout << length10 << endl;
		cout << length11 << endl;
		cout << length12 << endl;
		cout << length13 << endl;
		cout << length14 << endl;
	
	inFile.close() ;
	cin.get();
	cin.get();
	return 0 ;
}


"C:lengths.dat"

Perhaps you meant "C:/lengths.dat"? Is that the location of the file? At the root of the C: drive?
That didn't work either. It's in a regular Document folder. But, with the slash, I still have the same problem.
Then you need the entire path:
"c:/documents and settings/user/my documents/etc"


Also, something like this: ifstream in("input.txt"); will either point to the folder with the .cpp opening the folder or the folder that the .exe is (depending upon if you're doing a compile-run, or just run).
Your files are arranged on your hard drive in directories. You might know these as folders. You need to write in the complete name of the directory containing your file. You can discover this by using your file explorer. For example, it might be something like:

"C:/Users/someUser/Documents/lengths.dat"
Last edited on
Oh alright - I finally got it! Thank you guys! :)
Topic archived. No new replies allowed.