stdin read

Discussion in 'C Programming' started by jason_box, Apr 26, 2006.

  1. jason_box

    jason_box Guest

    I'm trying to parse a data file that has two colums, both columns
    should contain numerical values and I would like them to be stored that
    way for now into two seperate arrays. I was wondering whats the best
    way of doing this?

    file:

    112 56
    344 3
    233 9


    This simple program just counts the number of characters and line that
    I pipe into the program and I was wondering how to take it to the next
    step.

    Code:
    #include <stdio.h>
    
    int main()
    {
        int c, nc = 0, nl = 0;
    
        while ( (c = getchar()) != EOF )
        {
        nc++;
        if (c == '\n') nl++;
        }
    
        printf("Number of characters = %d, number of lines = %d\n", nc,
    nl);
    
        return 0;
    }
    
    jason_box, Apr 26, 2006
    #1
    1. Advertising

  2. jason_box

    Vladimir Oka Guest

    jason_box opined:

    Posting the same question every 15 minutes only *reduces* the
    probability that it will be answered. This is Usenet, an asynchronous
    medium, not a message board.

    > I'm trying to parse a data file that has two colums, both columns
    > should contain numerical values and I would like them to be stored
    > that way for now into two seperate arrays. I was wondering whats the
    > best way of doing this?
    >
    > file:
    >
    > 112 56
    > 344 3
    > 233 9
    >
    >
    > This simple program just counts the number of characters and line
    > that I pipe into the program and I was wondering how to take it to
    > the next step.
    >
    >
    Code:
    [/color]
    
      <snip line/char counting code (most likely) from K&R>
    [color=blue]
    > 


    A clever ploy, this: Copying some code only vaguely related to the
    (homework) problem at hand.

    I suggest you go back to your textbook and look up arrays, loops, and
    input/output functions. I'd suggest `fgets()` and `sscanf()`, although
    plain `scanf()` is probably OK if you're only dealing with numbers.

    --
    "I'd crawl over an acre of 'Visual This++' and 'Integrated Development
    That' to get to gcc, Emacs, and gdb. Thank you."
    (By Vance Petree, Virginia Power)

    <http://clc-wiki.net/wiki/Introduction_to_comp.lang.c>
    Vladimir Oka, Apr 26, 2006
    #2
    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. Johnathan Doe

    peek at stdin, flush stdin

    Johnathan Doe, May 15, 2004, in forum: C Programming
    Replies:
    5
    Views:
    24,835
    Chatoyer
    May 17, 2013
  2. Charlie Zender

    Reading stdin once confuses second stdin read

    Charlie Zender, Jun 19, 2004, in forum: C Programming
    Replies:
    6
    Views:
    768
    Dan Pop
    Jun 21, 2004
  3. Ben
    Replies:
    2
    Views:
    1,326
    jacob navia
    Aug 29, 2009
  4. Terry Cooper
    Replies:
    7
    Views:
    420
    Janos Sebok
    Jun 9, 2009
  5. Stefano Sabatini
    Replies:
    6
    Views:
    286
    Stefano Sabatini
    Jul 29, 2007
Loading...

Share This Page