Issues with fstream (and a hiccup with iostream)

I'd like to apologize in advance if this is a stupid question. I'm a bit of a beginner, giving CLion trial a try after using Code::Blocks for a while, and I'm having some issues getting started.

I'm trying to use fstream to i/o using files, but CLion seems to be having some trouble accessing them or something along those lines. Here's a sample program:

#include <fstream>
#include <iostream>

using namespace std;

ifstream fin("anarhie.in");
ofstream fout("anarhie.out");

int main() {
int x;
cin >> x;
cout << "output: " << x * 100 << endl;
fin >> x;
cout << x + 15 << endl;
fout << x * 2 << endl;
return 0;
}

If I input "2" in the console and "10" in anarhie.in, I'd expect to get "output: 200", followed by "25" in the console, as well as "20" in anarhie.out. However, I get

2
output: 200
17

in the console, which shows both that the input gets outputted again immediately, and that the value of x from anarhie.in was never read, remaining the initial "2". Additionally, nothing is ever printed to anarhie.out.

 

I'm probably just doing something really wrong, but I can't figure out what. I could appreciate a helping hand.

Thanks a lot!

0
Avatar
Permanently deleted user

Alright, well, I guess I couldn't really find any fix on this for now. A shame, since I guess my trial's going to run out soon. Oh well, maybe I can still find a solution eventually.

It was worth a try, though.

0

Hi! Sorry for the delay. Please check the result of stream input/output operations. It looks like they fail in your case (possible reason might be that they are not in the working dir of the app. It can be changed via Edit Configurations | Working directory).

1
Avatar
Permanently deleted user

Yes, thank you! Apparently CLion doesn't set a work directory by itself, while Code::Blocks sets it as the project directory by default, which was what got me confused.

I don't suppose there's a way to make it default to the project directory, is there?

 

I still don't know what the deal with iostream is - and why it echoes my input like that. I'd be glad to find out more about that, but I'm very thankful that I got a solution to the main issue at hand.

Thanks again!

0

请先登录再写评论。