quantumbutterfly
quantumbutterfly

Reputation: 1955

Loop over stdin in C

I am trying to loop over stdin, but since we cannot know the length of stdin, I am not sure how to create the loop or what condition to use in it.

Basically my program will be piped in some data. Each line in the data contains 10 characters of data, followed by a line break (So 11 characters per line)

In pseudocode, what I am trying to accomplish is:

while stdin has data:
    read 11 characters from stdin
    save 10 of those characters in an array
    run some code processing the data
endwhile

Each loop of the while loop rewrites the data into the same 10 bytes of data.

So far, I have figured out that

char temp[11];
read(0,temp,10);
temp[10]='\0';
printf("%s",temp);

will take the first 11 characters from stdin,and save it. The printf will later be replaced by more code that analyzes the data. But I don't know how to encapsulate this functionality in a loop that will process all my data from stdin.

I have tried

while(!feof(stdin)){
    char temp[11];
    read(0,temp,11);
    temp[10]='\0';
    printf("%s\n",temp);
}

but when this gets to the last line, it keeps repeatedly printing it out without terminating. Any guidance would be appreciated.

Upvotes: 1

Views: 7472

Answers (1)

Weather Vane
Weather Vane

Reputation: 34585

Since you mention line breaks, I assume your data is text. Here is one way, when you know the line lengths. fgets reads the newline too, but that is easily ignored. Instead of trying to use feof I simply check the return value from fgets.

#include <stdio.h>

int main(void) {
    char str[16];
    int i;
    while(fgets(str, sizeof str, stdin) != NULL) {  // reads newline too
        i = 0;
        while (str[i] >= ' ') {                     // shortcut to testing newline and nul
            printf("%d ", str[i]);                  // print char value
            i++;
        }
        printf ("\n");
        str[i] = '\0';                              // truncate the array
    }
    return 0;
}

Program session (ended by 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

Upvotes: 2

Related Questions