Connecting Tech Pros Worldwide Forums | Help | Site Map

Carriage Returns

Nimmy
Guest
 
Posts: n/a
#1: Nov 13 '05
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



Joona I Palaste
Guest
 
Posts: n/a
#2: Nov 13 '05

re: Carriage Returns


Nimmy <nojunk@please.com> scribbled the following:[color=blue]
> Hi,[/color]
[color=blue]
> 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.[/color]

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 (palaste@cc.helsinki.fi) ---------------------------\
| 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
Johan Aurér
Guest
 
Posts: n/a
#3: Nov 13 '05

re: Carriage Returns


On Thu, 9 Oct 2003, Joona I Palaste wrote:
[color=blue]
> Nimmy <nojunk@please.com> scribbled the following:[color=green]
> > Hi,[/color]
>[color=green]
> > 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.[/color]
>
> 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.[/color]

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.

--
aurer@axis.com
Joona I Palaste
Guest
 
Posts: n/a
#4: Nov 13 '05

re: Carriage Returns


Johan Aurér <aurer@axis.com> scribbled the following:[color=blue]
> On Thu, 9 Oct 2003, Joona I Palaste wrote:[color=green]
>> Nimmy <nojunk@please.com> scribbled the following:[color=darkred]
>> > Hi,[/color]
>>[color=darkred]
>> > 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.[/color]
>>
>> 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.[/color][/color]
[color=blue]
> 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.[/color]

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 (palaste@cc.helsinki.fi) ---------------------------\
| 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
Tristan Miller
Guest
 
Posts: n/a
#5: Nov 13 '05

re: Carriage Returns


Greetings.

In article <bm3blq$ibt$1@kermit.esat.net>, Nimmy wrote:[color=blue]
> 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.[/color]

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.

--
_
_V.-o Tristan Miller [en,(fr,de,ia)] >< Space is limited
/ |`-' -=-=-=-=-=-=-=-=-=-=-=-=-=-=-= <> In a haiku, so it's hard
(7_\\ http://www.nothingisreal.com/ >< To finish what you
Dan Pop
Guest
 
Posts: n/a
#6: Nov 13 '05

re: Carriage Returns


In <bm3cip$h11$1@oravannahka.helsinki.fi> Joona I Palaste <palaste@cc.helsinki.fi> writes:
[color=blue]
>Nimmy <nojunk@please.com> scribbled the following:
>[color=green]
>> 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[/color][/color]
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^[color=blue][color=green]
>> machine once I remove the carriage retunrns from my XP machine.[/color][/color]
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ ^^^^^^^^^^^^[color=blue]
>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.[/color]

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
Default User
Guest
 
Posts: n/a
#7: Nov 13 '05

re: Carriage Returns


Tristan Miller wrote:
[color=blue]
> 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.[/color]


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



Brian Rodenborn
Joona I Palaste
Guest
 
Posts: n/a
#8: Nov 13 '05

re: Carriage Returns


Dan Pop <Dan.Pop@cern.ch> scribbled the following:[color=blue]
> In <bm3cip$h11$1@oravannahka.helsinki.fi> Joona I Palaste <palaste@cc.helsinki.fi> writes:[color=green]
>>Nimmy <nojunk@please.com> scribbled the following:[color=darkred]
>>> I have a data file and I want to remove Carriage returns. Any one has any[/color][/color][/color]
^^^^^^^^^^^ ^^^^^^^^^^^^^^^^^^^^^^^
[color=blue][color=green][color=darkred]
>>> 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[/color][/color]
> ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^[color=green][color=darkred]
>>> machine once I remove the carriage retunrns from my XP machine.[/color][/color]
> ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ ^^^^^^^^^^^^[color=green]
>>Here's a simple filter-type program.[/color][/color]

(snip source code)
[color=blue][color=green]
>>Simply redirect stdin from the file you want to remove carriage
>>returns from and redirect stdout to the new file.[/color][/color]

(snip Dan's quite well warranted criticism)
[color=blue]
> 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.[/color]
[color=blue]
> And you probably don't want to filter any CR that is not part of a CR+LF
> pair...[/color]

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 (palaste@cc.helsinki.fi) ---------------------------\
| 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
Charles Richmond
Guest
 
Posts: n/a
#9: Nov 13 '05

re: Carriage Returns


Dan Pop wrote:[color=blue]
>
> In <bm3cip$h11$1@oravannahka.helsinki.fi> Joona I Palaste <palaste@cc.helsinki.fi> writes:
>[color=green]
> >Nimmy <nojunk@please.com> scribbled the following:
> >[color=darkred]
> >> 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[/color][/color]
> ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^[color=green][color=darkred]
> >> machine once I remove the carriage retunrns from my XP machine.[/color][/color]
> ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ ^^^^^^^^^^^^[color=green]
> >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.[/color]
>
> 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.
>[/color]
So just use "freopen()" to change "stdin" and "stdout"
to binary mode. Then you can continue to use "getchar()"
and "putchar()"...

--
+----------------------------------------------------------------+
| Charles and Francis Richmond richmond at plano dot net |
+----------------------------------------------------------------+
Charles Richmond
Guest
 
Posts: n/a
#10: Nov 13 '05

re: Carriage Returns


Tristan Miller wrote:[color=blue]
>
> Greetings.
>
> In article <bm3blq$ibt$1@kermit.esat.net>, Nimmy wrote:[color=green]
> > 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.[/color]
>
> 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.
>[/color]
Or BBEdit on the Macintosh...

--
+----------------------------------------------------------------+
| Charles and Francis Richmond richmond at plano dot net |
+----------------------------------------------------------------+
Mark McIntyre
Guest
 
Posts: n/a
#11: Nov 13 '05

re: Carriage Returns


On Thu, 09 Oct 2003 13:52:05 +0200, in comp.lang.c , Tristan Miller
<psychonaut@nothingisreal.com> wrote:
[color=blue]
>Greetings.
>
>In article <bm3blq$ibt$1@kermit.esat.net>, Nimmy wrote:[color=green]
>> 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.[/color]
>
>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.[/color]

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

Even Windows Wordpad can do this.

--
Mark McIntyre
CLC FAQ <http://www.eskimo.com/~scs/C-faq/top.html>
CLC readme: <http://www.angelfire.com/ms3/bchambless0/welcome_to_clc.html>


----== Posted via Newsfeed.Com - Unlimited-Uncensored-Secure Usenet News==----
http://www.newsfeed.com The #1 Newsgroup Service in the World! >100,000 Newsgroups
---= 19 East/West-Coast Specialized Servers - Total Privacy via Encryption =---
Dan Pop
Guest
 
Posts: n/a
#12: Nov 13 '05

re: Carriage Returns


In <3F85C4C5.870FBDD4@comcast.net> Charles Richmond <richchas@comcast.net> writes:
[color=blue]
>Dan Pop wrote:[color=green]
>>
>> In <bm3cip$h11$1@oravannahka.helsinki.fi> Joona I Palaste <palaste@cc.helsinki.fi> writes:
>>[color=darkred]
>> >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[/color]
>> ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^[color=darkred]
>> >> machine once I remove the carriage retunrns from my XP machine.[/color]
>> ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ ^^^^^^^^^^^^[color=darkred]
>> >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.[/color]
>>
>> 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.
>>[/color]
>So just use "freopen()" to change "stdin" and "stdout"
>to binary mode. Then you can continue to use "getchar()"
>and "putchar()"...[/color]

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
--
Dan Pop
DESY Zeuthen, RZ group
Email: Dan.Pop@ifh.de
Dan Pop
Guest
 
Posts: n/a
#13: Nov 13 '05

re: Carriage Returns


In <bm48rs$43g$1@oravannahka.helsinki.fi> Joona I Palaste <palaste@cc.helsinki.fi> writes:
[color=blue]
>Dan Pop <Dan.Pop@cern.ch> scribbled the following:
>[color=green]
>> And you probably don't want to filter any CR that is not part of a CR+LF
>> pair...[/color]
>
>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.[/color]

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

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