public inbox for gcc-help@gcc.gnu.org
 help / color / mirror / Atom feed
* Re: \n to \r\n in stdout
@ 2005-11-07 13:03 thales
  2005-11-07 13:08 ` John Love-Jensen
  0 siblings, 1 reply; 8+ messages in thread
From: thales @ 2005-11-07 13:03 UTC (permalink / raw)
  To: gcc-help

Well,

After 15 days of trying to get an answer for how the outputed file was being
corrupted and how to fix it, in 30 minutes my problem was solved here.

The setmode() function solved my problem. I didn't use it up to now because all
my references, including cplusplus.com doesn't have any citation of this
function.

Thanks a lot to Ingo Krabbe and even more to Eljay John, for this simple
resolution of my problem.

Even programming games using Allegro and SDL, programming in lots of languages
and programming lots of applications for a large company here in Brazil, I
can't get where you all are, but one day I'll be here to help people like you
all do!

Gratefully again,
Thales Medeiros.


Citando John Love-Jensen <eljay@adobe.com>:
> Hi Thales,
>
> I did not realize you were using C-style input/output.
>
> Try this before using stdout for anything else:
> setmode(fileno(stdout), O_BINARY);
>
> HTH,
> --Eljay

----------------------------------------------------------------
This message was sent using IMP, the Internet Messaging Program.

^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: \n to \r\n in stdout
  2005-11-07 13:03 \n to \r\n in stdout thales
@ 2005-11-07 13:08 ` John Love-Jensen
  2005-11-07 13:17   ` thales
  0 siblings, 1 reply; 8+ messages in thread
From: John Love-Jensen @ 2005-11-07 13:08 UTC (permalink / raw)
  To: thales, MSX to GCC

Hi Thales,

> Even programming games using Allegro and SDL, programming in lots of languages
and programming lots of applications for a large company here in Brazil, I can't
get where you all are, but one day I'll be here to help people like you all do!

Glad to be of help.  :-)

GCC is a user community supported product.  The more, the merrier!

Note:  in hindsight, your problem was actually off-topic for this forum,
since this is a forum for GCC issues in particular, not C or C++ (or other
GCC supported languages) issues themselves.  Not that I mind.  Usually the
response is a polite "Your question is off topic for this forum, but here's
the answer anyway..."

Sincerely,
--Eljay

^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: \n to \r\n in stdout
  2005-11-07 13:08 ` John Love-Jensen
@ 2005-11-07 13:17   ` thales
  0 siblings, 0 replies; 8+ messages in thread
From: thales @ 2005-11-07 13:17 UTC (permalink / raw)
  To: gcc-help

Oh! Sorry! :)

I didn't noticed after the answer that it was really a C or C++ issue.

When I was searching for an answer to my problem on the web, I read on a website
the exactly problem that my program was getting. At this point I didn't notice
yet that '\n' was being changed for "\r\n". Then I've found at this website
that this happens and that this is a configuration of the COMPILER. That I had
to look at the compiler manual for how to solve this.

Then, after the answer, I had to see that the problem was really a C or C++
issue. So I had to write at the answer what I'll write here:

Sorry for the off-topic, because initially I thought it was a compiler issue.
But I think that if I didn't go up to higher (the compiler discussion list), I
wouldn't solve my problem.

Thanks again!

Thales Medeiros.


Citando John Love-Jensen <eljay@adobe.com>:

> Hi Thales,
>
> > Even programming games using Allegro and SDL, programming in lots of
> languages
> and programming lots of applications for a large company here in Brazil, I
> can't
> get where you all are, but one day I'll be here to help people like you all
> do!
>
> Glad to be of help.  :-)
>
> GCC is a user community supported product.  The more, the merrier!
>
> Note:  in hindsight, your problem was actually off-topic for this forum,
> since this is a forum for GCC issues in particular, not C or C++ (or other
> GCC supported languages) issues themselves.  Not that I mind.  Usually the
> response is a polite "Your question is off topic for this forum, but here's
> the answer anyway..."
>
> Sincerely,
> --Eljay
>
>




----------------------------------------------------------------
This message was sent using IMP, the Internet Messaging Program.

^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: \n to \r\n in stdout
  2005-11-07 12:40 thales
@ 2005-11-07 12:52 ` John Love-Jensen
  0 siblings, 0 replies; 8+ messages in thread
From: John Love-Jensen @ 2005-11-07 12:52 UTC (permalink / raw)
  To: thales, MSX to GCC

Hi Thales,

I did not realize you were using C-style input/output.

Try this before using stdout for anything else:
setmode(fileno(stdout), O_BINARY);

HTH,
--Eljay



^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: \n to \r\n in stdout
  2005-11-07 12:08 thales
  2005-11-07 12:24 ` John Love-Jensen
@ 2005-11-07 12:41 ` Ingo Krabbe
  1 sibling, 0 replies; 8+ messages in thread
From: Ingo Krabbe @ 2005-11-07 12:41 UTC (permalink / raw)
  To: gcc-help; +Cc: thales

[-- Attachment #1: Type: text/plain, Size: 1369 bytes --]

This is no compiler problem but a problem of your system (Windows, Dos maybe, 
whose behaviour is to do such things).

You can try to change the terminal settings for the stdout line, which has a 
flag for such output postprocessings.  But such a discussion opens a vast 
area of misunderstandings.

Try to set the terminal parameters from your terminal instead.  This might 
help too.

Or just write directly to a file.

Use dup2 to duplicate your stdout descriptor and set some flags to write in 
binary mode to the descriptor might also help (never tried though).

Maybe fdopen(STDOUT_FILENO, "wb"), works too ?!

BYE INGO

Am Montag, 7. November 2005 13:09 schrieb thales@virtualbs.com.br:
> Hi all!
>
> I've looked at so many places that I'm thinking it won't have ways to
> resolve this problem. Then I remembered that exists this discussion list.
>
> So, here is my question:
>
> Please, but please... How can I prevent the compiler of substituting the
> character '\n' for the characters "\r\n"?
>
> I'm trying to open a binary file and print it to the stdout, but
> substituting '\n' for "\r\n", the file generated is corrupted.
>
> Thanks you all!
>
> Gratefully,
> Thales Medeiros.
>
> ----------------------------------------------------------------
> This message was sent using IMP, the Internet Messaging Program.

[-- Attachment #2: Type: application/pgp-signature, Size: 189 bytes --]

^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: \n to \r\n in stdout
@ 2005-11-07 12:40 thales
  2005-11-07 12:52 ` John Love-Jensen
  0 siblings, 1 reply; 8+ messages in thread
From: thales @ 2005-11-07 12:40 UTC (permalink / raw)
  To: gcc-help

Hi, John!

The file was already opened in binary mode. The problem isn't in the file
opened, but in the file outputed. Understand what's happening:

FILE *fp1, *fp2;
fp1=fopen("source.zip","rb");
fp2=fopen("destiny.zip","wb");
while(!feof(fp1)) fputc(fgetc(fp1),fp2);
fclose(fp1);
fclose(fp2);

This works! But I don't want to copy files. I need to open the source.zip and
send it to the stdout! So I tried this:

FILE *fp1=fopen("source.zip","rb");
while(!feof(fp1)) putchar(fgetc(fp1));
fclose(fp1);

This doesn't work, because when the character got from the file fp1 is a '\n',
then the program outputs the combination "\r\n" in place. This corrupts the
file.

Thanks for the attention and for the fast answer! I hope you can help me.

Gratefully,
Thales Medeiros.


Citando John Love-Jensen <eljay@adobe.com>:

> Hi Thales,
>
> > I'm trying to open a binary file and print it to the stdout, but
> substituting
> '\n' for "\r\n", the file generated is corrupted.
>
> Have you tried opening the file in binary mode?
>
> #include <fstream>
>
> std::fstream f("myfile.bin",
>    ios_base::binary | ios_base::in | ios_base::out);
>
> HTH,
> --Eljay

----------------------------------------------------------------
This message was sent using IMP, the Internet Messaging Program.

^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: \n to \r\n in stdout
  2005-11-07 12:08 thales
@ 2005-11-07 12:24 ` John Love-Jensen
  2005-11-07 12:41 ` Ingo Krabbe
  1 sibling, 0 replies; 8+ messages in thread
From: John Love-Jensen @ 2005-11-07 12:24 UTC (permalink / raw)
  To: thales, MSX to GCC

Hi Thales,

> I'm trying to open a binary file and print it to the stdout, but substituting
'\n' for "\r\n", the file generated is corrupted.

Have you tried opening the file in binary mode?

#include <fstream>

std::fstream f("myfile.bin",
   ios_base::binary | ios_base::in | ios_base::out);

HTH,
--Eljay

^ permalink raw reply	[flat|nested] 8+ messages in thread

* \n to \r\n in stdout
@ 2005-11-07 12:08 thales
  2005-11-07 12:24 ` John Love-Jensen
  2005-11-07 12:41 ` Ingo Krabbe
  0 siblings, 2 replies; 8+ messages in thread
From: thales @ 2005-11-07 12:08 UTC (permalink / raw)
  To: gcc-help

Hi all!

I've looked at so many places that I'm thinking it won't have ways to resolve
this problem. Then I remembered that exists this discussion list.

So, here is my question:

Please, but please... How can I prevent the compiler of substituting the
character '\n' for the characters "\r\n"?

I'm trying to open a binary file and print it to the stdout, but substituting
'\n' for "\r\n", the file generated is corrupted.

Thanks you all!

Gratefully,
Thales Medeiros.

----------------------------------------------------------------
This message was sent using IMP, the Internet Messaging Program.

^ permalink raw reply	[flat|nested] 8+ messages in thread

end of thread, other threads:[~2005-11-07 13:17 UTC | newest]

Thread overview: 8+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2005-11-07 13:03 \n to \r\n in stdout thales
2005-11-07 13:08 ` John Love-Jensen
2005-11-07 13:17   ` thales
  -- strict thread matches above, loose matches on Subject: below --
2005-11-07 12:40 thales
2005-11-07 12:52 ` John Love-Jensen
2005-11-07 12:08 thales
2005-11-07 12:24 ` John Love-Jensen
2005-11-07 12:41 ` Ingo Krabbe

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).