How to read such kind of file format ?

Discussion in 'C Programming' started by yezi, Oct 27, 2005.

  1. yezi

    yezi Guest

    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?
    yezi, Oct 27, 2005
    #1
    1. Advertising

  2. yezi

    Mike Wahler Guest

    "yezi" <> wrote in message
    news:...
    > 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
    Mike Wahler, Oct 28, 2005
    #2
    1. Advertising

  3. yezi

    Guest

    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
    , Oct 28, 2005
    #3
  4. yezi

    hotadvice Guest

    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






    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
    hotadvice, Oct 28, 2005
    #4
  5. yezi

    Huajian Luo Guest

    "yezi" <> 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.
    Huajian Luo, Oct 28, 2005
    #5
  6. yezi

    Flash Gordon Guest

    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.
    Flash Gordon, Oct 28, 2005
    #6
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Nils Petter Vaskinn
    Replies:
    0
    Views:
    774
    Nils Petter Vaskinn
    Jun 25, 2003
  2. Ron Natalie
    Replies:
    0
    Views:
    693
    Ron Natalie
    Jun 25, 2003
  3. Replies:
    6
    Views:
    1,229
    Alan Johnson
    Jun 5, 2005
  4. Replies:
    1
    Views:
    247
    Andy Fish
    Mar 31, 2008
  5. huisky
    Replies:
    11
    Views:
    353
    huisky
    Nov 18, 2010
Loading...

Share This Page