Re: Carriage Returns

From: Dan Pop (Dan.Pop_at_cern.ch)
Date: 10/09/03


Date: 9 Oct 2003 11:46:47 GMT

In <bm3cip$h11$1@oravannahka.helsinki.fi> Joona I Palaste <palaste@cc.helsinki.fi> writes:

>Nimmy <nojunk@please.com> scribbled the following:
>
>> I have a data file and I want to remove Carriage returns. Any one has any C
>> code/program which does this? I am working on Windows XP machine.....I don't
>> have access to UNIX machine. But I need to send this data file to the Unix
                                ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
>> machine once I remove the carriage retunrns from my XP machine.
   ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
>Here's a simple filter-type program.
>
>#include <stdio.h>
>int main(void) {
> int cr = '\r'; /* the code for carriage return */
> int c;
> while ((c = getchar()) != EOF) {
> if (c != cr) {
> putchar(c);
> }
> }
> return 0;
>}
>
>Simply redirect stdin from the file you want to remove carriage
>returns from and redirect stdout to the new file.

Had you engaged your brain, you'd have realised that your filter is
useless for its intended purpose: stdin being a text stream, it won't
see any CR character that is part of a CR+LF pair: such pairs are simply
mapped to a '\n' character, when read from a text stream on a Windows
platform. Furthermore, when you output a '\n' character, the C runtime
system will turn it into a CR+LF pair.

Of course, your program would work on a Unix system, but the OP wants
to perform the conversion on the Windows side. The solution is
incredibly simple, assuming that argv[1] contains the input file name and
argv[2] the output file name. All error checking deliberately omitted:

    int c;
    FILE *in = fopen(argv[1], "r"), *out = fopen(argv[2], "wb");

    while ((c = getc(in)) != EOF) putc(c, out);

Any CR that is part of a CR+LF pair will be automatically removed by the
C runtime system, because the input file is opened in text mode. The
opposite operation is not going to happen, because the output file is
opened in binary mode.

And you probably don't want to filter any CR that is not part of a CR+LF
pair...

As a last remark, if the file is sent using the FTP protocol, the
conversion will be automatically performed, if the transfer is made in
text mode.

Dan

-- 
Dan Pop
DESY Zeuthen, RZ group
Email: Dan.Pop@ifh.de


Relevant Pages

  • Re: Viewing Control Characters instead of the associated symbol
    ... >> was created in a unix based system. ... >> text editor every character is visible. ... >> told by one of our unix gurus that windows is incapable of seperating ... > have the same problem opening a Word document or a data file created ...
    (microsoft.public.windowsxp.general)
  • Re: Just venting (totally OT)
    ... Carriage Return means return the print head (or rather the carriage ... Unix regarded the outside world as byte-at-a-time teletypes, ... Confusingly that single key was usually labelled 'carriage ... because those missing Line Feeds have to come from ...
    (uk.people.support.depression)
  • Re: Different file defaults for Tcl 8.4?
    ... note that the carriage returns are converted to newline. ... This behavior is expected with Windows, but not with Unix. ... As the input translation mode, auto treats any of newline, carriage return, or carriage return followed by a newline as the end of line representation. ...
    (comp.lang.tcl)
  • Re: Tru64 5.1 System hangs when creating Oracle data file > 1Gb - any suggestions on cause
    ... James Blackmore wrote: ... > Last week I saw a very scary problem on a system I am doing some Unix ... > This system was configured last year, and a 23Gb data file was created ... > I don't know how to check patch levels, is their a Tru64 equivalent of ...
    (comp.unix.tru64)
  • Help with a data dump
    ... A person dropped off a CD with a MUMPS.DAT file from a UNIX ... I have a Mandrake Linux system and FreeBSD. ... figure out how to attach the mumps.dat file to see the tables. ... or steps I should take to see inside this data file, ...
    (comp.lang.mumps)