My c++ program won't open a file

Hi, I'm trying to open a file and store its data into a string to be processed and do some things, but that (still) doesn't matter now.

I've been reading lots oof posts on forums but nothing seems to be working for me. Here are the important parts of the code:

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
#include <iostream>
#include <fstream>
#include <string>

//various things

int main(int argc, char** argv)
{
	std::string elements_data;
	std::ifstream data_file("elements.txt", std::ios::in);
	bool end=false;
	if (data_file.is_open())
	{
		while (getline (data_file, elements_data))
		{
			//process
		}
	}
	else
	{
		std::cout<<"Couldn't open file \"elements.txt\"."<<std::endl;
		end=true;
	}
	data_file.close();
        //other things
	return 0;
}


No matter how I change it, it always tells me that it can't open the file.
I've tried not to put the whole path to it but it won't work, I've tried to open the file with the function data_file.open("elements.txt"); (changing the declaration not to open it twice), and I tried everything with both the whole pathname and without, and with and without the std::ios::in.

What am I doing wrong? This is driving me crazy.
Thank you in advance.
Last edited on
The code you have works fine for opening a file named element.txt that is in the working directory.

Show us how you tried it using the whole pathname.
Last edited on
I wasn't sure whether I had to use the backslash or not, so I tried both this:
std::ifstream data_file ("C:/Users/User/Desktop/Utilities/C++/Chimica/elements.txt");
and this:
std::ifstream data_file ("C:\\Users\\User\\Desktop\\Utilities\\C++\\Chimica\\elements.txt");
Because putting a single backslash would give me an error because the compiler couldn't find the corresponding character of \U, \D and so on.
Last edited on
std::ifstream data_file ("C:/Users/User/Desktop/Utilities/C++/Chimica/elements.txt");

That one is fine. Forwardslash works as a directory separator on Windows and *nix.

I know for a fact that the code is fine as I just ran it, so I can only imagine that the file doesn't exist at that location, or there's some problem reading it, or it is working and you're misinterpreting the output, or you're not running the program you think you're running.

Does windows have any trouble with addition signs in directory names? I haven't coded for windows for a long time so I have no idea what's forbidden.
Try perror to get a better error message.

http://www.cplusplus.com/reference/cstdio/perror/
Now I know what happened, and it's very stupid, but I'll leave this post both because it's funny and it may help someone else.

Everything was just fine, but the thing is that I misnamed the file: I saved the file as "elements.txt" to make sure it was in the correct extention, but windows already knew it was a .txt file and so the file was saved as "elements.txt.txt" and my program couldn't find it.
That was so dumb.

Thank you guys for your help!
Topic archived. No new replies allowed.