0

So I have written a C++ project which reads a file into a string.

//func to read file into string
std::string readFile( const std::string& fileName )
{
    std::ifstream ifs( fileName );
    return std::string((std::istreambuf_iterator<char>(ifs)),
        (std::istreambuf_iterator<char>()));
}

//func call in main
std::string text = readFile("test.txt");

This is the code I use for this.
I am using CLion to write my code. I changed the standard compiler to clang++. When I run the main.cpp directly in CLion the program can't read the file. There is no error code, but when I am debugging the program it says that the parameter fileName has an incomplete type.
When I compile the main.cpp in the terminal with

clang++ -std=c++14 main.cpp -o histogram

and then run it, it can read the file and the program works as it should do.
Why does it work in the terminal but not in CLion?

OldLazarus
  • 71
  • 1
  • 6
  • 1
    "_when I am debugging the program it says that the parameter fileName has an incomplete type._" This wasn't shown in your code, but did you `#include `? – TrebledJ Dec 14 '20 at 18:41
  • Command line `clang++ -std=c++14 main.cpp -o histogram` creates the executable in the **same directory** as source file. CMake (CLion) creates an executable in **other directory**. – Tsyvarev Dec 14 '20 at 18:41
  • C++ program will read file relative to the *working directory*. Set your working directory setting in your IDE to be your source directory. – Guillaume Racicot Dec 14 '20 at 18:42

1 Answers1

1

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.

OldLazarus
  • 71
  • 1
  • 6