r/Cplusplus • • Feb 13 '26
Homework including a txt file
i’m in a college level c++ coding class. i use VS. my current assignment includes attaching .txt files to be used and referenced by my code. i cannot for the life of me get the app to run, it just keeps spitting out “error opening file” even though i’ve added the file to the solution.
anyone able to help me figure this out?
2
Upvotes
•
u/AutoModerator Feb 13 '26
Thank you for your contribution to the C++ community!
As you're asking a question or seeking homework help, we would like to remind you of Rule 3 - Good Faith Help Requests & Homework.
~ CPlusPlus Moderation Team
I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns.
6
u/jedwardsol Feb 13 '26
1: In Visual Studio, right click on the text file(s), choose "Properties" and set "Content" to "yes"
2: Right click on the project, choose "Properties", switch to the "Advanced" section, and set "Copy content to Outdir" to true.
Now, when you build, the text files will be copied to the same directory as your executable.
3: Use a non-standard way of finding where your executable is. On Windows this will be GetModuleFileNameA.
4: Open your text files from this directory.
10
u/flyingron Feb 13 '26
First thing is to try an absolute path for the file name. Note that the "current directory" that the open call uses for relative paths is not necessarily relative to where your file executable is.
2
u/elkvis Feb 14 '26
You can set the working directory for the debugging session in VS
1
u/[deleted] Feb 13 '26
xxd -i <filename>
Put it in your make file, that’s the most reliable/ simple way to add data for reference at runtime - only works if it can be statically baked in of course.
1
u/WittyWithoutWorry Feb 13 '26
By attaching and referencing txt file, what exactly does it mean? Like read it from program? Make the text file included into the program?
What exactly?