0

It looks neat..but i think the "++ptr" should be inside else.

I tried the below and it works

char lname[32];
char fname[32];
char start[5];
..
char *subs[13]={lname,fname,start..};

while (fgets(buf, MAX_LINE_LEN+1, fpsub))
{
     ptr=buf;

     for(i=0;i<12;i++)
     {
       if(sscanf(ptr,"%[^:]%n", subs[i], &n) == 1 )
           {
              ptr += n + 1;
           }
           else
           {
              ++ptr;
              strcpy(subs[i],"NULL");
           }
}

The fields in the rows vary greatly in size.
Max is 33 bytes and minimum is just one byte..

Anyway i have to assign 'NULL' in case of empty field so minimum field length is '5'.

with using 2 dimensional array i think we have to use the maximum field length to every field(if i understand correctly!!)

Let me try to write a empty string instead of NULL for empty fields.
that way i can declare even 2 byte array for 1 byte fields.

Do you think this is a good way to go??

Edited by Dani: Formatting fixed

0

Opening and Parsing a file
We will use the function fopen to open a file (the c source file for this section). Then we will use the fscanf function to read the file word by word.
Note that in c a string is a character array (whose last element is a null character - integer value zero).

The fopen function is used:

file_pointer = fopen( filename, access_mode)

A file pointer is a special type of pointer that points to a file it is defined by the FILE definition (which is in <stdio.h>). It is normal to define file pointers globally (at the top of the program so all functions know their values).

filename is a string constant (or variable) stating the name of file to be opened - include path if not in the same directory.

access_mode is a one character string (normally constant) which states the way the file is to be opened. There are many variations but you will need "r" for read and "w" for write.

It is easy to test to see whether the file has been successfully opened - see function read_pdb_input in template file for project to see how.
The fscanf function is used:
fscanf( file_pointer, control string, variable list)

Except that it reads from the file indicated by file_pointer it works in exactly the same way as scanf - reading a file element by element.

When fscanf gets to the end of a file it returns the value EOF (a standard definned constant). The program below gives the normal way to test for this.
--------------------------------------------------------------------------------

#include <string.h> /* a header needed for scanf function */
#include <stdio.h>  /* a header needed for FILE */

/* define the pointer for the input file globally */
FILE *input_file_pointer;

void main(void)
{
/* variable to read in the words from the file */
     char word[50]; /* no word above 50 characters */

     printf("\n We will split the file parse.c into words");
     printf("\n each word will be output to the screen delimited by ()\n");
     printf("\n all words will be on the same line\n");


/* open input file n.b. no test for the case that filled does
   not exist */
     input_file_pointer = fopen("parse.c", "r");

/* use while construction to use fscanf until an EOF is reached */
/* %s reads a string - a word and != means not equal */
/* do not place semicolon at the end of the line */
/* do not put & before word as it is an array */
     while( fscanf( input_file_pointer, " %s", word)!=EOF)
         {
              printf(" (%s)", word); /* print word surrounded by brackets */
         } /* end of while construction */

} /* end of main function */

--------------------------------------------------------------------------------
Try using text file of the above program
Output should look something like:

We will split the file parse.c into words

each word will be output to the screen delimited by ()

all words will be on the same line

(#include) (<string.h>) (/*) (a) (header) (needed) (for) (scanf) (function) (*/ ) (#include) (<stdio.h>) (/*) (a) (header) (needed) (for) (FILE) (*/) (/*) (defi ne) (the) (pointer) (for) (the) (input) (file) (globally) (*/) (FILE) (*input_fi le_pointer;) (void) (main(void)) ({) (/*) (variable) (to) (read) (in) (the) (wor ds) (from) (the) (file) (*/) (char) (word[50];) (/*) (no) (word) (above) (50) (c haracters) (*/) (printf("\n) (We) (will) (split) (the) (file) (parse.c) (into) ( words");) (printf("\n) (each) (word) (will) (be) (output) (to) (the) (screen) (d elimited) (by) (()\n");) (printf("\n) (all) (words) (will) (be) (on) (the) (same ) (line\n");) (/*) (open) (input) (file) (n.b.) (no) (test) (for) (the) (case) ( that) (filled) (does) (not) (exist) (*/) (input_file_pointer) (=) (fopen("parse. c",) ("r");) (/*) (use) (while) (construction) (to) (use) (fscanf) (until) (an) (EOF) (is) (reached) (*/) (/*) (%s) (reads) (a) (string) (-) (a) (word) (and) (! =) (means) (not) (equal) (*/) (/*) (do) (not) (place) (semicolon) (at) (the) (en d) (of) (the) (line) (*/) (/*) (do) (not) (put) (&) (before) (word) (as) (it) (i s) (an) (array) (*/) (while() (fscanf() (input_file_pointer,) (") (%s",) (word)! =EOF)) ({) (printf(") ((%s)",) (word);) (/*) (print) (word) (surrounded) (by) (b rackets) (*/) (}) (/*) (end) (of) (while) (construction) (*/) (}) (/*) (end) (of ) (main) (function) (*/)
Attachments
#include <string.h> /* a header needed for scanf function */
#include <stdio.h>  /* a header needed for FILE */

/* define the pointer for the input file globally */
FILE *input_file_pointer;

void main(void)
{
/* variable to read in the words from the file */
     char word[50]; /* no word above 50 characters */

     printf("\n We will split the file parse.c into words");
     printf("\n each word will be output to the screen delimited by ()\n");
     printf("\n all words will be on the same line\n");


/* open input file n.b. no test for the case that filled does
   not exist */
     input_file_pointer = fopen("parse.c", "r");

/* use while construction to use fscanf until an EOF is reached */
/* %s reads a string - a word and != means not equal */
/* do not place semicolon at the end of the line */
/* do not put & before word as it is an array */
     while( fscanf( input_file_pointer, " %s", word)!=EOF)
         {
              printf(" (%s)", word); /* print word surrounded by brackets */
         } /* end of while construction */

} /* end of main function */
/* Template for part1.c
	
	(a) ask user for an input pdb file name (which should contain as
		 single water molecule, with the atom order O H1 H2
	(b) open the file
	(c) check that the atoms are in the correct order and read coordinates
	(d) mirror coordinates back to screen
	(e) tell user bond lengths and the bond angle.
	(f) work out and tell user potential energy
        (h) work force vector by central differences and tell users r.m.s. value

    Program completed by A.N. Other??
*/

/* standard headers */
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <string.h>

/* definitions */
#define NO_ATOMS 3 /* number of atoms in the molecule */

/* protypes for functions used NOT TO BE MODIFIED */
	 /* find name and open input pdb file */
	 void ask_open_pdb_input( void );

	 /* read in coords from pdb_input for NO_ATOMS atoms
		 if read is successful then function returns 1 if not 0*/
	 int read_pdb_input( double xyz[NO_ATOMS][3] );

	 /* calculate bond_length between atoms list_atom1 and list_atom2 */
	 double distance( double xyz[NO_ATOMS][3], int list_atom1, int list_atom2);

	 /* calculate angle between atoms list_atom1,list_atom2 and ,list_atom3 
            N.B. result is in radians*/
	 double angle( double xyz[NO_ATOMS][3], int list_atom1, int list_atom2, int list_atom3);

	 /* calculate torsion angle between atoms list_atom1,list_atom2 and ,list_atom3, list_atom4 */
         /* N.B. version will be supplied by O.S.S. for those who cannot write their own */
         double tors_angle( double xyz[NO_ATOMS][3], int list_atom1, int list_atom2, int list_atom3, int list_atom4);

	 /* potential energy for whatever molecule is being considered (current config)*/
	 double pot_eng( double xyz[NO_ATOMS][3]);

         /* the force vector calculated by central difference - one component for each atomic coord 
            calc_force is returned the root mean square of the force vector */
         double calc_force( double xyz[NO_ATOMS][3], double force[NO_ATOMS][3]);

	 /* dot product between two 3-vectors */
	 double dot_prod3( double vec1[3], double vec2[3]);

	 /* cross product between two vectors result last */
	 void cross_prod( double vec1[3], double vec2[3], double vec_res[3]);

	 /* unit 3 vector */
	 void unit_3vector( double vec[3]);
/* protypes for functions used */

/* file pointers are defined globally */
/* pdb input file */
FILE *pdb_input;
double PICONST; /* PICONST is a global vble - 1st job to fill it with correct value */

int main()
{
	 /* The Cartesian coordinates of the water molecule:
		 atom number 1 is oxygen O, atom number 2 is H1 and atom number 3 is H2
		 so xyz[0][0] is the x coordinate of O
			 xyz[0][1] is the y coordinate of O
			 xyz[1][2] is the z coordinate of H1 */
	 double xyz[NO_ATOMS][3];
         double force[NO_ATOMS][3]; /* force vector each component is the partial gradient along 
                                       that cartesian coordinate of the molecule */
	 int iexit=0;  /* success indicator for program - 0 for success 1 failure */

         /* fill PICONST with correct value */
         PICONST=2.*acos(0.);

         /* write greeting */
         printf("\n\n Program to calculate the potential energy of a water molecule");
         printf("\n written by A.N. Other from Jan 1997");

	 /* ask user for the pdb input filename and open it to
		 pointer *pdb_input (globally) defined*/
	 ask_open_pdb_input( );

	 /* get coordinates from file - read_one_wat returns 1 if successful */
	 /* original routine if (read_one_wat(0,xyz)!=1) */
	 if (read_pdb_input(xyz)!=1) /* test to see if 1 returned */
	 {
		 printf("\n Error found on call to read_pdb_input \a"); /* \a produces bleep  */
		 goto Exit_here;  /* ERROR found ! */
	 }

	 /* echo coords read to screen to indicate success */
	 printf("\n coordinates read for O  atom %8.3f %8.3f %8.3f",
			  xyz[0][0], xyz[0][1], xyz[0][2]);
	 printf("\n coordinates read for H1 atom %8.3f %8.3f %8.3f",
			  xyz[1][0], xyz[1][1], xyz[1][2]);
	 printf("\n coordinates read for H2 atom %8.3f %8.3f %8.3f",
			  xyz[2][0], xyz[2][1], xyz[2][2]);

	 /* write out initial bond lengths etc. */
         printf("\n Initial values: ");
	 printf("\n Bond length O H1 is %f angstroms ", distance(xyz,0,1) );
	 printf("\n Bond length O H2 is %f angstroms ", distance(xyz,0,2) );
	 printf("\n Angle H1 O H2 is    %f degrees ",   angle(xyz,1,0,2) );
	 printf("\n Potential energy is %f kcal/mol ",  pot_eng(xyz) );
	 printf("\n Rms force is        %f kcal/(mol.ang) ",  calc_force(xyz, force) );

	 iexit = 1;              /* indicate success */
	 Exit_here: printf("\n\n Normal successful completion \n"); return(iexit); /* n.b. only one exit for whole progrem*/
} /* end of main */

void ask_open_pdb_input()  /* ask user for the pdb input filename and open
										it to pointer *pdb_input (globally) defined*/
{
    char filename[50]; /* include enough characters for full path filename */

    printf("\n Enter input pdb format filename> "); /* prompt user */
    gets(filename); /* read reply */

    while ((pdb_input=fopen( filename, "r"))==NULL) /* try to open for read if successful do nothing
									otherwise try again*/
	{
	    printf("\n Error cannot open file \a "); /* bleep error message */
	    printf("\n Enter input pdb format filename> "); /* prompt user again*/
            gets(filename); /* read reply */
	}

} /* end of fn ask_open_pdb_input */

int read_pdb_input( double xyz[NO_ATOMS][3] )
/* reads the x, y and z for NO_ATOMS from pdb file already opened to
	pdb_input (global variable).
	The x coord for atom 0 to be placed in xyz[0][0],
	the y coord for atom 0 to be placed in xyz[0][1],
	the z coord for atom 0 to be placed in xyz[0][2] &
	the x coord for atom 0 to be placed in xyz[1][0] etc.
	If NO_ATOMS coords are successfully read then routine returns value 1
	but if any problem is found then it returns zero
	N.B. no check is yet made on whether the atoms have expected names */
{
	 int atom_loop; /* loop count for atoms */
	 int result;    /* result of routine, 1 for success, 0 failure */
	 char word[50];  /* loop word for read */

	 fscanf( pdb_input, "%s", word); /* read first word in file */

	 for (atom_loop=0; atom_loop < NO_ATOMS; atom_loop++)  /* read NO_ATOMS coords */
	 {
		 result = 0;  /* indicate that we have as yet missing coord */
		 while( fscanf( pdb_input, "%s", word) != EOF) /* read file word by word
																		  until end of file */
		 {
			  if (strcmp("ATOM",word) == 0 ) /* is the word ATOM? */
			  {                              /* yes it is */
				  fscanf( pdb_input, "%s", word); fscanf( pdb_input, "%s", word);
				  fscanf( pdb_input, "%s", word); fscanf( pdb_input, "%s", word);
				  fscanf( pdb_input, "%s", word); /* must skip 5 words before x*/
				  fscanf( pdb_input, " %lf", &xyz[atom_loop][0]); /* read x coord */
				  fscanf( pdb_input, " %lf", &xyz[atom_loop][1]); /* read y coord */
				  fscanf( pdb_input, " %lf", &xyz[atom_loop][2]); /* read z coord */
				  result = 1; /* have found coord for this atom */
				  break; /* break out of while loop i.e. look for next atom */
			  }
		 }
		 if (result==0) printf("\n ERROR cannot find ATOM record for atom number %d in file \a"
									  ,atom_loop );

	 } /* end of atom_loop loop */

	 return( result);
} /* end of fn read_pdb_input */

double distance( double xyz[NO_ATOMS][3], int list_atom1, int list_atom2)
{
     printf("\n (fn distance not yet written!)");
     return( 0. );
} /* end of fn distance */

/* bond angle function  Result to be returned in radians */
double angle( double xyz[NO_ATOMS][3], int list_atom1, int list_atom2, int list_atom3)
{
     printf("\n (fn angle not yet written!)");
     return( 0. );
} /* end of fn angle */

/* fn tors_angle: calculate the torsion angle between 4 atoms 
   arguements, the coordinates of the atoms, and the 4 integer list number of the
   atoms definning the torsion.

   For atoms 1-2-3-4 the calculation finds the vectors: ([*] indicates 0, 1 then 2)
     avec[*] = xyz[1][*] - xyz[2][*] (i.e vector from atom 2 to 1)
     bvec[*] = xyz[3][*] - xyz[2][*] (i.e vector from atom 2 to 3)
     cvec[*] = xyz[4][*] - xyz[2][*] (i.e vector from atom 2 to 4)
   The plane unit normals for the planes 1-2-3 and 2,3,4 are then found
     norm123 = unit(avec X bvec)
     norm234 = unit(cvec X bvec) (X is a cross product)
   The dihedral angle is the angle between norm123 and norm234
   LOOKING down the vector bvec.
   Find the magnitude by acos(norm123 dot norm234).
   The angle is negative if norm123 X norm234 is in the direction minus bvec.

   Note the angle is returned in radians 

   Note no check is made as to whether the angle is defined (i.e. that no two atoms are coincident) */
double tors_angle( double xyz[NO_ATOMS][3], int list_atom1, int list_atom2, int list_atom3, int list_atom4)
{
   double avec[3], bvec[3], cvec[3], norm123[3], norm234[3], norm123_X_norm234[3]; /* see opening comment */
   double result; /* in radians! */
   int xyz_loop;  /* loop count for x (0) y (1) and z (2) */

   for (xyz_loop=0; xyz_loop < 3; xyz_loop++)
   { /* work out avec, bvec and cvec (see opening comments */
        avec[xyz_loop] = xyz[list_atom1][xyz_loop] - xyz[list_atom2][xyz_loop];
        bvec[xyz_loop] = xyz[list_atom3][xyz_loop] - xyz[list_atom2][xyz_loop];
        cvec[xyz_loop] = xyz[list_atom4][xyz_loop] - xyz[list_atom2][xyz_loop];
   }

   cross_prod( avec, bvec, norm123); /* first plane normal - not yet unit */
   unit_3vector( norm123);           /* unit it */
   cross_prod( cvec, bvec, norm234); /* now second */
   unit_3vector( norm234);           

   result = acos( dot_prod3( norm123, norm234) ); /* angle in radians - but not sign */

   cross_prod( norm123, norm234, norm123_X_norm234); /* cross product of the plane normals */
   
   /* if this cross product is along minus b then dihedral is -ve */
   if (dot_prod3( bvec, norm123_X_norm234) < 0) result = -result;

   return( result);
} /* end of fn tors_angle */

/* potential energy for whatever molecule is being considered i
  i.e. same call should be used to call p.e. with all molecule set ups*/
double pot_eng( double xyz[NO_ATOMS][3])
{
    double result;
    /* SINGLE WATER V
0

Why not start a new thread?


>When fscanf gets to the end of a file it returns the value EOF

while( fscanf( input_file_pointer, " %s", word)!=EOF)

It is better to check whether fscanf successfully scanned the correct number of items. In this case,

fscanf( input_file_pointer, " %s", word) != 1)

This is because fscanf can fail before reaching EOF.


The following is not correct for a hosted implementation.

void main(void)

The main function has one of two forms.

int main(void) { /* ... */ }
int main(int argc, char *argv[]) { /* ... */ }

Since main returns an int, there should be a return statement. In general, there are three portable options.

return 0;
return EXIT_SUCCESS; /* #include <stdlib.h> */
return EXIT_FAILURE; /* #include <stdlib.h> */
0

svp comment parser ce code:

#define INSTNAME_MAX	16
#define MAX_PARAM	4
#define MAX_FIELD	8

/*
  instruction translation
*/
struct inst_tr_s{
    char it_name[INSTNAME_MAX];             /* instruction name */
    unsigned it_nparam;                     /* number of parameters */
    unsigned it_param_bitlength[MAX_PARAM]; /* bitlengths of each parameter */
    unsigned it_nfield;                     /* number of fields */
    struct itfield_s it_fields[MAX_FIELD];  /* the fields */
} ;

/* field types : value, newline, param, high-order bits, low-order_bits */
enum itfield_type_e {FT_VAL, FT_NL, FT_P, FT_L, FT_H};

struct itfield_s it_fields {
    unsigned if_bitlength;
    enum itfield_type_e if_fieldtype;
    unsigned if_paramidx;       /* parameter index (p, h, or l) */
}; 

/*
 instruction
*/
struct inst_s {
    char in_name[INSTNAME_MAX];
    unsigned in_nparam; 
    unsigned in_param[MAX_PARAM];
};

Edited by Narue: added code tags

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.