How to read variable length, colon delimited fields 
Author Message
 How to read variable length, colon delimited fields

I have 2 questions.

1.  I am trying to read a file with variable length fields.  The fields
are separated by a colon.  In some cases there are spaces within the
fields.   I tried using fscanf but I kept getting the entire string read
into the 1st variable.

ie.   fscanf(fp,"%s:%s:%d:%s",lname,fname,age,notes) ;

What am I doing wrong.

2.  How can perform commands (mkdir, adduser) in C.

Thanks in advance



Sun, 13 Oct 2002 03:00:00 GMT  
 How to read variable length, colon delimited fields

Quote:

> I have 2 questions.

> 1.  I am trying to read a file with variable length fields.  The fields
> are separated by a colon.  In some cases there are spaces within the
> fields.   I tried using fscanf but I kept getting the entire string read
> into the 1st variable.

> ie.   fscanf(fp,"%s:%s:%d:%s",lname,fname,age,notes) ;

Use fgets() and then strtok(); but be a bit careful, strtok() is a
{*filter*}.

Quote:
> 2.  How can perform commands (mkdir, adduser) in C.

system()

Richard



Sun, 13 Oct 2002 03:00:00 GMT  
 How to read variable length, colon delimited fields

You might also want to try:

fscanf( fp,  "%[^:]%[^:]%[^:]%s", lname, fname, string_age, notes );

then you would use strtol() on string_age, and trim the whitespace from
lname, fname as necessary.  I'd think this way would be easier than strtok()
which has a tendency to be a pain to use.


Quote:
> I have 2 questions.

> 1.  I am trying to read a file with variable length fields.  The fields
> are separated by a colon.  In some cases there are spaces within the
> fields.   I tried using fscanf but I kept getting the entire string read
> into the 1st variable.

> ie.   fscanf(fp,"%s:%s:%d:%s",lname,fname,age,notes) ;

> What am I doing wrong.

> 2.  How can perform commands (mkdir, adduser) in C.

> Thanks in advance



Sun, 13 Oct 2002 03:00:00 GMT  
 How to read variable length, colon delimited fields

Quote:

> 1.  I am trying to read a file with variable length fields.  The fields
> are separated by a colon.  In some cases there are spaces within the
> fields.   I tried using fscanf but I kept getting the entire string read
> into the 1st variable.
> ie.   fscanf(fp,"%s:%s:%d:%s",lname,fname,age,notes) ;

you can either use strtok() (as previously mentioned), or just use
fscanf/fgets+sscanf in a different manner:

        fscanf(fp, "%[^:]:%[^:]:%d:%[^:\n]", lname, fname, age, notes);

(fgets() + sscanf() is generally preferred over fscanf/scanf, though,
because it makes it easier to recover from bad input).

--
              /"\                              m i k e    b u r r e l l

               X        AGAINST HTML MAIL
              / \



Sun, 13 Oct 2002 03:00:00 GMT  
 How to read variable length, colon delimited fields


Quote:
> I have 2 questions.

> 1.  I am trying to read a file with variable length fields.
> The fields are separated by a colon.  In some cases there are spaces
> within the fields.   I tried using fscanf but I kept getting the
> entire string read into the 1st variable.

> ie.   fscanf(fp,"%s:%s:%d:%s",lname,fname,age,notes) ;

> What am I doing wrong.

Use a combination of fgets() and strtok() (or strchr()) instead of
fscanf.

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

int get_fields (FILE *in, char *f1, char *f2, int *f3, char *f4)
{
    char buf[MAX_LINE_LEN];
    char *field;
    int ret = NO_ERROR;

    if (!feof (in))
    {
        fgets (buf, sizeof buf, in);
        field = strtok (buf, ":");
        while (field)
        {
            /*
            ** Do what you need to here to process each field
            ** and copy it to the appropriate parameter.
            ** Then fetch the next field from the record.
            */
            field = strtok (NULL, ":");
        }
    }
    else
        ret = AT_EOF;

    return ret;

Quote:
}

> 2.  How can perform commands (mkdir, adduser) in C.

Use the system() function.  Note that this will not return any status
from the command you issued.

Quote:
> Thanks in advance

--
www.biggerhammer.com | Low Technology Solutions for
                     | High Technology Problems

Get the FAQ's at http://www.eskimo.com/~scs/C-faq/top.html

Sent via Deja.com http://www.deja.com/
Before you buy.



Sun, 13 Oct 2002 03:00:00 GMT  
 
 [ 5 post ] 

 Relevant Pages 

1. Thanks - Re: How to read variable length, colon delimited fields

2. Parsing fields in a colon-delimited record

3. parsing variable lenth delimited to fixed lentth fields

4. Reading a delimited file into variables?

5. Reading in variable length files

6. Reading Variable-length Strings

7. Reading in variable-length strings.

8. Colon Separated Fields in a CEdit control?

9. Need to reformat file from variable length & delimited to fixed length

10. reversing the order of comma delimited fields in a file

11. Parsing a comma delimited line with optional fields using sscanf or strtok

12. Reading a colon delineated text line.

 

 
Powered by phpBB® Forum Software