Hi guys. So I was trying to take some names from a file and sort them but when I try to get the lines from the original .txt my string n_name comes empty. Can someone help pls?
The short answer is that line 16 is trying to read a file that is not open.
On line 11 you get user input for the variable "loc" on line 12 you set loc_f equal to "loc" plus a file name. Then on line 14 you use "loc" to open the file when you should have used "loc_f", so you never open the file. Line 16 does not work because there is no open file, so "n_name" does not receive a value.
I find it useful to check that the file has been opened before proceeding with the program. I like to use this code, but there are other ways:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
std::ifstream inFile;
inFile.open(iFileName); // iFileName would compare to your loc_f. I define and initialize this variable
// at the beginning of main or the function s it can be easily changed.
if (inFile.is_open())
{
// When working you can comment out these lines.
std::cout << "\n File " << iFileName << " is open" << std::endl;
std::this_thread::sleep_for(std::chrono::seconds(3)); // Needs the header files chrono and thread.
}
else
{
std::cout << "\n File " << iFileName << " did not open" << std::endl;
std::this_thread::sleep_for(std::chrono::seconds(3));
exit(1); // Exit the program because nothing can be done.
}
Once you know that the file is open things should work better.
You can work "iFileName" to be a "path + fileName" combination.
I will also mention your use of the "_" in variable names and your file name is acceptable, but not considered the best way of writing names. As soon as I can find a link to better explain this I will post it.
have a check in your program that the file has been opened successfully loc_f = loc + "_sorted.txt";
loc should have a file extension already at it's end, so while not strictly necessary to keep things clean removed that before the assignment to loc_f
thank you both for answering.
loc is a direction to a file, like "...//names", and loc_f is the name of the new file that will have the names sorted, so I still don't understand why it is not open... do I have to put the file extension? it doesn't work like I wrote on the exemple??
And pls post that link :)
This is a place to start, but not quite what I wanted. I have been reading for the last year so sometimes it is hard to remember where I read something.
You can always do a search here. I tried "naming variables" for the above links.
I still don't understand why it is not open... do I have to put the file extension
yes, for e.g. in windows 10 if I have a file test1.txt in the F folder, my code for direct initialization of the std::ifstream object would would be:
std::ifstream inFile{"F:\\test1.txt"};
I suppose you tried running the if(in_stream) test suggested and that's how you found out your file was not opening? the only thing i can think of is the std::string loc is not matching the full file-name such as described above.