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??

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) (*/)

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> */

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];
};
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.