Re: Cannot find error on my program ( verry basic C stuff )
- From: Barry Schwarz <schwarzb@xxxxxxxx>
- Date: Mon, 30 Mar 2009 19:59:12 -0700
On Mon, 30 Mar 2009 11:03:56 +0200, Chakib Benziane
<spykspygel@xxxxxxxxx> wrote:
snip
Sorry , the link is broken .
Here's the code
/* Remove trailing blanks and tabs from each line
**
*/
#include <stdio.h>
#define MAXLINE 1000 /* max input line length , must be at minimum 4 */
/* function prototypes */
int getline (char line[], int maxline);
int clean (char line[], char cleaned[]);
/* prints input lines after removing trailing blanks and tabs */
int main(void)
{
char line[MAXLINE]; /* current line */
char cleaned[MAXLINE]; /* cleaned line */
int start;
start = 1;
while (getline (line, MAXLINE)){
if (start){ /* if text input starting , print a new line */
printf ("\n");
start = 0;
}
if (clean (line, cleaned))
printf ("%s", cleaned);
else
;
}
return 0;
}
/* get new line into a char array */
int getline(char line[], int lim)
The value passed in to the function for lim is 1000.
{
int c;
int l_index; /* line array index */
/* lim - 2 : 2 elements reserved for '\n' and '\0' */
/* l_index[0] reserver to a pointer */
for (l_index=1; l_index < lim - 2 && ((c = getchar()) != EOF) && (c !=
'\n'); ++l_index){
line[l_index] = c;
}
Let's assume the user typed in 300 characters.
if (c == '\n'){
line[l_index] = c;
line[0] = l_index; /* save the location of nl in the first case of
the array */
Unless your system has bytes bigger than 8 bits, you cannot store the
current value of l_index (300) in a char.
++l_index;
line[l_index] = '\0';
return 1;
}
if (c == EOF)
return 0; /* looking for a way to save the line even if EOF is
reached ! */
You should be aware that getchar can return EOF for reasons other than
end of file. You might want to use the feof or ferror functions.
At this point:
line[0] does not contain the length
your array of char does not contain a '\n' following the input
your array of char does not contain a '\0' following the input
(which means the array does not contain a string)
Since you return 0, the body of the while loop in main will not be
executed so you will not call clean nor will you print out the last
line.
/* If line[] is too short */
if (l_index >= lim - 2){
At this point, can this if ever be false?
printf ("\n\n!!!!!! No enough space to run the program ! MAXLINE
defined as %d !!!!!!!\n\n\n",lim);
return 0;
You need to distinguish between the error return here and the EOF
return above.
}
}
int clean(char line[], char cleaned[])
{
int l_index; /* line array index */
int c_index; /* cleaned line array index */
if (line[1] == '\n')
return 0;
else {
for (l_index = (line[0] - 1); (l_index > 0) && ( (line[l_index] == '
') || (line[l_index] == '\t')); --l_index)
There is another tab character besides horizontal tab. Do you want to
deal with that also? To deal with all characters that are considered
white space, consider using the isspace function.
;
if (l_index <= 0)
return 0;
else {
/* put \n and \0 in the end of the cleaned line */
cleaned[l_index] = '\n';
cleaned[l_index + 1] = '\0';
/*copy every char to the cleaned line */
for (c_index=(l_index - 1) ; (c_index >= 0); --c_index)
cleaned[c_index] = line[c_index + 1];
Ever here of strcpy (or strncpy)?
return 1;
}
}
}
--
Remove del for email
.
- Follow-Ups:
- Re: Cannot find error on my program ( verry basic C stuff )
- From: Keith Thompson
- Re: Cannot find error on my program ( verry basic C stuff )
- References:
- Cannot find error on my program ( verry basic C stuff )
- From: Chakib Benziane
- Re: Cannot find error on my program ( verry basic C stuff )
- From: Chakib Benziane
- Re: Cannot find error on my program ( verry basic C stuff )
- From: Chakib Benziane
- Cannot find error on my program ( verry basic C stuff )
- Prev by Date: Help - trying to serialize a struct
- Next by Date: Re: switch() Statement Question
- Previous by thread: Re: Cannot find error on my program ( verry basic C stuff )
- Next by thread: Re: Cannot find error on my program ( verry basic C stuff )
- Index(es):
Relevant Pages
|