Redirecting Standard Input C

Chrystian_Vieyra's Avatar, Join Date: Jan 2007
Newbie Member
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 shabbir; 30Jan2007 at 14:24.. Reason: Code formating.
pradeep's Avatar, Join Date: Apr 2005
Team Leader
Try reading this http://www.cs.odu.edu/~zeil/cs361/Le...nterCrash.html
shabbir's Avatar, Join Date: Jul 2004
Go4Expert Founder
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.
DaWei's Avatar, Join Date: Dec 2006
Team Leader
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 by DaWei; 30Jan2007 at 17:24..