Help with easy project - reading a file.


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Help with easy project - reading a file.
# 1  
Old 02-08-2007
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!
# 2  
Old 02-08-2007
we don't allow classroom problems, however since you have done some work I will give you some general observations.

1. You will lose the first character with this implementation, you read but never use it.
2. You don't actually need to test the value of the character, a simple true/false test is sufficient for what you are doing in both the if and while cases.
 
Login or Register to Ask a Question

Previous Thread | Next Thread

2 More Discussions You Might Find Interesting

1. Solaris

what is the use of /etc/project file and project administration commands?

i have two doubts.. 1. what is the use /etc/project file. i renamed this file and when i tried to switch user or login with some user account the login was happening slowly. but when i renamed it to original name it was working fine... why so? 2. unix already has useradd and grouadd for... (4 Replies)
Discussion started by: chidori
4 Replies

2. UNIX for Dummies Questions & Answers

Need help on installing an EASY to use and easy to install command line text editor

Hi again. Sorry if it seems like I'm spamming the boards a bit, but I figured I might as well ask all the questions I need answers to at once, and hopefully at least get some. I have installed Solaris 10 on a server. The default text editors are there (vi, ex, ed, maybe others, I know emacs is... (4 Replies)
Discussion started by: EugeneG
4 Replies
Login or Register to Ask a Question