Reputation: 53
I've been trying to learn C lately, and some of the exercises in this book have been rather problematic.
Here's my code for exercise 1-16 - "Revise the main routine of the longest-line program so that it will correctly print the length of arbitrary long input lines, and as much as possible of the text."
#include <stdio.h>
#define MAXLINE 1000 /* maximum input line length */
int getline(char line[], int maxline);
void copy(char to[], char from[]);
/* print the longest input line */
main()
{
int len, c; /*current line length, current character */
int max; /*maximum length seen so far */
char line[MAXLINE]; /* current input line */
char longest[MAXLINE]; /*longest line saved here */
max = 0;
while ((len = getline(line, MAXLINE)) > 0)
if (line[len] != '\0') { // my solution begins here
while ((c = getchar()) != EOF && c != '\n')
++len;
}
else if (len > max) {
max = len;
copy(longest, line);
} //and ends here
if (max > 0) { /* there was a line */
printf("The length of the longest line was: %d\n", max);
printf("%s", longest);
}
return 0;
}
/* getline: read a line into s, return length */
int getline(char s[], int lim)
{
int c, i;
for (i=0; i < lim-1 && (c=getchar()) != EOF && c != '\n'; ++i)
s[i] = c;
if (c == '\n') {
s[i] = c;
++i;
}
s[i] = '\0';
return i;
}
/* copy: copy 'from' into 'to'; assume to is big enough */
void copy(char to[], char from[])
{
int i;
i = 0;
while ((to[i] = from[i]) != '\0')
++i;
}
My problem is that it appropriately truncates the output but states the length of any given arbitrary long line as 999, which is not the case.
It should be noted that this code follows the ANSI C standard.
Upvotes: 2
Views: 324
Reputation: 9082
A problem is that you search for NUL character on position len:
if (line[len] != '\0') { // my solution begins here
But you put a NUL character on that position here:
s[i] = '\0';
so it will always be null.
Another problem is that in this part of code:
if (line[len] != '\0') { // my solution begins here
while ((c = getchar()) != EOF && c != '\n')
++len;
}
you are doing the job of getline. You made the getline function to avoid putting this piece of code in many places by replaceing it with a simple call to getline function.
As you are a beginner, I think it's the best for you to give some hints, not the actual code. Also, even if you can find the answers to exercices, I strongly recommend you try to solve them yourself.
So, you need to find the longest line. getline reads a line and returns to you its lenght and, using the array you pass as a parameter, you get the actual line too. Now, to find the longest one, you need to keep the longest line until now and its length. When you read a new line, you compare its length with the lenght of the line you keep. If it is bigger, then you have a new candidate for the longest line. So, get rid of the line you kept and replace it with this line (you also need to replace its length). After you have read all the lines, the line that you keep is the longest one.
I hope it is clear enough. Also, note that in these days, the files don't end with EOF character anymore. There are other ways to check for file anding, some described here: Why is “while ( !feof (file) )” always wrong? (the question is about a bad practice in checking for file ending, but you will find some good methods in answers).
Upvotes: 0
Reputation: 154198
line
is normally \0
terminated, so looking for it does not inform is a long line was read. Need to check for \n
.
// if (line[len] != '\0') { /
if (line[len-1] != '\n') { /
The while loop in getline()
terminates for 1 of 3 reasons: Full, EOF or \n
. By testing line
did not end in a "\n\0"
, it must be because of EOF or buffer full.
May also want to change as follows. Not sure of your max lengths requirements.
// else if (len > max) {
if (len > max) {
Upvotes: 2