I have code that processes a txt file word by word and hashes the input.
However I cant even get my code to open the txt file. I have used both eclipse and visual studio, I made sure the txt file is within the same folder as the other files and in VS I added it to the project. For some reason it works only on visual studio and only with 1 of my 4 files. I think this 1 file started working after I copied the contents to another file for some reason. I recive no errors or warning when building but I keep getting the eroor "Error opening file." in eclipse and in VS it just crashes.
#include <iostream>
#include <fstream>
#include <string>
#include <cctype>
#include <sstream>
#include "HashType - Copy.h"
using namespace std;
int main() {
for (int b : {33, 37, 39, 41}) {
ifstream inFile("hashText1.txt");
if (!inFile) {
cerr << "Error opening file." << endl;
return 1;
}
int primeSize;
string line;
if (getline(inFile, line)) {
stringstream ss(line);
ss >> primeSize;
}
HashType<string> hashTable(primeSize);
buildHashTable(inFile, hashTable, 1, b);
}
return 0;
}
As I mentioned it works only with 1 file and only in VS but not at all with any other files or at all in eclipse!
2
Answers
This question is so, so very common.
The best way to fix this is…
…to pass the filename as argument to your program. Then tell your IDE to give the full file path as argument when it runs your program. You can do this in all IDEs. In MSVS, it’s at Project → Settings → Debug → Program Arguments (last I checked).
quux.cpp
Another (less awesome) way to fix it…
…is to hard-code the full path. This kind of solution keeps professors and TAs up at night having to modify your program to simply work.
Rationale
In order to be clear, “less awesome” is programmer-speak for “it works, but don’t do this if you value a reputation as someone who knows what they are doing”. I mention it because it is a quick-n-easy kludge for dealing with something before you put in the proper code to do it right.
The “best way” is yet more programmer-speak to say “do it this way because <good reasons>”.
Those reasons may very well be opinion, but there are actual reasons involved, not just ego.
Reason 1 • It is expected behavior
Let us take the GNU Image Manipulation Program as an example. GIMP is a program that operates on image files. Load ’em, edit ’em, save ’em.
Notice how when you are scrolling around using your file explorer and right-click on an image, one of the options is to “Open this file with GIMP”? How does your file explorer actually do that?
Easy. It calls the gimp executable with the image filename as argument.
You can even drag an image file and drop it on the GIMP executable and it will open GIMP with the image already loaded!
From the command shell prompt, you can type
gimp cats.jpg
and it will start GIMP and load “cats.jpg” ready for editing.Amazing!
Reason 2 • It makes life easier for everyone
By moving the name of the file to open into the user’s hands, your program becomes infinitely more useful.
The point is, it moves control into the hands of both you (the programmer) and other users of your program (such as the TA grading your assignment, who will grade you much more nicely when your program is Easy To Use™).
Reason 3 • People won’t despise your program (or you)
This is kind of an et sequitur bit, but when people download software that is difficult to use… they toss it. They tell all their friends and internet acquaintances. They’ll send you hate email about how much time they wasted on your program. Your program won’t just be forgotten, it’ll burn in hell. And any other software you write will have the soot stains on it, regardless of quality.
Reason 4 • It is super-mega-ultra-über-easy
Seriously. How hard is it to type:
Then just use your file explorer to find the input file, Ctrl–C, then open your IDE’s “run program with arguments” option and Ctrl–V.
Done! Program runs from the IDE easy-peasy!
Hopefully this elucidates things a bit. Programming is often about being picky about little things that have big impact and taking the time to set things up to make life easier later.
/end rando lecture
I’m honestly not sure if this should be marked as a duplicate or a low effort question. But Visual Studio and Eclipse use build folders to dump the executables/libraries in. And you have to remember: When you launch a program and are asking for the file, it is looking for the file relative to the executable.
For example, say I have the following folder structure:
And you launch that within Visual Studio it will create build folders that will make your project directory look something like this:
And inside the x64 Folder it will have either
Debug
orRelease
or any other configuration you configured the project in. And inside of thoseDebug
orRelease
folders will contain your executable,MyProgram.exe
.This is where the text file needs to be since the application is launching itself from the
Debug
orRelease
directory. And since you programmed the project to look for a file namedhashText1.txt
it will look for that file, literally, right next to the executable.If you truly want to search for the file in your source tree then you will need to hard code the path to your file as @Dúthomhas has suggested. Or add it as a resource as they also suggested as well.
I still want to emphasize that the program is still executing what you are telling it to do. But it will always look relative to it’s executable if you only pass it a filename without a path.