Thread: How to read input from stdin

  1. #1
    Registered User
    Join Date
    Oct 2011
    Posts
    6

    How to read input from stdin

    I am writing the c program using linux shell, i want to save a batch of the input parameter in the text file, input.txt
    i want to run my program like this
    ./a.out < input.txt
    at that time, i need to save the input list in the char* array[10]
    could somebody give me any hints of source code?

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User
    Join Date
    Oct 2011
    Posts
    6
    i am not wanna have argv command.........i want to use scanf to the stdin, is that ok??

  4. #4
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Yep. Should work provided you know what to expect from the command line.

  5. #5
    Registered User
    Join Date
    Oct 2011
    Posts
    6
    My situation is that I stored all the parameter in the text file call a.txt
    i need to run my file in Linux server like this:
    ./a.out < a.txt
    I am try to assign thoes parameter in the array of char*

    i wrote the code like this:
    char *word[n];
    for (i=0; i<count; i++)
    scanf("%s", &word[i]);

    unfortunately, the error message occured
    "segmentation fault"
    i have no idea of that problems
    could any helps my to figure out this problem?
    Thank you very much!!

  6. #6
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    That's just an array of pointers. You don't have them pointing to any memory you have allocated. Try using fgets to read into a temporary buffer, then find out the length of whatever it is you want to store, then allocate space (malloc), copy from the buffer to the new space (strcpy), and assign the new space to that pointer.


    Quzah.
    Hope is the first step on the road to disappointment.

  7. #7
    Registered User
    Join Date
    Aug 2010
    Posts
    231
    Quote Originally Posted by johnny8888 View Post
    Code:
    char *word[n];
    for (i=0; i<count; i++)
       scanf("%s", &word[i]);
    Code:
    char words[10][100];
    int i;
    for( i=0;i<10;++i )
      if( fgets(words[i],100,stdin) )
      {/* fgets doesn't ever add a '\n', therefore... */
        if( strchr(words[i],'\n') ) *strchr(words[i],'\n')=0;
      }

  8. #8
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by BillyTKid View Post
    Code:
      {/* fgets doesn't ever add a '\n', therefore... */
        if( strchr(words[i],'\n') ) *strchr(words[i],'\n')=0;
      }
    I'm not sure what your comment is supposed to mean, but that's an expensive way to try to get rid of the newline.


    Quzah.
    Hope is the first step on the road to disappointment.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Checking stdin for input
    By hawaiian robots in forum C Programming
    Replies: 7
    Last Post: 05-19-2009, 09:06 AM
  2. Multiplexing read socket and stdin
    By redcameron in forum Networking/Device Communication
    Replies: 8
    Last Post: 04-06-2009, 05:34 PM
  3. using fread to read from stdin
    By -EquinoX- in forum C Programming
    Replies: 17
    Last Post: 03-23-2009, 12:41 AM
  4. How do you catch a read error from stdin?
    By mr_coffee in forum C Programming
    Replies: 8
    Last Post: 09-03-2008, 03:34 PM
  5. Input: File or stdin?
    By Arker in forum C++ Programming
    Replies: 3
    Last Post: 02-13-2003, 08:14 PM