Re: can I split a C string



"Richard Heathfield" <rjh@xxxxxxxxxxxxxxx> wrote in message
news:NPydncrGj7D_v-7VRVnyugA@xxxxxxxxx
KIRAN said:

On Jul 7, 10:13 pm, Keith Thompson <ks...@xxxxxxx> wrote:
KIRAN <kira...@xxxxxxxxx> writes:
can i split a C string like this?
char * p = "Hello \
World\n\r";
is this according to standard?

In addition to the answers you've gotten (yes, you can, but there's a
much better way), why does your string end in "\n\r"?

Actually I am writing portable code that runs on windows & ARM9 .
my print function will print

1. (WINDOWS)the string on console window(here "\r" is not
required(correct me if am wrong)

2. (ARM) the string on hyperterminal(UART)

If that's meant
to be a Windows-style line ending, then (a) it's "\r\n", not "\n\r",
and

Why?

Because there was a teletype device in the 1960s on which moving the
carriage (the thing that carried the print head) from the right edge of
the paper to the left edge took twice as long as it took for a character
to arrive over the wire. To avoid dropping characters during the return of
the carriage from right to left, the designers split the task into two -
returning the carriage to the left edge of the paper, and feeding the
paper up a line - so that it would require two input characters to do this
one task, and thus the carriage would have time to do its thing without
any characters being dropped. (Essentially, the linefeed started life as a
padding character.)

If you're talking about the ASR33, this I think /required/ the two
characters, CR to move the head to the left, and LF to advance a line. They
were two different operations.

For convenience, whenever the user pressed Return, it was echoed back as CR
LF.


CP/M uncritically adopted the CR/LF convention, wasting one byte per line
of text file. Recognising this, the Unix guys dropped the CR, whereas the
Mac folks dropped the LF. 86-DOS (QDOS) inherited the pair from CP/M, and
then was rebadged as MS-DOS, which was later rebadged as Windows. So even
in 2008, Windows users are stuck with this two-character newline encoding
several decades after the obsolescence of the hardware whose shortcomings
it was introduced to work around.

I think Windows itself didn't help by requiring both CR and LF to be present
when sending text to the console, just like the teletype, otherwise lines
would either overwrite themselves (LF missing), or newlines would start
halfway across the screen (CR missing). So it was advisable to keep the CRLF
ending in a text file.

On Jul 7, 10:13 pm, Keith Thompson <ks...@xxxxxxx> wrote:
If that's meant
to be a Windows-style line ending, then (a) it's "\r\n", not "\n\r",

In text mode these will result in CR CR LF or CR LF CR, even more waste. And
if sent to a file, a lot of confusion for programs that expect specific line
endings.

--
Bartc


.



Relevant Pages

  • Re: How many bytes per Italian character?
    ... yes I know how to ask Windows CE how big a buffer I'm going to need. ... and characters in Italian are the same size as characters in English, ... So have you detected that WinCE will cause a buffer overrun? ... It is not an error to tell you a string might be longer than it actually ...
    (microsoft.public.vc.mfc)
  • Re: How many bytes per Italian character?
    ... In a general case where I don't know the expected length of the string, yes I know how to ask Windows CE how big a buffer I'm going to need. ... In some cases I might still verify the type that comes back to see if it's really a string or not, but in the general case I know how to inspect the retorted result to find out what type it's registered as today. ... correlation between characters and code points. ...
    (microsoft.public.vc.mfc)
  • Re: Joining 2 strings
    ... > posted above overwrite the 3 lines, ... "carriage return, line feed". ... Now, if you print that string to the screen, Windows prints "abcdefghi", ...
    (comp.lang.perl.misc)
  • Re: can I split a C string
    ... Actually I am writing portable code that runs on windows & ARM9. ... the string on console window(here "\r" is not ... the carriage from right to left, the designers split the task into two - ... paper up a line - so that it would require two input characters to do this ...
    (comp.lang.c)
  • Re: After being send through socket string is corrupted
    ... unix text file in windows notepad and you will see a square character ... > that only contains a string to a server. ... > it contains the correct text but additionally there are strange characters ...
    (comp.lang.java.programmer)