You are here:

C/Help!!!!

Advertisement


Question
Dear Narendra:
That's me again. In my program if I delete the /** NOTE 1 **/  or I delete  /** NOTE 2 **/ (close file), the program runs ok. I don't know why this happend. I'm sending you my code and the imput file. Thanks in advance.

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <math.h>


#define AllocMem(a, n, t)  a = (t *) malloc ((n) * sizeof (t))

#define PI 3.14159265358979323

typedef struct {double x, y, z;} VecR;
typedef struct {int x, y, z;} VecI;

typedef struct {
 VecI box;
 VecR r, rv, ra;
 int type,Nstep;
 long int step;
} Mol;


int main()
{ Mol *mol;
  double Matrix[3][3];
  int Winding;
  int   id, type;
  double xlo, xhi, ylo, yhi, zlo, zhi, Lx = 0.0, Ly = 0.0, Lz = 0.0;
  int bx ,by, bz;
  double xp,yp,zp;
  char char_TimeStep[80] = " ITEM: TIMESTEP\n";
  char char_Natoms[80] = " ITEM: NUMBER OF ATOMS\n";
  char char_Box[80] = " ITEM: BOX BOUNDS\n";
  char char_Atoms[80] = " ITEM: ATOMS\n";
  char string_temp[80] ;
  long int  Nrigth, Nleft;
  int counter, NSTEP = 0, i=0,i2=0;
  long int Tempstep=0;                          
  long int Tinitial=6770000, deltaT=1, Tfinal=6774000; /** initial timestep : delta T: final timestep  */
  FILE *file3;
  FILE *file1, *file2;
  
  long int size, No=0, Nm = 0 ,Nc =0, natoms=10000;
  long int Model=0;
  char AtomType[]={'T','R','C','S','\0'};

/** **************************************************************************************/
/** **************************************************************************************/
 if ( ( file1 = fopen( "samplehistorycopy", "r" ) ) == NULL ) {
     printf( "File xxx could not be created. \n");
  return 0;
  }

  if ( ( file2 = fopen( "DNA.pdb", "w" ) ) == NULL ) {
     printf( "File xxx could not be created. \n" );
  return 0;
  }

  if ( ( file3 = fopen( "Natoms", "w" ) ) == NULL ) {
     printf( "File xxx could not be created. \n");
  return 0;
  }
/** **************************************************************************************/
/** **************************************************************************************/
          
          Nm=3;
          Nc=2;
          natoms=5;
     AllocMem (mol, natoms, Mol);
/** ***********************************************************************************************/
/** ***********************************************************************************************/
fprintf (file2,"HEADER    \n");
fprintf (file2,"REMARK    THIS IS A SIMULATION BOX\n");
fprintf (file2,";2345678901234567890123456789012345678901234567890123456789\n");

fprintf (file3,"#Static properties \n");
fprintf (file3,"#Timestep (1), Nleft (2), Nrigth (3)\n");

/** ***********************************************************************************************/
/** ***********************************************************************************************/

/** get information of history file**/
  /**  **/
   fgets(string_temp,80,file1);
   printf("Reading frame = \n");
  
   while (((!feof( file1 )) && (Tempstep <= Tfinal))) { /** feof(end)=x, feof(noend)=0**/
     do{    /** Find "Tinitial" **/

      if (strcmp (string_temp,char_TimeStep) == 0){
     fgets(string_temp,80,file1);   
          sscanf(string_temp,"%ld",&Tempstep);
            
          fgets(string_temp,80,file1);
          }

      if (strcmp (string_temp,char_Natoms) == 0) {
          fgets(string_temp,80,file1);   
         /* sscanf(string_temp,"%ld",&natoms);*/
          fgets(string_temp,80,file1);}

      if (Tinitial>Tempstep){
          for (counter=1;counter<=(natoms+4);counter++)
              {fgets(string_temp,80,file1);}
              fgets(string_temp,80,file1);
            }
       }while (Tinitial>Tempstep);/** Find "Tinitial" **/

               NSTEP ++;
      if (strcmp (string_temp,char_Box) == 0) {
          fgets(string_temp,80,file1);
          sscanf(string_temp,"%lf%lf",&xlo, &xhi);
           Lx = xhi - xlo ;

          fgets(string_temp,80,file1);
          sscanf(string_temp,"%lf%lf",&ylo, &yhi);
          Ly = yhi - ylo ;

          fgets(string_temp,80,file1);
          sscanf(string_temp,"%lf%lf",&zlo, &zhi);
          Lz = zhi - zlo ;
          fgets(string_temp,80,file1); }
           
      if (strcmp (string_temp,char_Atoms) == 0){
              printf("%d\r", i);
          if ( NSTEP == deltaT)
          {       NSTEP=1;
                  i++;
              
              for (counter = 1; counter <= natoms; counter++ )
         {
         fgets(string_temp,80,file1);
                   sscanf(string_temp,"%d%d%lf%lf%lf%d%d%d",&id,&type,&xp,&yp,&zp,&bx,&by,&bz);
         mol[id].type     = type;
                   mol[id].step     = Tempstep;
                   /** getting the real possition **/
                   mol[id].r.x      = xp*Lx + bx*Lx;
                   mol[id].r.y      = yp*Ly + by*Ly;
                   mol[id].r.z      = zp*Lz + bz*Lz;


                   /** ********* Start NOTE 1 *****************/
         mol[id].box.x   = bx;
                   mol[id].box.y   = by;
                   mol[id].box.z   = bz;
                   printf("%ld\r", Tempstep);
                   /** ********* End NOTE 1 *****************/
         }
      /** **** write  into file with fprintf ****/
                      
                     Nleft=100;
                     Nrigth=200;
                     printf("%ld %ld %ld\n",Tempstep, Nleft, Nrigth);
                     Model++;
                     fprintf (file2,"MODEL   %ld\n",Model);
                     fprintf(file2,"TIME STEP %ld\n", Tempstep);
                     fprintf(file3,"%ld %ld %ld\n",Tempstep, Nleft, Nrigth);
                     fflush(file3) ;
                     
                     for (counter = 1; counter <= natoms ; counter++){
                          type=mol[counter].type;
                          
                         if(counter >=Nleft && counter <= Nrigth)
                            {type = mol[counter].type-1;}
                           
                         fprintf (file2,"ATOM   %4.d  %c              %8.2f%8.2f%8.2f\n",counter,AtomType[type],mol[counter].r.x, mol[counter].r.y, mol[counter].r.z);
                          }
                  fprintf (file2,"TER\n");
                  fprintf (file2,"ENDMDL\n");
     /** **** write  into file with fprintf ****/
           }
          else{
              for (counter = 1; counter <= natoms; counter++ )
                 fgets(string_temp,80,file1); }

 
         fgets(string_temp,80,file1);
          }
  }  /** end while **/
  /** fclose closes files **/
  fclose(file1);
  printf("flag0\n");;
  fclose( file2 );
  printf("flag1\n");
  fclose(file3) ;   /** NOTE 2 **/
  printf("everything ok!\n");

  return 0; /* indicates successful termination */

} /** end main */

/******** INPUT FILE*******/
/**** NAME =  samplehistorycopy ***/
ITEM: TIMESTEP
    6770000
ITEM: NUMBER OF ATOMS
        642
ITEM: BOX BOUNDS
 -309.100000000000        309.100000000000     
 -86.1773500000000        86.1773500000000     
 -86.1773500000000        86.1773500000000     
ITEM: ATOMS
 1  1  0.44832  0.49731  0.50099    0    0    0
 2  1  0.44982  0.49486  0.49859    0    0    0
 3  1  0.45148  0.49378  0.49618    0    0    0
 4  1  0.45286  0.49414  0.49380    0    0    0
 5  1  0.45444  0.49446  0.49117    0    0    0
ITEM: TIMESTEP
    6771000
ITEM: NUMBER OF ATOMS
        642
ITEM: BOX BOUNDS
 -309.100000000000        309.100000000000     
 -86.1773500000000        86.1773500000000     
 -86.1773500000000        86.1773500000000     
ITEM: ATOMS
 1  1  0.44832  0.49731  0.50099    0    0    0
 2  1  0.44982  0.49486  0.49859    0    0    0
 3  1  0.45148  0.49378  0.49618    0    0    0
 4  1  0.45286  0.49414  0.49380    0    0    0
 5  1  0.45444  0.49446  0.49117    0    0    0
ITEM: TIMESTEP
    6772000
ITEM: NUMBER OF ATOMS
        642
ITEM: BOX BOUNDS
 -309.100000000000        309.100000000000     
 -86.1773500000000        86.1773500000000     
 -86.1773500000000        86.1773500000000     
ITEM: ATOMS
 1  1  0.44832  0.49731  0.50099    0    0    0
 2  1  0.44982  0.49486  0.49859    0    0    0
 3  1  0.45148  0.49378  0.49618    0    0    0
 4  1  0.45286  0.49414  0.49380    0    0    0
 5  1  0.45444  0.49446  0.49117    0    0    0
ITEM: TIMESTEP
    6773000
ITEM: NUMBER OF ATOMS
        642
ITEM: BOX BOUNDS
 -309.100000000000        309.100000000000     
 -86.1773500000000        86.1773500000000     
 -86.1773500000000        86.1773500000000     
ITEM: ATOMS
 1  1  0.44832  0.49731  0.50099    0    0    0
 2  1  0.44982  0.49486  0.49859    0    0    0
 3  1  0.45148  0.49378  0.49618    0    0    0
 4  1  0.45286  0.49414  0.49380    0    0    0
 5  1  0.45444  0.49446  0.49117    0    0    0
ITEM: TIMESTEP
    6774000
ITEM: NUMBER OF ATOMS
        642
ITEM: BOX BOUNDS
 -309.100000000000        309.100000000000     
 -86.1773500000000        86.1773500000000     
 -86.1773500000000        86.1773500000000     
ITEM: ATOMS
 1  1  0.44832  0.49731  0.50099    0    0    0
 2  1  0.44982  0.49486  0.49859    0    0    0
 3  1  0.45148  0.49378  0.49618    0    0    0
 4  1  0.45286  0.49414  0.49380    0    0    0
 5  1  0.45444  0.49446  0.49117    0    0    0  

Answer
You haven't given the problem statement.
So, I am not able to understand what you are trying to do.
Also, the logic you are using is not clear.

And you haven't mentioned clearly, what problem you are facing.

Without all this info, I will not be able to do anything on this!

Regards,
Narendra

C

All Answers


Answers by Expert:


Ask Experts

Volunteer


Narendra

Expertise

I can answer questions in C related to programming, data structures, pointers and file manipulation. I use Solaris for doing C code and if you have questions related to C programming on Solaris, I will be able to help better.

Experience

6.5

Organizations belong to
Sun Microsystems

Awards and Honors
Brain Bench Certified Expert C programmer.
Advanced System Software Certified

©2012 About.com, a part of The New York Times Company. All rights reserved.