[ale] Stupid C Question

Chris Fowler ChrisF at computone.com
Wed Mar 21 17:42:30 EST 2001





Here is my solution.  It may be dirty, but is there a better way?


Chris




------- /config/conserver.passwd ----------------


bryan:td1AgneGE3RsU:any
djs:*passwd*:any
todd:*passwd*:server1
root:$1$O7JS4ncH$HTyOSyhHQ8vkUOUXe5l8M/:any
louisz:$1$05jQ05Nb$cOjKAvstm7MrVmfins6LL/:any
---------------------------------------------



----- lconusers.c --------------
#include <stdio.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <fcntl.h>
#include <unistd.h>
#include <string.h>


#define CONFIG "/config/conserver.passwd"


void
err_exit(void)
{
        perror("lconusers");
        exit(1);
}


int
main(void)
{


        int fd, nread, counter = 0;
        char buf[1024];
        char *username = '\0';
        char *passwd = '\0';
        char *access = '\0';
        char *tmp = '\0';
        char *tmp2;


        if ((fd = open(CONFIG, O_RDONLY,0)) == -1)
                err_exit();


        if((nread = read(fd, &buf, sizeof(buf))) == -1)
                err_exit();


        buf[nread] = 0;


        
        tmp = strtok(buf, ":");


        do
        {
                tmp2 = strchr(tmp , '\n');
                if (tmp2)
                {
                        printf("%s:", tmp);
                        tmp = tmp2 + 1;
                        *tmp2='\0';
                        counter++;
                        
                }
                else
                if (tmp)
                        if (counter != 3)
                                printf("%s:", tmp);
                        else
                        {
                                counter=0;
                                printf("\n%s:", tmp);
                        }
                
                counter++;              
        
                tmp = strtok(NULL, ":");


        } while ( tmp != NULL);


        return 0;
}



-----Original Message-----
From: Jeff T [mailto:ale at jeffx.com]
To: ale at ale.org
Sent: Wednesday, March 21, 2001 5:33 PM
To: Chris Fowler
Cc: 'ale at ale.org'
Subject: Re: [ale] Stupid C Question



How about something like strtok?  Then if you pass it in null as 
its first parameter you can walk through the line buffer.  


Jeff
At Wednesday, 21 March 2001, Chris Fowler <ChrisF at computone.com> wrote:


>I have a file that looks like this
>
>DATA:DATA:DATA
>DATA:DATA:DATA
>
>Each line is a new record and the DATA of each one can be of variable 
length.  I have tried to read the whole file in buffer and use sscanf 
to parse but that seems to yield no results.  I want to place each 
piece of DATA into character arrays.  The number of records in the 
file could be variable as well.  Any help on this would be greatly 
appreciate.  If you could point me to good documentation on reading,
writing, parsing, and changing of disks files using C, I would appreciate 
that to.  I have many books on C but non really explain how to work 
with files.  Most of my files are config files that I am trying to 
create an interface to change them.
>
>Thanks,
>Chris
>
>








--
To unsubscribe: mail majordomo at ale.org with "unsubscribe ale" in message body.







More information about the Ale mailing list