473,387 Members | 1,766 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Carriage Returns

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
Nov 13 '05 #1
12 5574
Nimmy <no****@please.com> scribbled the following:
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.


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 (pa*****@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
Nov 13 '05 #2
On Thu, 9 Oct 2003, Joona I Palaste wrote:
Nimmy <no****@please.com> scribbled the following:
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.


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.

--
au***@axis.com
Nov 13 '05 #3
Johan Aurér <au***@axis.com> scribbled the following:
On Thu, 9 Oct 2003, Joona I Palaste wrote:
Nimmy <no****@please.com> scribbled the following:
> 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.


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.


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 (pa*****@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
Nov 13 '05 #4
Greetings.

In article <bm**********@kermit.esat.net>, Nimmy wrote:
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.

--
_
_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
Nov 13 '05 #5
In <bm**********@oravannahka.helsinki.fi> Joona I Palaste <pa*****@cc.helsinki.fi> writes:
Nimmy <no****@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: Da*****@ifh.de
Nov 13 '05 #6
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
Nov 13 '05 #7
Dan Pop <Da*****@cern.ch> scribbled the following:
In <bm**********@oravannahka.helsinki.fi> Joona I Palaste <pa*****@cc.helsinki.fi> writes:
Nimmy <no****@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.

(snip source code)
Simply redirect stdin from the file you want to remove carriage
returns from and redirect stdout to the new file.


(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 (pa*****@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
Nov 13 '05 #8
Dan Pop wrote:

In <bm**********@oravannahka.helsinki.fi> Joona I Palaste <pa*****@cc.helsinki.fi> writes:
Nimmy <no****@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.

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 |
+----------------------------------------------------------------+
Nov 13 '05 #9
Tristan Miller wrote:

Greetings.

In article <bm**********@kermit.esat.net>, Nimmy wrote:
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.

Or BBEdit on the Macintosh...

--
+----------------------------------------------------------------+
| Charles and Francis Richmond richmond at plano dot net |
+----------------------------------------------------------------+
Nov 13 '05 #10
On Thu, 09 Oct 2003 13:52:05 +0200, in comp.lang.c , Tristan Miller
<ps********@nothingisreal.com> wrote:
Greetings.

In article <bm**********@kermit.esat.net>, Nimmy wrote:
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.


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.

--
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 =---
Nov 13 '05 #11
In <3F***************@comcast.net> Charles Richmond <ri******@comcast.net> writes:
Dan Pop wrote:

In <bm**********@oravannahka.helsinki.fi> Joona I Palaste <pa*****@cc.helsinki.fi> writes:
>Nimmy <no****@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.

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
--
Dan Pop
DESY Zeuthen, RZ group
Email: Da*****@ifh.de
Nov 13 '05 #12
In <bm**********@oravannahka.helsinki.fi> Joona I Palaste <pa*****@cc.helsinki.fi> writes:
Dan Pop <Da*****@cern.ch> scribbled the following:
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.


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: Da*****@ifh.de
Nov 13 '05 #13

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Les Juby | last post by:
Can someone please help with a suggestion as to how I can keep the formatting (carriage returns) that the user enters into a memo field and then display that later. I figured I might be able to...
2
by: Andrew Chanter | last post by:
I have a VBA function that returns a string including "vbcr" (VB Carriage Return) to seperate a list into multiple rows, eg Item1 & vbcr & Item2 & vbcr & Item3 This works as planned in the...
2
by: eagleofjade | last post by:
I am trying to import data from a Word document into an Access table with VBA. The Word document is a form which has various fields. One of the fields is a field for notes. In some cases, this...
2
by: Matt Mercer | last post by:
Hi all, I am having a frustration problem, and I have read about 25 newsgroup postings that do not have a satisfying answer :) The problem appears to be common where carriage returns are lost...
8
by: TheDude5B | last post by:
Hi, I have some data which is stored in my MySQL database as TEXT. when the data is entered in, it has some carriage returns in it, and this can be seen when querying the data using MySQL Query...
7
by: mattrapoport | last post by:
I have a page with a div on it. The div displays a user comment. When the user logs into this page, their current comment is pulled from a db and displayed in the div. The user can edit the...
2
by: GregBeagle | last post by:
Windows XP I have a simple form from which I want to generate an email with the contents of the form. I use carriage returns to format the content for readability. When I test it on my computer...
0
by: markus.shure | last post by:
Hi, I'm noticed a problem testing a JAX-WS client with a WSE server. The JAX-WS client adds carriage returns to a SOAP header element that is signed. This causes the WSE server to raise an...
11
by: evenlater | last post by:
My db allows the user to send email via CDO. The body of the email is determined in code. I have built an email form with To, CC and Subject lines and a large text box for the body of the message...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.