How to read such kind of file format ?

Y

yezi

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

Mike Wahler

yezi said:
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.

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.).
Do we have other better way?

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

-Mike
 
D

dinakar_desai

yezi said:
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?
read character at time and parse it.
HTH
../dinakar
 
H

hotadvice

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
 
H

Huajian Luo

yezi said:
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

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

Flash Gordon

hotadvice said:

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?
One can use strtok() here.

Yes, provided one knows the caveats.
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

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,764
Messages
2,569,567
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top