Adding a newline to a string 
Author Message
 Adding a newline to a string

Hi,

I'm writing a small program were I read a string from an .ini file and then
try to append a newline to the string.  I get a runtime exception.  Here is
a snip of the code.

WriteStr(char  *string)
{
  int count=0;
 count =lstrlen(string);
 string[count + 1] = '\n' ;

The string passed in is variable in length but I must append the newline.

Can someone provide some insight?

Thanks,



Mon, 08 Mar 2004 10:24:06 GMT  
 Adding a newline to a string

Quote:

>Hi,

>I'm writing a small program were I read a string from an .ini file and then
>try to append a newline to the string.  I get a runtime exception.  Here is
>a snip of the code.

>WriteStr(char  *string)
>{
>  int count=0;
> count =lstrlen(string);
> string[count + 1] = '\n' ;

>The string passed in is variable in length but I must append the newline.

>Can someone provide some insight?

You're writing '\n' past the string's terminating nul, and you aren't
reterminating the string. To fix it, you could use:

 size_t sz = strlen(string);
 string[sz] = '\n'; // Overwrite current nul terminator
 string[sz+1] = 0; // Reterminate the string

But this is easier:

 strcat(string,"\n");

Of course, you must ensure the string has room to do this, and string
mustn't point to a string literal, because literals aren't writeable.
I don't know where WriteStr is writing the string, but in general, you
don't need to append a newline to the string, because you can write
the string and newline with two I/O's. With buffering, you don't even
take much of a speed hit. This way, you avoid the surprise of a
function called WriteStr modifying the string, WriteStr can take a
const char*, you can pass it string literals, and you can pass it the
same string multiple times without appending a newline each time. Even
if WriteStr isn't doing I/O but instead must construct a string ending
with a newline, say for display in a MessageBox, I would almost
certainly construct a temporary string rather than modify the input.
It makes sense to modify the input only if the caller needs it to be
done, but such cases are rare.

--
Doug Harrison [VC++ MVP]
Eluent Software, LLC
http://www.eluent.com
Tools for Visual C++ and Windows



Mon, 08 Mar 2004 11:15:34 GMT  
 Adding a newline to a string
Hi,

You should use lstrcat rather than append the string directly, or a access
violation will be produced.

Write the code like this.

wrtiestr(char* string)
{
    int count=lstrcat(string,"\n");

Quote:
}

//caller;

char test[MAX_STRING]="hello";
writestr(test);



Tue, 09 Mar 2004 09:12:34 GMT  
 Adding a newline to a string

Quote:

>Hi,

>I'm writing a small program were I read a string from an .ini file and then
>try to append a newline to the string.  I get a runtime exception.  Here is
>a snip of the code.

>WriteStr(char  *string)
>{
>  int count=0;
> count =lstrlen(string);
> string[count + 1] = '\n' ;

>The string passed in is variable in length but I must append the newline.

>Can someone provide some insight?

>Thanks,

The '\n' you are storing in the string is overlaying the terminating
'\0'.  Try instead
        strcat(string,"\n");

<<Remove the del for email>>



Tue, 09 Mar 2004 09:35:39 GMT  
 
 [ 4 post ] 

 Relevant Pages 

1. Adding a newline to a file

2. How do you get a newline in a textbox and string in C#

3. insert newlines into long string

4. string newline replace

5. length of strings w. newlines in them

6. stripping newlines from strings

7. newlines in string constants

8. building a string with newline characters

9. newline in string

10. Newline in commandline string

11. formatting long strings using sprintf() without including tabs or newlines

12. Remove newlines from string

 

 
Powered by phpBB® Forum Software