I haven't needed to use fscanf before, so I'm not very familiar with it... Everything is working fine except that the numbers it inputs are not what's in the file and they are all the same number (-9.255959e+061 to be specific) I know exactly how the files are formatted they are opening and everything... so I'm not sure what's wrong

#include <math.h>
#include <stdio.h>
#include <iostream>

int main ()
{
  char fname [80];
  const int num=16;
  double ke[50],tbin[50],ket1[50],ket2[50],tbin1[50],tbin2[50],k,t;
  int i,j,extra;
  FILE * fin;
  FILE * fout1;
  FILE * fout2;
  for(i=0;i<50;i++){ket1[i]=0.;ket2[i]=0;tbin1[i]=0;tbin2[i]=0;ke[i]=0;tbin[i]=0;}
  fout1=fopen("avgke1.txt","w");
  fout2=fopen("aveke2.txt","w");
  for (i=0;i<num;i++){
  sprintf(fname,"avgkei_100_1.5e-07_1_1.00_%i.txt",i);
  fin = fopen (fname,"r");
  printf("file open");
  if (fin!=NULL){
  for (j=0;j<49;j++){
	  fscanf(fin,"%e %e %i",&t,&k,&extra);
	  ke[j]=k;
	  tbin[j]=t;
  printf("%e %e\n",tbin[j],ke[j]);
  //getchar();
  if(i<(num/2)){
	  ket1[j]+=ke[j];
	  tbin1[j]+=tbin[j];
  }
  else{
	  ket2[j]+=ke[j];
	  tbin2[j]+=tbin[j];
  }
  }
 
  fclose (fin);
  }
  } 
  for(i=0;i<50;i++){
	  ket1[i]=ket1[i]/(num/2);
	  tbin1[i]=tbin1[i]/(num/2);
	  ket2[i]=ket2[i]/(num/2);
	  tbin2[i]=tbin2[i]/(num/2);
	  fprintf(fout1,"%13.6e %13.6e\n",tbin1[i],ket1[i]);
	  fprintf(fout2,"%13.6e %13.6e\n",tbin2[i],ket2[i]);
  }

  fclose(fout1);
  fclose(fout2);
}

Recommended Answers

All 3 Replies

Can you post a couple of lines of your data file? Your code resembles C more than C++ (other than the fact that you included iostream).

I got it fixed, turns out I needed to use %le instead of %e in the fscanf...

Great! I was going to suggest something to that effect, but I had wanted to verify it with your file first.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.