Re: Cannot find error on my program ( verry basic C stuff )



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
.



Relevant Pages

  • Re: scanf behaviour
    ... char, i have to reread the input until I get the needed pos. ... user input a number that's too large to be stored in an integer. ... static int ignoreblks ... which may be \n or EOF ...
    (comp.lang.c)
  • Re: memory leak?
    ... char, short, int are all 16 bits. ... them rely on EOF being returned by the function. ... value distinct from all unsigned char values. ...
    (microsoft.public.vc.mfc)
  • Re: Is there any GENRIC MACROS in c for INTEGERS,CHARACTERS ?
    ... >> The descriptions of the ctype functions all take int values. ... >> that char is converted to int in this case and that if char is signed ... What is EOF for in this context? ... the 'space' characters and so 0 must be the result. ...
    (comp.lang.c)
  • SSPI Kerberos for delegation
    ... const char *tokenSource, const char *name = NULL, ... DWORD bufsiz = sizeof buf; ... int n = ib.cbBuffer; ... // wserr() displays winsock errors and aborts. ...
    (microsoft.public.dotnet.framework.remoting)
  • Re: huffman encoder
    ... > to the specified stream and advances the position indicator for the ... > the error indicator for the stream is set and EOF is returned. ... the whole damn int does not go to the file, only the byte value of the int. ... flushing the, eg, so the last char is not truncated. ...
    (comp.compression)