CLion C++ can't read/open .txt file in project directory
if inputFile.is_open()
always returns false
, inputFile.open("twoday.txt");
is not opening the file correctly, presumably because it can't find "twoday.txt"
Try setting an explicit path like "c:/path/twoday.txt"
or "/path/twoday.txt"
if you're using Linux. You could also try writing a file instead to see where it shows up, or something more exotic to return the current path.
Trying to open a file in C++, but the file cannot be found
If you are using fopen
or something similar and just passing "data.txt"
, it is assumed that that file is in the current working directory of the running program (the one you just compiled).
So, either
Give a full path instead, like
fopen("/full/path/to/data.txt")
, where you use the actual full path(not preferable), Move data.txt to the directory where CLion runs its compiled programs from.
(for #2, here's a hacky way to get that directory)
char buf[1024]; // hack, but fine for this
printf("%s\n", getcwd(buf, 1024));
Can't open a file with unicode chars in the file name using CLion
I have figured out the issue. My hunch was correct. CLion appears to be providing unicode as input to the program. Using the Windows run dialog and passing it as a parameter to my program, I was able to open and process the file without an issue.
C++ project can't read file when building with CMake
As said in the comments, CMake created a subfolder in which the output file was located. The test.txt file was in the source folder and therefore out of range. When I changed the working directory to the source folder the program worked.
Thank you for the quick help.
Clion CMakeLists.txt not found when switching PC
Well, I found the issue. As far as I can tell, in version 1.0.1 there is no way to remedy the problem through the IDE.
Solution:
- Go to
projectDir/.idea
- Open
misc.xml
- Edit the field
PROJECT_DIR
to point to the directory with the project'sCMakeLists.txt
.
I think this is a bug and this field should probably be updated when choosing a new project root. A temporary solution may be to add misc.xml
to .gitignore
but I haven't tested this and don't know if this will cause other problems or if the IDE will automatically regenerate the file.
Related Topics
What Does *& Mean in a Function Parameter
Why Class Size Depend Only on Data Members and Not on Member Functions
Threadsafe Vector Class for C++
Detect When Network Cable Unplugged
How to Easily Indent Output to Ofstream
C++: Std::Move with Rvalue Reference Is Not Moving Contents
Visual Studio 2015 Code Analysis C6386 Warns of Buffer Overrun
What's the Semantically Accurate Position for the Ampersand in C++ References
How to Convert a Byte Array into Double in C
How Can Adding Code to a Loop Make It Faster
C++ -- How to Overload Operator+=
Why Are C++ Int and Long Types Both 4 Bytes
Handling Ssl_Shutdown Correctly
Factory Method Implementation - C++
How Does Excel Successfully Round Floating Point Numbers Even Though They Are Imprecise
Concurrent Writes in the Same Global Memory Location
What Are the Advantages and Disadvantages of Implementing Classes in Header Files