Getting rid of the Carriage return 
Author Message
 Getting rid of the Carriage return

I'm not sure if this is the correct place to post this:

I want to get rid of the carriage return at the end of a string.
The code looks like this:
Not all declarations are given

char ReqLine[80];
char* TempLine;

while ((fgets(ReqLine,MaxLen,TempReq)) != NULL)
{
 TempLine = strchr(ReqLine,'\r');
 TempLine[0] = '\0';
 printf("%s<BR>",ReqLine);

Quote:
}

This code will be printing to a web page.  The code as it is crashes
and I can't figure out how to change it to make it work.

--
  Mandie Smith  M/S SDVS2

  1000 Coit Road Plano, Texas 75075
  **** The opinions expressed are not those of DSC Communications, Inc ****



Sun, 11 Jul 1999 03:00:00 GMT  
 Getting rid of the Carriage return

Quote:

> I want to get rid of the carriage return at the end of a string.
> while ((fgets(ReqLine,MaxLen,TempReq)) != NULL)
> {
>  TempLine = strchr(ReqLine,'\r');
>  TempLine[0] = '\0';
>  printf("%s<BR>",ReqLine);
> }

My reference states that fgets retains newline character ('\n')
not carriage return ('\r')...This means that strchr() will
skip past end of ReqLine and into some other variable or code
space and replace the '\r' with '\0'...most likely -the crash.

Later,
--



Sun, 11 Jul 1999 03:00:00 GMT  
 Getting rid of the Carriage return

Quote:


> > I want to get rid of the carriage return at the end of a string.
> > while ((fgets(ReqLine,MaxLen,TempReq)) != NULL)
> > {
> >  TempLine = strchr(ReqLine,'\r');
> >  TempLine[0] = '\0';
> >  printf("%s<BR>",ReqLine);
> > }

> My reference states that fgets retains newline character ('\n')
> not carriage return ('\r')...This means that strchr() will
> skip past end of ReqLine and into some other variable or code
> space and replace the '\r' with '\0'...most likely -the crash.

Wrong, strchr() will return a NULL pointer if character not found.
you should test TempLine for NULL before blindly using it
...sorry for the first post...a little hasty....

Later,
--



Sun, 11 Jul 1999 03:00:00 GMT  
 Getting rid of the Carriage return


)I'm not sure if this is the correct place to post this:
)
)I want to get rid of the carriage return at the end of a string.
)The code looks like this:
)Not all declarations are given
)
)char ReqLine[80];
)char* TempLine;
)
)while ((fgets(ReqLine,MaxLen,TempReq)) != NULL)
){
) TempLine = strchr(ReqLine,'\r');
) TempLine[0] = '\0';
) printf("%s<BR>",ReqLine);
)}
)
)
)This code will be printing to a web page.  The code as it is crashes
)and I can't figure out how to change it to make it work.

strchr(.,.) does not always return a non-NULL pointer, you know.

Mike
--
----
char *p="char *p=%c%s%c;main(){printf(p,34,p,34);}";main(){printf(p,34,p,34);}
This message made from 100% recycled bits.
I don't speak for DSC.         <- They make me say that.



Mon, 12 Jul 1999 03:00:00 GMT  
 Getting rid of the Carriage return

Quote:

> I'm not sure if this is the correct place to post this:
> I want to get rid of the carriage return at the end of a string.
> The code looks like this:
> Not all declarations are given
> char ReqLine[80];
> char* TempLine;
> while ((fgets(ReqLine,MaxLen,TempReq)) != NULL)
> {
>  TempLine = strchr(ReqLine,'\r');
>  TempLine[0] = '\0';
>  printf("%s<BR>",ReqLine);
> }
> This code will be printing to a web page.  The code as it is crashes
> and I can't figure out how to change it to make it work.

1. You don't check for the return value of strchr() wich might not have
   worked.
2. Is MaxLen 80 bytes ?

A little suggestion, although this depends a bit on wich system you
are working (depending wether lines are terminated with \n, \r, or
\r\n - you might want to opt for a bit more simpler approach ;

 if(strlen(ReqLine)) /* Do we have a line at all ? */
    ReqLine[strlen(ReqLine)-1] = '\0' ; /* Kill the damn '\r' */

Cheers, flux.



Mon, 12 Jul 1999 03:00:00 GMT  
 Getting rid of the Carriage return


Quote:

> > I want to get rid of the carriage return at the end of a string.
> A little suggestion, although this depends a bit on wich system you
> are working (depending wether lines are terminated with \n, \r, or
> \r\n - you might want to opt for a bit more simpler approach ;

>  if(strlen(ReqLine)) /* Do we have a line at all ? */
>     ReqLine[strlen(ReqLine)-1] = '\0' ; /* Kill the damn '\r' */

fgets() stops when a newline '\n' is encountered, not '\r'.

Using the above overwrite presents two problems when you input a
greater number of characters than you have set with the second
parameter of fgets. fgets(char*s,int n,FILE *stream) reads in at
most n-1 characters into the array s, stopping if a newline ('\n')
character is encountered. Thus if input characters exceed n -1 the
above will overwrite a character and not the newline character.
This can be avoided by first checking s[strlen(s) -1] to see if it
is indeed the '\n' character and then overwriting it.

The second problem is, if you exceed n-1 characters in the input,
you still have (at least in my implementation) unread
characters in the OS level input buffer. You will need to flush
this before you make another stdin read. I use the non-standard
fflush(stdin) statement.

You can do something like the following:

#include <stdio.h>
#include <string.h>

int main()
   {
   char buffer[10];

   fgets(buffer,10,stdin);
   fflush(stdin);     /* NOT standard C  */
   if(buffer[strlen(buffer) - 1] == '\n')
      buffer[strlen(buffer) - 1] = '\0';
   printf("%s\n",buffer);
   fgets(buffer,10,stdin);
   fflush(stdin);
   if(buffer[strlen(buffer) - 1] == '\n')
      buffer[strlen(buffer) - 1] = '\0';  
   printf("%s\n",buffer);
   puts("last line!\n");
   return 0;
   }
--
Al Bowers
Tampa, FL

http:www.gate.net/~abowers/index.html



Mon, 12 Jul 1999 03:00:00 GMT  
 Getting rid of the Carriage return

|> I'm not sure if this is the correct place to post this:
|>
|> I want to get rid of the carriage return at the end of a string.

  Are you _sure_ that there _is_ a carriage return at the end ?? If the
  file was opened as  a text file and you're reading it _via_ stdio calls,
  any '\r's should have been removed ...

|> The code looks like this:
|> Not all declarations are given
|>
|> char ReqLine[80];
|> char* TempLine;
|>
|> while ((fgets(ReqLine,MaxLen,TempReq)) != NULL)
|> {
|>  TempLine = strchr(ReqLine,'\r');
|>  TempLine[0] = '\0';
|>  printf("%s<BR>",ReqLine);
|> }
|>

 You don't check the value returned by 'strchr()' prior to dereferencing
 it -- if I am correct in my previous remarks, there's no '\r' to be
 found, 'strchr()' returns NULL and the next line of code invokes undefined
 behavior (typically, a segmentation violation on Unix boxes).

 Why don't you try:

 char ReqLine[80];
 char *TempLine;

 while ( fgets(ReqLine,sizeof ReqLine,TempReq) ) {
        if ( TempLine=strchr(ReqLine,'\n') )
                *TempLine = '\0';
        printf("%s<BR>",ReqLine);
 }

 That should do what you want ...

|>
|> This code will be printing to a web page.  The code as it is crashes
|> and I can't figure out how to change it to make it work.
|>
|>
|> --
|>   Mandie Smith  M/S SDVS2

|>   1000 Coit Road Plano, Texas 75075
|>   **** The opinions expressed are not those of DSC Communications, Inc ****

--
 Ed Hook                              |       Copula eam, se non posit
 Computer Sciences Corporation        |         acceptera jocularum.
 NASA Langley Research Center         | Me? Speak for my employer?...<*snort*>



Mon, 12 Jul 1999 03:00:00 GMT  
 Getting rid of the Carriage return

: I'm not sure if this is the correct place to post this:

: I want to get rid of the carriage return at the end of a string.
: The code looks like this:
: Not all declarations are given

: char ReqLine[80];
: char* TempLine;

: while ((fgets(ReqLine,MaxLen,TempReq)) != NULL)
: {
:  TempLine = strchr(ReqLine,'\r');
:  TempLine[0] = '\0';
:  printf("%s<BR>",ReqLine);
: }

Write your own string reading function rather than using fgets.

FF



Mon, 12 Jul 1999 03:00:00 GMT  
 Getting rid of the Carriage return

: I'm not sure if this is the correct place to post this:

: I want to get rid of the carriage return at the end of a string.
: The code looks like this:
: Not all declarations are given

: char ReqLine[80];
: char* TempLine;

: while ((fgets(ReqLine,MaxLen,TempReq)) != NULL)
: {
:  TempLine = strchr(ReqLine,'\r');
:  TempLine[0] = '\0';
:  printf("%s<BR>",ReqLine);
: }

: This code will be printing to a web page.  The code as it is crashes
: and I can't figure out how to change it to make it work.

The problem is that lines read by fgets will either contain a \n as the
last character if the whole line was read in or some other character if
line length was greater that 80 - 1, in your case.  This means that
strchr will likely always return the null pointer and you are not
checking for that.  Here is a routine I wrote when I was learning C in
first year University.  It works OK as long as you follow what the
comments say.  You can change the value of ONE_K to any positive number.
I would rename it though since then it won't make sense to call it
ONE_K.

#include <stdio.h>
#include <stdlib.h>
#define ONE_K 1024

/*
 * read a line from a FILE; line length need not be known; on first call
 * the pointer "st" must be the null pointer or you will have serious
 * problems; if this routine is passed a non null pointer and that non
 * null pointer is pointing to garbage or less than 1K of memory then
 * you will have problems; the newline character is not included in the
 * input string; returns the null pointer on error and end_of_file
 * eg.
 * char *s = 0;
 * while(s = readln(s,stdin))
 *  printf("%s",s);
 */
char*
readln(char* st,FILE* file)
{
    int ch, /* character read from stream */
        i,  /* index into array of char */
        chunks; /* number of 1 K chunks of memory allocated */

    if(!st)
        st = malloc(ONE_K + 1); /* allocate memory on first call */
    /*
     * always allocate 1 extra character for the terminating null byte;
     * chunks starts at 2 because if memory needs to be realloc'ed it
     * will be at least the second chunk of memory that will need to be
     * realloc'ed
     */
    for(i = 0,chunks = 2;EOF != (ch = fgetc(file));++i) {
        if('\n' == ch)
            break;
        *(st + i) = ch;
        if(!(i % ONE_K) && i > 0) {
            char* tmp = realloc(st,chunks * ONE_K + 1);

            if(tmp)
                st = tmp;
            else {
                free(st);
                return 0;
            }
            ++chunks;
        }
    }
    if(EOF == ch)
        st = realloc(st,0);
    else
        *(st + i) = 0;

    return st;

Quote:
}

--




Mon, 12 Jul 1999 03:00:00 GMT  
 Getting rid of the Carriage return

Quote:


> > I want to get rid of the carriage return at the end of a string.
>     It may be faster to do something like...

>     while ((fgets(ReqLine,MaxLen,TempReq)) != NULL)
>     {
>       int len = strlen(RegLine) - 1;
>       if (len >= 0 && ReqLine[len] = '\n')

                                    ^^^^
Looks like a classic error. Should be ==

Quote:
>          TempLine[len] = '\0';
>       printf("%s<BR>",ReqLine);
>     }

If you are desire to get rid of a '\n' char at the end of
a string, use
     if(RegLine[strlen(ReqLine) - 1] == '\n')
        ReqLine[strlen(ReqLine) - 1] = '\0';
--
Al Bowers
Tampa, FL

http:www.gate.net/~abowers/index.html


Sat, 17 Jul 1999 03:00:00 GMT  
 Getting rid of the Carriage return

Quote:

> I want to get rid of the carriage return at the end of a string.
> The code looks like this:
> Not all declarations are given
> char ReqLine[80];
> char* TempLine;
> while ((fgets(ReqLine,MaxLen,TempReq)) != NULL)
> {
>  TempLine = strchr(ReqLine,'\r');

              ^^^ [1]        ^^^[2]
Quote:
>  TempLine[0] = '\0';

        ^^^^^^^^^^^^[3]

Quote:
>  printf("%s<BR>",ReqLine);
> }
> This code will be printing to a web page.  The code as it is crashes
> and I can't figure out how to change it to make it work.

[1]: Remember strchr() can return NULL if the character you are
     searching for is not found.

[2]: are you sure you want to get rid of '\r's? perhaps you want to
     get rid of newlines (or both... I suppose you have to be prepared
     for netascii, which specifies lines terminate with "\r\n"... note
     the order).  If it's both, you may want to use strcspn() like so:
     ReqLine[strcspn("\r\n", ReqLine)] = 0;

[3]: because of #1, when a '\r' is not found in the ReqLine, TempLine
     is NULL and you dereference a NULL pointer.. ({*filter*} Bad Pointer
     Thing [tm]).

--

Support bacteria -- it's the only         | is unwelcome.  I avoid dealing
form of culture some people have!         | with companies that email ads.



Sun, 18 Jul 1999 03:00:00 GMT  
 
 [ 11 post ] 

 Relevant Pages 

1. get rid of carriage return with getchar?

2. Carriage return / line feed?

3. lines ending with only a carriage return

4. Problems with reading a new line / carriage return

5. carriage return newline problem

6. carriage returns in preprocessor macros (#define)?

7. Behaviour of Carriage return '\r'

8. Unwanted carriage returns from pc to unix

9. - Those damn carriage returns in the code...

10. how to get character through stdin not waiting for carriage return

11. How to check if user hit carriage return

12. Carriage return

 

 
Powered by phpBB® Forum Software