0

I have the following code in VS:

#include <iostream>
#include <fstream>

using namespace std;

int main() {
    ifstream inData;
    int temp;

    inData.open("test_file.txt");

    do {
        inData >> temp;
        cout << temp << " ";
    } while (!(inData.eof()));

    inData.close();


    return 0;
}

test_file.txt contains the following data (it's a single line):

1 2 3 4 5 6 7 8 9 0 10 11

When I built and ran this in VS, it worked fine. Today, when I tried to run it on CLion, it gives me garbage data. I tried creating a new project from scratch in CLion and copy-pasting just the code in main.cpp, but still gives me random data. And now I'm wasting my Saturday wrestling with my IDE instead of just finishing my homework. Any help?

  • 2
    First of all, are you sure that the program manages to open the file? And while your loop might be marginally better than `while (!infile.eof())` [using `eof()` in a loop condition should always be considered wrong](https://stackoverflow.com/questions/5605125/why-is-iostreameof-inside-a-loop-condition-i-e-while-stream-eof-cons). – Some programmer dude Nov 13 '21 at 17:58
  • 1
    Maybe the file is not even found. Your code does not care if the file was opened or not. – drescherjm Nov 13 '21 at 17:59
  • Thanks. I just figured out what the problem is, CLion doesn't recognize the path, VS does. – extDependency Nov 13 '21 at 17:59
  • 4
    The problem is very likely that the [working directory](https://en.wikipedia.org/wiki/Working_directory) differs between the two environments, meaning the relative path to the file is different. Consider to *always* set the working directory in the project run settings. – Some programmer dude Nov 13 '21 at 17:59
  • 1
    You should put some error handling in your code. – drescherjm Nov 13 '21 at 18:00
  • Will do. My project is much bigger than this, but I was just testing the very beginning: TDD. – extDependency Nov 13 '21 at 18:01

1 Answers1

0

I figured out what the issue is. CLion doesn't recognize the parameter as a path, whereas VS does.

  • 1
    IDEs are not compilers. CLion is not the issue. It's the compiler that it uses that interprets your code differently: most likely gcc or clang. Where as Visual Studio ships the Microsoft compiler. And when you say, "doesn't recognize the path", that leads me to believe it's really a Linux vs Windows issue. – selbie Nov 13 '21 at 18:01