By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,187 Members | 1,060 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,187 IT Pros & Developers. It's quick & easy.

How to read such kind of file format ?

P: n/a
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?

Nov 15 '05 #1
Share this Question
Share on Google+
5 Replies


P: n/a

"yezi" <ye*****@hotmail.com> wrote in message
news:11**********************@g14g2000cwa.googlegr oups.com...
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
Nov 15 '05 #2

P: n/a

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

Nov 15 '05 #3

P: n/a
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


di***********@yahoo.com wrote:
yezi wrote:
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


Nov 15 '05 #4

P: n/a
"yezi" <ye*****@hotmail.com> writes:

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.
--
Thanks,

Huajian Luo.
Nov 15 '05 #5

P: n/a
hotadvice wrote:
hi
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.
--
Flash Gordon
Living in interesting times.
Although my email address says spam, it is real and I read it.
Nov 15 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.