c - Trying to use command line ls -l argument as scanf input -
i'm trying parse through ls -l output check file permission , size. seems scanf() function not take in after space. there trick or workaround this? i'd stick using scanf if it's not possible i'm open suggestions. when run program below , print test2 "total" gets stored.
<#include stdio.h> int main(int argc, char **argv){ char test[200]; char test2[200]; scanf("%c\n", test); for(int = 0; < 9 ; i++){ if(test[i] != '\n'){ test2[i] = test[i]; } } (int = 0; < 9; i++){ printf("%c\n", test2[i]); } return 0;
}
thanks. there way check if file or directory using ls -l?
however seems scanf() function not take in after space. ...
scanf("%c\n", test);
it not scanf()
, format used. scanf("%c\n", test);
scans 1 char
test
(test not null character terminated) , scans , tosses white-space. continues until non-white-space found.
more want scanf(" %199[^\n]", test); fgetc(stdin);
1) scan , discard white-space, 2) scan 199 non-'\n'
. 3) stop when full, eof, or '\n'
detected. next character scanned , tossed.
in general. best use fgets()
if (fgets(test, sizeof test, stdin) == null) handle_eof(); test[strcspn(test, "\n")] = 0; // lop off potential \n. (size_t = 0; test[i]; i++){ printf("%c\n", test[i]); }
to clear, not scanf()
evil, trying to many things (io , conversions). better separate.
most scanf()
format specifiers , directive ' '
not distinguish between space , '\n'
. line ending critical code. fgets()
seeks '\n'
, treats spaces other char
.
Comments
Post a Comment