Trouble reading from file

Hey guys, I'm trying to display the contents of multiple files but it will not open any of them.

I tried setting the path and its still not working, using Visual Studio I have the files in the same directory and added as resource files.

Any help appreciated thanks in advance.

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
#include "stdafx.h"
#include <iostream>
#include <fstream>
#include <string>

using namespace std;

string file[4];

void Load() {
	file[0] = "E\TheLightsDeceit\TheLightsDeceit\Data\Account\Account\account.txt";
	file[1] = "E\TheLightsDeceit\TheLightsDeceit\Data\Account\Characters\character1.txt";
	file[2] = "E\TheLightsDeceit\TheLightsDeceit\Data\Account\Characters\character2.txt";
	file[3] = "E\TheLightsDeceit\TheLightsDeceit\Data\Account\Characters\character3.txt";

		//Check
		ifstream files;
	for (int x = 0; x < 4; x++) {
		files.open(file[x]);
		cout << file[x] << "\n\n" << endl;

		//Error
		if (!files.is_open()) {
			cout << file << " cannot open" << endl;
		}
	}

	cout << "Load Complete..." << endl;
}

int main()
{

	Load();

}

Last edited on
There's a missing colon after the drive letter and each single backslash needs to be replaced with a double backslash, or use a single forward slash instead.

 
E\TheLightsDeceit\TheLightsDeceit\

should be
 
E:\\TheLightsDeceit\\TheLightsDeceit\\

or
 
E:/TheLightsDeceit/TheLightsDeceit/

and so on ....
The only thing that matters is relative or absolute paths.

It looks like you are trying to use an absolute path, but you left off an important piece:

E:\TheLightsDeceit\TheLightsDeceit\Data\Account\Account\account.txt"

That said, you really, really want to avoid absolute paths outside of stuff you get from the OS. There exist WinAPI functions to help you get the locations of user data.

Where do you wish to store the data for your program? User's AppData?
Thank you guys its semi working now, it only reads the file name, I want it to output the contents of the files on lines 11-14.

Id like to store the the main TheLightsDeceit folder inside c/programfiles86 but it Visual Studio wont let me put my project folder there.
It won't let you put it there because no application has any business putting user data in Program Files directories.

However, if it is just static data your program needs to operation, then store it in the same folder (or a subfolder of) your application's executable. You can get the application's executable path using the WinAPI.

So again, which kind of data are you trying to read? Read-only resources needed by your program to run? Or user data that your program would like to update?
Topic archived. No new replies allowed.