I call shenanigans :-)
Obviously, if string
were exactly the same value as your string literal, and all others things remained the same, it would work fine.
So, if all other things are the same, string
is obviously not set to the value you think it is, to wit "/home/user/desktop/bla/test"
.
My advice would be to print it out (along with an error string) to be sure, something like:
fprintf (stderr, "DEBUG: string is [%s]\n", string);
int ff = open (string, O_RDONLY);
if (ff < 0)
perror ("Could not open file");
And, now that you've posted the code, your problem is obvious: fgets()
does not remove the trailing newline so, were you to add the code I proposed above, you would see:
DEBUG: string is [/home/user/desktop/bla/test
]
and deduce the problem.
A quick way to fix this is to remove the newline yourself, with something like:
while (fgets (line, 255, fp) != NULL) {
size_t ln = strlen (line); // Add these
if ((ln > 0) && (line[ln-1] == '\n')) // three
line[ln-1] = '\0'; // lines.
bla (line);
}