reading line by from file without fgets

This is a discussion on reading line by from file without fgets within the C Programming forums, part of the General Programming Boards category; I want to read from file line by line without using fgets. I have the following code but in the ...

  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
    Epy
    Epy is offline
    Fortran lover Epy's Avatar
    Join Date
    Sep 2009
    Location
    California, USA
    Posts
    960

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, 01:13 PM
  3. Reading a file using fgets
    By Ferris in forum C Programming
    Replies: 10
    Last Post: 12-06-2010, 02: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


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21