Really weird file I/O problem
Hey all,
I have been attempting to write the following simple program. I want to take a file that looks like this
Code:
Header stuff not needed
more useless header
5.88 3.55 1.22
6.77 8.99 2.33
* * *
* * *
and make it look like this....
Code:
5.88,3.55,1.22
6.77,8.99,2.33
* ,* ,*
* , * , *
Easy enough right? Well file 1 has 4002 lines and file 2 will have 4000 and I think this is where the problem lies. I wrote the following code.
Code:
#include <stdio.h>
#define NUMLINES 4000
int main(){
char name[20],outname[20];
double x[NUMLINES],y[NUMLINES],z[NUMLINES],dummy;
FILE *infile, *outfile;
for(int i=0;i<NUMLINES;i++){
x[i]=-999.999;
y[i]=-999.999;
z[i]=-999.999;
}
printf("name? ");
scanf("%s",name);
printf("outname? ");
scanf("%s",outname);
infile = fopen(name,"r");
outfile = fopen(outname,"w");
if(infile && outfile){
fscanf(infile,"%lf %lf %lf %lf %lf\n",&dummy,&dummy,&dummy,&dummy,&dummy);
fscanf(infile,"%lf %d\n",&dummy,(int)&dummy);
for (int i=0; i<NUMLINES;i++){
fscanf(infile,"%lf %lf %lf \n", &x[i],&y[i],&z[i]);
printf("%f %f %f",x[i],y[i],z[i]);
fprintf(outfile,"%lf,%lf,%lf\n",x[i],y[i],z[i]);
}
fclose(infile);
fclose(outfile);
}
else{printf("\n\n Something went awry\n");}
return 0;
}
But it was not giving me anything in the arrays. They were all still -999.999 when output. I could not locate the bug in this code for the life of me so I started again from scratch. I wrote this code.
Code:
#include <stdio.h>
#include <stdlib.h>
int main(void)
{
FILE *fp;
double x[4000],y[4000],z[4000];
if((fp=fopen("rest","r"))==NULL)
{
printf("Cannot open file \n");
exit(0);
}
for (int k=0;k<4000;k++)
{
fscanf(fp,"%lf %lf %lf \n", &x[k],&y[k],&z[k]);
printf("%lf %lf %lf\n",x[k],y[k],z[k]);
}
fclose(fp);
return 0;
}
and unless I only take the first 40 or so lines, I get garbage printed to the screen.....
I realize this is a simple issue but I am baffled as to what could be going on. I have had other co-workers look at the code to no avail as well. Any help someone could provide would be greatly appreciated.