----- Original Message ----- 
From: "shashi kiran" 
To: "Vijay Kumar R Zanvar" 
Sent: Tuesday, May 11, 2004 5:50 PM
Subject: Urgent

>  
> struct header
> {
>  
>     int interval;
>     char (*colnames)[10];
> };
>  
> given a text file which is required to be parsed in such a way that
> segregate the line in to no of cols
>  
> input:
> 10|response|reply|send|wait
>  
> here give me an idea how to store this names reponse,reply,send and wait
> in pointer to an array member of struct Header
>  
> TID
> kiran
>  
    I would not comment on the structure and why to use pointer to an array of
character.  Following program, without using the above structure, does the task
of parsing the file.  The format of the record file should be:

    |chars|chars|chars|chars
            
Each field is delimited by a vertical bar (|).  

F:\Vijay\C> type read_file.c 
/*
 * read_file.c  -   Read a formatted file
 * Author       -   Vijay Kumar R Zanvar 
 * Date         -   May 12, 2004
 */

#include 
#include 

#define NUM_OF_COL  5
#define NAME_LENGTH 16
#define NUM_LINES 20

static char name[NUM_LINES][NUM_OF_COL][NAME_LENGTH];

int
read_file ( FILE * const fp )
{
    int     interval,
            line = 0;

    while ( !feof ( fp ) && !ferror ( fp ) )
    {
        int n = fscanf ( fp, 
                     "%d%*[|] %[^|]%*[|] %[^|]%*[|] %[^|]%*[|] %s%*[^\n]",
                     &interval,
                     name[line][0],
                     name[line][1],
                     name[line][2],
                     name[line][3]
                );
        
        if ( n != EOF && n != NUM_OF_COL )
        {
            fprintf ( stderr, "Corrupt record found at line %d\n", line+1 );
            return EXIT_FAILURE; 
        }
        else
        if ( n != EOF && n == NUM_OF_COL ) 
            printf ( "interval = %d field2 = %s field3 = %s field4 = %s \
                        field5 = %s\n",
                         interval,
                         name[line][0],
                         name[line][1],
                         name[line][2],
                         name[line][3]
                   );

        line++;
    }
            
    return EXIT_SUCCESS;
}
 
int
main ( int argc, char *argv[] )
{
    FILE *fp;
    
    /* Do error checking on argc */
     if ( argc != 2 ) 
     { 
         fprintf ( stderr, "Usage: read_file.exe \n" ); 
         return EXIT_FAILURE; 
     } 
  
     fp = fopen ( argv[1], "r" ); 
     if ( !fp ) 
     { 
         perror ( "read_file.exe" ); 
         return EXIT_FAILURE; 
     }  
     return read_file ( fp ); 
} 

F:\Vijay\C> gcc -Wall read_file.c -o read_file.exe

F:\Vijay\C> type record.txt
10|response|reply|send|wait
12|field2|field3|field4|field5
13|f1|f2|f3|f4

F:\Vijay\C> 
F:\Vijay\C> read_file record.txt 
interval = 10 field2 = response field3 = reply field4 = send field5 = wait
interval = 12 field2 = field2 field3 = field3 field4 = field4 field5 = field5
interval = 13 field2 = f1 field3 = f2 field4 = f3 field5 = f4 

F:\Vijay\C> type record.txt
10|response|reply|send|wait
12|field2|field3|field4|field5
13|f1|f2|f3|f4
200|

F:\Vijay\C> read_file record.txt 
interval = 10 field2 = response field3 = reply field4 = send field5 = wait
interval = 12 field2 = field2 field3 = field3 field4 = field4 field5 = field5
interval = 13 field2 = f1 field3 = f2 field4 = f3 field5 = f4
Corrupt record found at line 4


    The expression, "%d%*[|] %[^|]%*[|] %[^|]%*[|] %[^|]%*[|] %s%*[^\n]",
deserves an explanation.  Section B.1.3, Formatted Input, of K&R says: 

[...]           matches the longest non-empty string of input characters from
                the set between brackets; char *. A '\0' is added. []...]
                includes ] in the set. 

[^...]          matches the longest non-empty string of input characters not 
                from the set between brackets; char *. A '\0' is added. [^]...]
                includes ] in the set.

    So,

[^|]            Read all the characters from the input stream till a vertical
                bar(|) is found.  Leave the vertical bar in the stream or 
                buffer.

*[|]            Read any number of times (the asterisk in *[] causes) all the
                characters that match a vertical bar.
                
*[^\n]          Read all the characters till end-of-line.

    Source: geocities.com/vijoeyz/faq/c

               ( geocities.com/vijoeyz/faq)                   ( geocities.com/vijoeyz)