c - Interpreting data from fread() -


so attempting pass .txt file through code have echo characters individually output. running script like

./a.out < testwords.in > myout.out 

the crux bit here:

size_t bytes = fread(buffer. sizeof(char),sizeof(char),stdin); fwrite(buffer,sizeof(char),bytes,stdout); fflush(stdout); 

which works fine.

but how can interpret characters 1 1 in if statement? example

if (bytes == '\n') 

doesn't trigger on new lines.

edit:

getc(stdin) more efficient way accomplish task.

i don't know how define efficiency, code might run faster if fread() large buffer. see line of @bluepixy . see speed comparison between fgetc/fputc , fread/fwrite in c

and line if (bytes == '\n') quite strange since pixels in buffer...bytes number of elements read. sure meant if(buffer[i]=='\n')

#include <stdio.h> #include <string.h> #include <stdlib.h>  int main() {     int count=0;     int i;     char buffer[1000];     size_t bytes=1;     while(bytes!=0){         bytes = fread(buffer, sizeof(char),sizeof(buffer),stdin);          for(i=0;i<bytes;i++){             if(buffer[i]=='\n'){                 count++;             }         }         fwrite(buffer,sizeof(char),bytes,stdout);         fflush(stdout);     }     printf("there %d lines in file\n",count);     return 0; } 

i quite sure answer comes late useful !

bye,

francis


Comments

Popular posts from this blog

python - Subclassed QStyledItemDelegate ignores Stylesheet -

java - HttpClient 3.1 Connection pooling vs HttpClient 4.3.2 -

SQL: Divide the sum of values in one table with the count of rows in another -