Characters missing when writing in file in C -


i'm sorry if similar question has been asked, i've searched , i'm not sure how answer it's pretty specific.

i have function checks if file exists , if doesn't, create , writes parameters in it. after function adds data in file. first line of data missing number of characters in line created file.

the code looks (i'm putting parts think relevant, it's pretty messy) :

the function checks if file exists :

int check_datafile(char *fname, par *par){   char filename[64] = "data/";    strcat(filename, fname);   file* fdesc = null;    fdesc = fopen(filename, "r");   if (fdesc == null){   fdesc = fopen(filename, "w");   fprintf(fdesc,"%d %.3f \n", par -> l, par -> t);   close(fdesc);   }   return 1; } 

then function writes :

void result_block(par *par, double m, double m2, double m4, int ntot, char *fname) {   char filename[64] = "data/";    strcat(filename, fname);   file* fichier = null;    fichier = fopen(filename, "a");    if (fichier != null) // file should exist   {     fprintf(fichier, "%.3f %.3f %.3f\n", m/ntot, m2/ntot, m4/ntot);     fclose(fichier);   }   else   {     printf("problem file : %s\n", filename);     exit(0);   } } 

they called by

int initialize_mc(par *par, int *spin) {   int i, l2 = par->l * par->l;   double t = par -> t;   char *f2;   double ex[1];   ex[0] = exp(-2/t);    if (!par->l) {     printf("give system size n!\n");     return 0;   }    init_ran(par->seed);    sprintf(fname, "%3.3d_%5.3f", par->l, par->t);    check_datafile(fname, par);    mc(par, spin, ex);    return 1; } 

and function result_block called in function mc.

typically, want file :

16 2.210  205.412 43371.160 2010463301.344 201.876 42319.600 1951381846.720 198.396 40897.632 1836904396.032 197.652 40743.856 1833699088.000 ... 

and looks :

16 2.210  371.160 2010463301.344 201.876 42319.600 1951381846.720 198.396 40897.632 1836904396.032 197.652 40743.856 1833699088.000 ... 

the first line of data cut same amount of characters in first line of file.

what can cause problem? it's making jack dull boy. in advance.

close(fdesc); needs fclose(fdesc);. when use file * implicitly using buffer on output. need call fclose buffer gets flushed.

by calling close casting pointer int , closing random file descriptor (which presumably fails of time). you're not closing file * @ all.


Comments

Popular posts from this blog

angularjs - ADAL JS Angular- WebAPI add a new role claim to the token -

php - CakePHP HttpSockets send array of paramms -

node.js - Using Node without global install -