I have a code which should read an array, write it to a binary and to a text file, then print the files. However, the fprintf function returns an error and i have no idea why. This is my code :
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main()
{
FILE *f,*b;
if (f=fopen("fis.txt","w+")==NULL) {
printf ("Error\n");
exit(1);
}
if(b=fopen("binar.txt","w+b")==NULL) {
printf ("Error\n");
exit(1);
}
float *v; int n;
char s[1000];
scanf("%d",&n);
v=malloc(n*sizeof(float));
int x,y;
for (int i=0;i<=n;i++) {
scanf("%f",&v[i]);
printf("%f",v[i]);
x=fprintf(f,"%f",v[i]);
if (x<0) printf("err\n");
y=fprintf(b,"%f",v[i]);
if (y<0) printf ("err2\n");
}
fgets(s,sizeof(s),f);
puts(s);
printf("\n");
fgets(s,sizeof(s),b);
puts(s);
printf("\n");
free(v);
fclose(f);
fclose(b);
}
v[0],v[1], ...v[n-1],v[n]; butv[n]does not exist.int i=0;i<=n;i++should beint i=0;i<n;i++writing 1 value out of arrayperrorto print the error reason (based on the value oferrno); most standard library functions seterrnoon failure.