Good Lord, C is totally wrong choice for that.
Solution in 6 other languages:
awk:
Code:
awk '{print $1}' foo.txt
sed:
Code:
sed 's/^\([^ ]*\).*$/\1/' foo.txt
perl:
Code:
perl -pe '($_) = split(" ", $_); $_=~s/$/\n/' foo.txt
or other perl implementation:
Code:
perl -pe '$_=~ s/^(\w+).*$/$1/;' foo.txt
tcl: (inside tclsh)
Code:
while {[ gets stdin line] >= 0} { puts [lindex [split $line " "] 0] }
shell script/coreutils:
Code:
cut -f1 -d' ' foo.txt
python:
Code:
python -c $'import sys;\ndef fword(x): print x.split(" ")[0].strip();\nmap(fword,sys.stdin.readlines());' < foo.txt
Do you still want to do that in C?