scanf - How to let fscanf stop reading after a new line -


#include <stdio.h>  #define max 1000  int line_counter (file *file, char buf[]);  int main(int argc, char *argv[]) {     file *ptr_file;      char buf[max];     ptr_file = fopen("alice-eg.txt", "r");      if (!ptr_file) {         return 1;     }       int count = 0;      while (fscanf(ptr_file, "%s", buf) == 1) {         printf("%s", buf);         if (buf == '\n') {             return count;         }         else {             count += 1;         }     }     printf("the number of words in line is: %d", count);     return 0;     }  

i want along lines of have no idea how make work buf pointer array of letters (correct me if i'm wrong started c , understanding of pointers still quite bad).

fscanf write line file (separated enter) buff array , if read empty line buff[0] = '\n' should condition.

secondly:

while (fscanf(ptr_file, "%s", buf) == 1) 

is wrong since fscanf returns number of read character , line "abcd" form file return 4 , loop stop right away instead of reading entire file , condition should be:

while (fscanf(ptr_file, "%s", buf) != eof) 

since fscanf return eof when reach end of file


Comments

Popular posts from this blog

javascript - how to protect a flash video from refresh? -

android - Associate same looper with different threads -

visual studio 2010 - Connect to informix database windows form application -