Connecting Tech Pros Worldwide Forums | Help | Site Map

How to read such kind of file format ?

yezi
Guest
 
Posts: n/a
#1: Nov 15 '05
HI:

I need to read the file format like :


1 flatt xxx.xxx.xxx.xxx 5000 1 2
2 latt xxx.xxx.xxx.xxx 5001 3 1 3 4

currently I use the fscanf to read the file.

Do we have other better way?


Mike Wahler
Guest
 
Posts: n/a
#2: Nov 15 '05

re: How to read such kind of file format ?



"yezi" <ye_line@hotmail.com> wrote in message
news:1130452883.295290.221920@g14g2000cwa.googlegr oups.com...[color=blue]
> HI:
>
> I need to read the file format like :
>
>
> 1 flatt xxx.xxx.xxx.xxx 5000 1 2
> 2 latt xxx.xxx.xxx.xxx 5001 3 1 3 4
>
> currently I use the fscanf to read the file.[/color]

That's one way. Another would be to read each line
with 'fgets' and parse it after the fact (e.g. with
'sscanf()', 'strtol()', etc.).
[color=blue]
>
> Do we have other better way?[/color]

Define 'better'. If you show your code, perhaps we
can offer critique and ideas.

-Mike


dinakar_desai@yahoo.com
Guest
 
Posts: n/a
#3: Nov 15 '05

re: How to read such kind of file format ?



yezi wrote:[color=blue]
> HI:
>
> I need to read the file format like :
>
>
> 1 flatt xxx.xxx.xxx.xxx 5000 1 2
> 2 latt xxx.xxx.xxx.xxx 5001 3 1 3 4
>
> currently I use the fscanf to read the file.
>
> Do we have other better way?[/color]
read character at time and parse it.
HTH
../dinakar

hotadvice
Guest
 
Posts: n/a
#4: Nov 15 '05

re: How to read such kind of file format ?


hi

One can use strtok() here.

here all tokens are seperated by whitespace (and assuming any token
does not contain a whitespace character).

char *tok;
char* buff= readLine(...); /* read in a line in buffer say
"1 flatt xxx.xxx.xxx.xxx 5000" */

tok=strtok(buff,"\t "); /* token one ie "1" */

while(tok)
{
tok=strtok(NULL,"\t ");/*
token two ie "xxx.xxx.xxx.xxx" and so
on.
stop when it returns NULL.

*/
}

read about strtok() // its a standard function

hope it helps

bye






dinakar_desai@yahoo.com wrote:
[color=blue]
> yezi wrote:[color=green]
> > HI:
> >
> > I need to read the file format like :
> >
> >
> > 1 flatt xxx.xxx.xxx.xxx 5000 1 2
> > 2 latt xxx.xxx.xxx.xxx 5001 3 1 3 4
> >
> > currently I use the fscanf to read the file.
> >
> > Do we have other better way?[/color]
> read character at time and parse it.
> HTH
> ./dinakar[/color]

Huajian Luo
Guest
 
Posts: n/a
#5: Nov 15 '05

re: How to read such kind of file format ?


"yezi" <ye_line@hotmail.com> writes:[color=blue]
>
> I need to read the file format like :
>
>
> 1 flatt xxx.xxx.xxx.xxx 5000 1 2
> 2 latt xxx.xxx.xxx.xxx 5001 3 1 3 4
>[/color]

Please do some google on this group, and there are loads of examples
as to read conf file like this.
--
Thanks,

Huajian Luo.
Flash Gordon
Guest
 
Posts: n/a
#6: Nov 15 '05

re: How to read such kind of file format ?


hotadvice wrote:[color=blue]
> hi[/color]

Please place your reply *after* the text you are replying to, it makes
it far easier to read.

A: because you have to keep scrolling up and down
Q: what is one reason why top posting is harder to read?
[color=blue]
> One can use strtok() here.[/color]

Yes, provided one knows the caveats.
[color=blue]
> here all tokens are seperated by whitespace (and assuming any token
> does not contain a whitespace character).
>
> char *tok;
> char* buff= readLine(...); /* read in a line in buffer say
> "1 flatt xxx.xxx.xxx.xxx 5000" */
>
> tok=strtok(buff,"\t "); /* token one ie "1" */
>
> while(tok)
> {
> tok=strtok(NULL,"\t ");/*
> token two ie "xxx.xxx.xxx.xxx" and so
> on.
> stop when it returns NULL.
>
> */
> }
>
> read about strtok() // its a standard function[/color]

strtok has problems that the OP needs to watch out for. It maintains
state internally, so you can't interleave two sets of passing using
strtok, which can happen by mistake if it is called from within a
function. strtok merges delimiters, which in this case is probably not a
problem.
--
Flash Gordon
Living in interesting times.
Although my email address says spam, it is real and I read it.
Closed Thread


Similar C / C++ bytes