hi
i want to read line by line using C only.the file which i want to read is .cpp or .c file
Printable View
hi
i want to read line by line using C only.the file which i want to read is .cpp or .c file
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.
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);