fscanf question

Discussion in 'C Programming' started by Fallon, Jul 12, 2003.

  1. Fallon

    Fallon Guest

    If someone could help it would be greatly appreciated.

    I am trying to write a simple program to read in elements from an
    input file formatted as follows:

    3
    4
    81
    5
    6
    45
    54
    7
    ..
    ..
    ..
    (etc)

    Now I'd like to be able to extract the first, third, or any ordered
    element?
    How would I do this, I've looked at all documentation for fscanf and
    haven't been able to find the answer. I know I can read in the first
    element using:

    fscanf(fd, "%d", &x)


    Or the entire list can be read in using a for loop of something like:

    for(i=0; i < number; i++)
    if (fscanf(fd, "%d", &x)==EOF)
    break;


    But how can I access specific ordered elements, or for that matter a
    specific range of elements from the input file (e.g. elements 4-8)?

    Much appreciated,
    Jessica
    Fallon, Jul 12, 2003
    #1
    1. Advertising

  2. In 'comp.lang.c', (Fallon) wrote:

    > I am trying to write a simple program to read in elements from an
    > input file formatted as follows:
    >
    > 3
    > 4
    > 81
    > 5
    > 6
    > 45
    > 54
    > 7
    > .
    > .
    > .
    > (etc)
    >
    > Now I'd like to be able to extract the first, third, or any ordered
    > element?
    > How would I do this, I've looked at all documentation for fscanf and
    > haven't been able to find the answer. I know I can read in the first
    > element using:
    >
    > fscanf(fd, "%d", &x)
    >
    >
    > Or the entire list can be read in using a for loop of something like:
    >
    > for(i=0; i < number; i++)
    > if (fscanf(fd, "%d", &x)==EOF)
    > break;
    >
    >
    > But how can I access specific ordered elements, or for that matter a
    > specific range of elements from the input file (e.g. elements 4-8)?


    Use the i value to 'filter' the required actions

    int x;
    int i;

    for (i = 0; i < number; i++)
    {
    if (fscanf(fd, "%d", &x) == 1)
    {
    /* filter */
    switch (i + 1)
    {
    case 1:
    /* 1st process */
    break;

    case 3:
    /* 3rd process */
    break;
    }
    default:
    /* ignore */
    ;
    }
    else
    {
    break;
    }
    }

    --
    -ed- [remove YOURBRA before answering me]
    The C-language FAQ: http://www.eskimo.com/~scs/C-faq/top.html
    <blank line>
    FAQ de f.c.l.c : http://www.isty-info.uvsq.fr/~rumeau/fclc/
    Emmanuel Delahaye, Jul 12, 2003
    #2
    1. Advertising

  3. Fallon

    Jason Guest

    You could count each item after using fscanf, and determine which items you
    want to keep.

    If you really do need to access elements directly from a file, then you
    could use fopen in binary mode ... fopen("file", "rb"); and use the fseek
    function to get to the desired value. The file will not be in such a
    favoriable format if you want to edit it in a text editor though.
    Jason, Jul 12, 2003
    #3
  4. Fallon

    Giuseppe Guest

    On 12 Jul 2003 09:33:58 -0700, (Fallon) wrote:
    >If someone could help it would be greatly appreciated.
    >
    >I am trying to write a simple program to read in elements from an
    >input file formatted as follows:
    >
    >3
    >4
    >81
    >5
    >6
    >45
    >54
    >7
    >.
    >.
    >.
    >(etc)
    >Now I'd like to be able to extract the first, third, or any ordered
    >element?
    >How would I do this, I've looked at all documentation for fscanf and
    >haven't been able to find the answer. I know I can read in the first
    >element using:
    >
    >fscanf(fd, "%d", &x)


    >Or the entire list can be read in using a for loop of something like:
    >
    >for(i=0; i < number; i++)
    > if (fscanf(fd, "%d", &x)==EOF)
    > break;


    >But how can I access specific ordered elements, or for that matter a
    >specific range of elements from the input file (e.g. elements 4-8)?


    you could count '\n' in the file
    _____
    ABBASSO L'Europa e il governo globale del mondo
    Giuseppe, Jul 13, 2003
    #4
    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. Marc Reclaire

    Question about fscanf ..

    Marc Reclaire, Dec 18, 2003, in forum: C++
    Replies:
    2
    Views:
    371
    Karl Heinz Buchegger
    Dec 18, 2003
  2. Barry Schwarz

    Re: Simple fscanf Question

    Barry Schwarz, Aug 1, 2003, in forum: C Programming
    Replies:
    2
    Views:
    395
    Barry Schwarz
    Aug 2, 2003
  3. Cal Lidderdale

    fscanf question - most of line -> null

    Cal Lidderdale, Dec 22, 2004, in forum: C Programming
    Replies:
    4
    Views:
    599
    Chris Torek
    Dec 28, 2004
  4. siliconwafer

    question abt fscanf

    siliconwafer, Sep 1, 2005, in forum: C Programming
    Replies:
    1
    Views:
    575
    Chris Torek
    Sep 2, 2005
  5. Gary Baydo

    Question about fscanf() behavior

    Gary Baydo, Nov 6, 2005, in forum: C Programming
    Replies:
    2
    Views:
    299
    Gary Baydo
    Nov 6, 2005
Loading...

Share This Page