Carriage Returns

N

Nimmy

Hi,

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.

Thanks
 
J

Joona I Palaste

Nimmy said:
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.

--
/-- Joona Palaste ([email protected]) ---------------------------\
| Kingpriest of "The Flying Lemon Tree" G++ FR FW+ M- #108 D+ ADA N+++|
| http://www.helsinki.fi/~palaste W++ B OP+ |
\----------------------------------------- Finland rules! ------------/
"You will be given the plague."
- Montgomery Burns
 
?

=?ISO-8859-1?Q?Johan_Aur=E9r?=

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.

No, both stdin and stdout are text streams by default. A one-to-one
correspondence between the characters of a text stream and the
external representation is not mandated by the standard. You need to
use binary streams.
 
D

Dan Pop

^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
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
 
J

Joona I Palaste

No, both stdin and stdout are text streams by default. A one-to-one
correspondence between the characters of a text stream and the
external representation is not mandated by the standard. You need to
use binary streams.

Whoops. Serves me right for only ever testing this program on UNIX,
which makes no distinction between text and binary streams. Thanks for
the correction.

--
/-- Joona Palaste ([email protected]) ---------------------------\
| Kingpriest of "The Flying Lemon Tree" G++ FR FW+ M- #108 D+ ADA N+++|
| http://www.helsinki.fi/~palaste W++ B OP+ |
\----------------------------------------- Finland rules! ------------/
"B-but Angus! You're a dragon!"
- Mickey Mouse
 
T

Tristan Miller

Greetings.

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.

How are you sending it to the Unix machine? Many FTP and other file
transfer programs include options which automatically convert text files
between DOS/Unix/Mac formats. A sufficiently powerful programmer's editor
(e.g., (X)Emacs, UltraEdit) should also do this for you automatically. No
need to reinvent the wheel.
 
D

Default User

Tristan Miller wrote:
How are you sending it to the Unix machine? Many FTP and other file
transfer programs include options which automatically convert text files
between DOS/Unix/Mac formats. A sufficiently powerful programmer's editor
(e.g., (X)Emacs, UltraEdit) should also do this for you automatically. No
need to reinvent the wheel.


There are also utilities that can do the transformation, dos2unix and
such.



Brian Rodenborn
 
J

Joona I Palaste

^^^^^^^^^^^ ^^^^^^^^^^^^^^^^^^^^^^^

(snip source code)

(snip Dan's quite well warranted criticism)
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...

Why? If we're being pedantic here, there was nothing in the OP's
request that indicated we would be dealing with a text file.

--
/-- Joona Palaste ([email protected]) ---------------------------\
| Kingpriest of "The Flying Lemon Tree" G++ FR FW+ M- #108 D+ ADA N+++|
| http://www.helsinki.fi/~palaste W++ B OP+ |
\----------------------------------------- Finland rules! ------------/
"I will never display my bum in public again."
- Homer Simpson
 
C

Charles Richmond

Dan said:
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.
So just use "freopen()" to change "stdin" and "stdout"
to binary mode. Then you can continue to use "getchar()"
and "putchar()"...
 
C

Charles Richmond

Tristan said:
Greetings.



How are you sending it to the Unix machine? Many FTP and other file
transfer programs include options which automatically convert text files
between DOS/Unix/Mac formats. A sufficiently powerful programmer's editor
(e.g., (X)Emacs, UltraEdit) should also do this for you automatically. No
need to reinvent the wheel.
Or BBEdit on the Macintosh...
 
M

Mark McIntyre

Greetings.



How are you sending it to the Unix machine? Many FTP and other file
transfer programs include options which automatically convert text files
between DOS/Unix/Mac formats.

And if they don't they need a jolly good slapping.
A sufficiently powerful programmer's editor
(e.g., (X)Emacs, UltraEdit) should also do this for you automatically.

Even Windows Wordpad can do this.
 
D

Dan Pop

In said:
So just use "freopen()" to change "stdin" and "stdout"
to binary mode. Then you can continue to use "getchar()"
and "putchar()"...

Think harder...

freopen() has the nasty habit of first closing the old stream (which a
filter program cannot afford to do) and, in its pre-C99 incarnations,
of requiring a valid file name.

Dan
 
D

Dan Pop

In said:
Why? If we're being pedantic here, there was nothing in the OP's
request that indicated we would be dealing with a text file.

The mere words "carriage return" indicate a text file. '\r' becomes
devoid of any special meaning when output to a binary stream.

Dan
 

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

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,012
Latest member
RoxanneDzm

Latest Threads

Top