Redirecting Standard Input C

Discussion in 'C' started by Chrystian_Vieyra, Jan 30, 2007.

  1. Chrystian_Vieyra

    Chrystian_Vieyra New Member

    Joined:
    Jan 30, 2007
    Messages:
    1
    Likes Received:
    0
    Trophy Points:
    0
    Location:
    Macomb, IL
    Home Page:
    http://www.wiu.edu/users/cv103
    I am trying to redirect standard input to be a file, unfortunately I have not been succesful.

    My command in unix to execute my c applicacion is:

    a.out < file.something

    However, I receive a segmentation error.

    my c code to read that standard in is here:
    Code:
    void input()
    {
     char s[1];
     scanf("%c",s[1]);
     printf("Contents %c", s[1]);
    }
     
    Last edited by a moderator: Jan 30, 2007
  2. pradeep

    pradeep Team Leader

    Joined:
    Apr 4, 2005
    Messages:
    1,645
    Likes Received:
    87
    Trophy Points:
    0
    Occupation:
    Programmer
    Location:
    Kolkata, India
    Home Page:
    http://blog.pradeep.net.in
  3. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,375
    Likes Received:
    388
    Trophy Points:
    83
    The segmentation error is you are defining the size of the array as 1 and you are doing the scanf in the position 1 but it should be at 0. Also you need to specify the address of the variable and not the variable.

    Try

    scanf("%c",&s[0]);

    Also you now know what you need to be doing with printf I guess.
     
  4. DaWei

    DaWei New Member

    Joined:
    Dec 6, 2006
    Messages:
    835
    Likes Received:
    5
    Trophy Points:
    0
    Occupation:
    Semi-retired EE
    Location:
    Texan now in Central NY
    Home Page:
    http://www.daweidesigns.com
    What's the point of a one-byte array? Why not just define a char? You also might as well redirect a line at a time since the standard libraries don't fetch a character at a time. They don't give you anything until the end of the line is encountered. You have to go implementation-specific for keystroke-level input.
     
    Last edited: Jan 30, 2007

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice