Thread: reading line by from file without fgets

  1. #1
    Registered User
    Join Date
    Dec 2011
    Posts
    7

    reading line by from file without fgets

    I want to read from file line by line without using fgets. I have the following code but in the end crashes and i can not find the what is the problem
    Code:
    # include <stdio.h>
    # include <stdlib.h>
    int main(){
      char buf[60];
      int i=0;
      char ch;
      FILE *f;
      f=fopen("c.txt","r");
      if ( f== NULL ){ 
        printf("ERROR OPENING THE FILE");
            return(1);
       }
       while (!feof(f))
       {    
            do{
                   ch=fgetc(f);
                 buf[i]=ch;
                i++;
            }while(ch!='\n');
            buf[i-1]='\0';
            i=0;    
            printf("%s\n",buf);
        
       }
       fclose(f);
    }

  2. #2

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help with reading a file in C, with fgets()/sscanf()
    By ErickN in forum C Programming
    Replies: 3
    Last Post: 04-23-2011, 10:54 AM
  2. Replies: 7
    Last Post: 12-13-2010, 02:13 PM
  3. Reading a file using fgets
    By Ferris in forum C Programming
    Replies: 10
    Last Post: 12-06-2010, 03:31 PM
  4. fgets reading the same line twice
    By lukeaar in forum C Programming
    Replies: 3
    Last Post: 03-25-2010, 06:50 AM
  5. reading a file line by line and char *
    By odysseus.lost in forum C Programming
    Replies: 8
    Last Post: 05-31-2005, 09:47 AM

Tags for this Thread