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.
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 to 0
because it uses integer arithmetics.Here is a modified version:
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main() {
FILE *fp = fopen("taula.txt", "w");
if (fp == NULL) {
fprintf(stderr, "cannot open taula.txt: %s\n", strerror(errno));
return 1;
}
for (double j = -100; j < 100; j += 1) {
double i = j / 100000.;
double i2 = i * i;
fprintf(fp, "%f %.14f\n", i, -i2*i2/840. + i2/30. - 1./3.);
}
fclose(fp);
return 0;
}