Loop over stdin in C -
i trying loop on stdin, since cannot know length of stdin, not sure how create loop or condition use in it.
basically program piped in data. each line in data contains 10 characters of data, followed line break (so 11 characters per line)
in pseudocode, trying accomplish is:
while stdin has data: read 11 characters stdin save 10 of characters in array run code processing data endwhile each loop of while loop rewrites data same 10 bytes of data.
so far, have figured out that
char temp[11]; read(0,temp,10); temp[10]='\0'; printf("%s",temp); will take first 11 characters stdin,and save it. printf later replaced more code analyzes data. don't know how encapsulate functionality in loop process data stdin.
i have tried
while(!feof(stdin)){ char temp[11]; read(0,temp,11); temp[10]='\0'; printf("%s\n",temp); } but when gets last line, keeps repeatedly printing out without terminating. guidance appreciated.
since mention line breaks, assume data text. here 1 way, when know line lengths. fgets reads newline too, ignored. instead of trying use feof check return value fgets.
#include <stdio.h> int main(void) { char str[16]; int i; while(fgets(str, sizeof str, stdin) != null) { // reads newline = 0; while (str[i] >= ' ') { // shortcut testing newline , nul printf("%d ", str[i]); // print char value i++; } printf ("\n"); str[i] = '\0'; // truncate array } return 0; } program session (ended ctrl-z in windows console, ctrl-d in linux)
qwertyuiop 113 119 101 114 116 121 117 105 111 112 asdfghjkl; 97 115 100 102 103 104 106 107 108 59 zxcvbnm,./ 122 120 99 118 98 110 109 44 46 47 ^z
Comments
Post a Comment