testing for the newline

B

bacadman

I have a variable that seems to be placing a newline when outputting to
a file. How can I test to see if this is the case.
 
J

Jens.Toerring

bacadman said:
I have a variable that seems to be placing a newline when outputting to
a file. How can I test to see if this is the case.

Show us the code you're using, perhaps then someone can tell you what's
going wrong. A variable as such never outputs anything. It's probably
your call of printf() (or whatever you're using to print the variables
value).
Regards, Jens
 
C

chaka

I have a variable that seems to be placing a newline when outputting to
a file. How can I test to see if this is the case.

if(line[strlen(line) - 1] == '\n')
line[strlen(line) - 1] = '\0';
 
L

Lew Pitcher

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1
I have a variable that seems to be placing a newline when outputting to
a file. How can I test to see if this is the case.


if(line[strlen(line) - 1] == '\n')
line[strlen(line) - 1] = '\0';

Which is a good way to kill off the trailing newline in a string.

But, the OP didn't make it clear that this is what is happening. It could be
that the newline is a single character (as in printf("%c",'\n'); ), or even
imbedded at some point inside a string (as in printf("%s","abc\n123"); ).

Neither of these cases are addressed by your (good) code fragment.

- --
Lew Pitcher
IT Consultant, Enterprise Data Systems,
Enterprise Technology Solutions, TD Bank Financial Group

(Opinions expressed are my own, not my employers')
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.4 (MingW32)

iD8DBQFB9VcyagVFX4UWr64RAg/qAKDwWTBYHWYam+/EVFHRH4E7mTB3BACgyPoC
NZMIpob3XRWgMM1Rgl15kTw=
=iICq
-----END PGP SIGNATURE-----
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,159
Messages
2,570,879
Members
47,414
Latest member
GayleWedel

Latest Threads

Top