-
Junior Member
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
-
Junior Member
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.
-
Junior Member
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
-
Forum Rules