#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.
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.
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.
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.