I don’t understand why is marking as identifier "FILE" is undefined
. Firstly I thought it was because of the includes, but in my code I include <stdio.h>
. Then I thought that it was just a "marking" squiggle, but when I execute in the terminal shows segmentation fault
, so I don’t know what I can do.
Here is my program:
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
int main() {
FILE *fp;
fp = fopen("taula.txt", "W");
for (double i = -0.001; i < 0.001; i += 0.00001) {
fprintf(fp, "%lf %.14lf n", i, -pow(i,4)*1/(840)+(i*i)*1/(30)-1/3);
}
fclose(fp);
return 0;
}
I’m using Visual Studio Code 1.71.2 (Universal), clang as compiler and the OS I use is macOS Monterey 12.6 in MacBook Pro with M1 Pro.
I hope someone can solve my problem.
2
Answers
I cannot say why it claims that
FILE
is undefined — on that front, your code looks fine to me, and it compiles without issues (and since you can run it, I assume it must have compiled for you too, which it wouldn’t have ifFILE
was indeed undefined).It does, however, segfault. I strongly recommend that you check that the return value of
fopen
isn’tNULL
, and, when you have found out that it is, that you read thefopen
manpage carefully, especially the section on what the legal values for the second argument are.EDIT: And the comment about constants being ints is worth listening to, even if that’s unlikely to cause segfaults.
The error reported for
FILE
seems unwarranted. Check these possibilities:stdio.h
somewhere in your include path?Note also these problems:
"w"
, not"W"
.fopen
failure, which probably happens because of the above mistake.0.00001
, which cannot be represented exactly using binary floating point representation.-pow(i,4)*1/(840)+(i*i)*1/(30)-1/3
seems incorrect:1/3
evaluates to0
because it uses integer arithmetics.Here is a modified version: