Maurice
Maurice

Reputation: 25

Why won't ifstream open this file?

#include <iostream>
#include <fstream>

using namespace std;

#define BRIGHTNESS_FILE "/sys/class/backlight/radeon_b10/brightness"

int main()
{
     ifstream brightness_file("BRIGHTNESS_FILE");
     int a;
     brightness_file >> a;
     cout << a;
}

I've checked the path and permissions for the file. I am at a loss on why it won't read from it.

EDIT

I fixed the whole define thing with BRIGHTNESS_FILE, but it still won't open. I've checked the path multiple times just to be safe.

Upvotes: 1

Views: 307

Answers (2)

Mateusz Grzejek
Mateusz Grzejek

Reputation: 12118

You define BRIGHTNESS_FILE as constant string literal:

#define BRIGHTNESS_FILE "/sys/class/backlight/radeon_b10/brightness"

But don't use it at all:

ifstream brightness_file("BRIGHTNESS_FILE");

"BRIGHTNESS_FILE" is also a string literal - it has nothing to do with your macro. That's why its content won't be replaced by preprocessor.

What you need is:

ifstream brightness_file(BRIGHTNESS_FILE);

Now, BRIGHTNESS_FILE will be changed to "/sys/class/backlight/radeon_b10/brightness" and your file should properly opened (if it exists and is accessible, of course).

Upvotes: 5

Benjamin Lindley
Benjamin Lindley

Reputation: 103751

I assume you don't have a file named "BRIGHTNESS_FILE". Because that's the file name you are trying to open. Remove the quotation marks in order to use your macro-defined string.

ifstream brightness_file(BRIGHTNESS_FILE);

Upvotes: 5

Related Questions