Clion C++ Can't Read/Open .Txt File in Project Directory

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

  1. Give a full path instead, like fopen("/full/path/to/data.txt"), where you use the actual full path

  2. (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's CMakeLists.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



Leave a reply



Submit