Posted By

imprfikt on 04/22/11


Tagged

file


Versions (?)

Read file line by line


 / Published in: C
 

  1. #include <stdio.h>
  2.  
  3. void main()
  4. {
  5. char str[200];
  6. FILE *fp;
  7.  
  8. fp = fopen("test.txt", "r");
  9. if(!fp) return 1; // bail out if file not found
  10. while(fgets(str,sizeof(str),fp) != NULL)
  11. {
  12. // strip trailing '\n' if it exists
  13. int len = strlen(str)-1;
  14. if(str[len] == '\n')
  15. str[len] = 0;
  16. printf("\n%s", str);
  17. }
  18. fclose(fp);
  19. }

Report this snippet  

You need to login to post a comment.