Results 1 to 3 of 3

Thread: how to read line by line from a file in C

  1. #1
    Junior Member
    Join Date
    Jul 2008
    Answers
    5

    how to read line by line from a file in C

    hi
    i want to read line by line using C only.the file which i want to read is .cpp or .c file


  2. #2
    Junior Member
    Join Date
    Apr 2008
    Answers
    2

    Re: how to read line by line from a file in C

    hi i am ravindra



    1.declare file pointer
    2.open the file using fopen()
    3.use getc(fp) to read from the file.
    4.close the file when pointer reaches end of file.


  3. #3
    Junior Member
    Join Date
    Dec 2006
    Answers
    1

    Re: how to read line by line from a file in C

    this code will read line by line in c
    //f-file pointer

    int i;
    char c,cc;
    FILE *f;
    i=0; //to count number of line
    f=fopen("file name","r"); //fopen("file name","mode i.e r-read");
    while((c=getc(f))!=EOF) //loop for reading a char upto end of file
    {
    while((cc=getc(f))!='\n') // loop for reading a char upto end of line
    {
    putchar(cc); // to display line
    }
    i++;
    }
    fclose(f);
    printf("\nNumber of lines%d",i);


Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
About us
Applying for a job can be a stressful and frustrating experience, especially for someone who has never done it before. Considering that you are competing for the position with a at least a dozen other applicants, it is imperative that you thoroughly prepare for the job interview, in order to stand a good chance of getting hired. That's where GeekInterview can help.
Interact