02-08-2007
1,
0
Join Date: Feb 2007
Last Activity: 8 February 2007, 6:25 PM EST
Posts: 1
Thanks Given: 0
Thanked 0 Times in 0 Posts
Help with easy project - reading a file.
I need to do a project that is suppose to read a file and it is suppose to display the number of the line plus whatever the file is reading indented by a tab:
example file:
"This is
the test
file."
expected output:
1 "This is
2 the test
3 file."
I am having two problems.
1. I need to check first if the file is empty.
2. The program includes the last line in the file, even though its empty. It comes out like this:
1 "This is
2 the test
3 file."
4
This is my program:
#include <stdio.h>
main()
{
int c, nl_cnt = 1;
if ((c = getchar()) == 'null') /*<-----"Doesn't work */
printf("File is empty\n");
else
{
printf("%d\t", nl_cnt);
while ((c = getchar()) != EOF)
if (c == '\n')
{
++nl_cnt;
printf("\t\n");
printf("%d\t", nl_cnt);
}
else
printf("%c", c);
}
}
I need help please!