I was doing my school assignment on C++ and I had issues with it while running my program in Visual Studio Code. It took me a stupidly long time to fix, and my google searches and ChatGPT prompts led to no where.
So if anyone faced the same issue as me, I hope this helps!
filename="messy.txt";fstreamfile(filename,fstream::in);if(!file.fail())cout<<"it works \n";
Above is a simple C++ code snippet that reads a file named "messy.txt". If it works, the program prints out "it works ".
I had my .cpp and .exe in the same folder ad the messy.txt but the program still said it couldn't find the file.
Then I realised this only happened when I press the "play" or run button in vscode. If I did a manual g++ build command, the program can read the file when run.
Compiler issue then... I looked at launch.json inside the .vscode folder of my project (or the settings button beside the run button), and sure enough the "cwd" value was pointing to my compiler and not my project folder.
Yep so that fixed everything.
Built on Forem — the open source software that powers DEV and other inclusive communities.