Hi,
Behold my ignorance. I've got the following code that works:
Code:
#include <stdio.h>
#include <stdlib.h>
int main(void)
{
FILE *text = fopen("/home/heras/code/text", "r");
// stuff that works as expected
But even with tons of brackets I can't get this to compile:
Code:
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
int main(void)
{
if (FILE *text = fopen("/home/heras/code/text", "r") == NULL) {
perror("fopen: ");
exit(EXIT_FAILURE);
}
Code:
## I get varying versions of the below, depending on where I put brackets:
readfile.c: In function ‘main’:
readfile.c:7: error: expected ‘)’ before ‘text’
readfile.c:7: error: expected expression before ‘==’ token
readfile.c:16: error: ‘text’ undeclared (first use in this function)
This FAQ shows a working solution, but I don't understand why mine doesn't. In another program I've got such a construction that does work:
Code:
if ((sockfd = socket(PF_INET, SOCK_STREAM, 0)) == -1) {
perror("socket: ");
exit(EXIT_FAILURE);
}
I must be making some basic syntax mistake(s) or something. What is wrong with my attempt at error handling?
Thanks,
heras