ChttpFile receives and extra \r in its carriage return

Posted on 2004-04-14
Last Modified: 2013-11-20
From the server I send 1024 bytes.  On the client I receive 1037 bytes.  When comparing the two files they are identical meaning I received the 1024 bytes.  However, each \r\n in the file is not \r\r\n.  Or in binary 0D 0D 0A.

So, my question is what is adding the extra 0D to the carriage returns?  On the server I its an executable that just passes it down stdout.  I've verified that the data it sends is in fact 1024 bytes.

On the Client I use CHttpFile::Read to get the data and the data has the extra character.

Any ideas?

Question by:Jas001
  • 4
  • 2
  • 2
  • +2

Author Comment

ID: 10826415
However, each \r\n in the file is not \r\r\n.
Should be:
However, each \r\n in the file is NOW \r\r\n.

Expert Comment

ID: 10827497
as far as i know soft line breaks (\r\r\n) are only generated by multiline edit controls . is your server using any edit control ?what function do you use on the server to send data ?

Author Comment

ID: 10827604
I just write it out to stdout like so:
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

LVL 11

Expert Comment

ID: 10827656
ASCII transmission mode from a server to a client usually replaces the \n with \r\n, but since the \r\n is already there you get the \r\r\n.

Can you switch to binary mode?

Expert Comment

ID: 10827703
I see you use fwrite.. do you use "b" flag while fopen'ing the file? If not, can you please to add a b (binary) flag next to your "w"... such as:

fopen( filename, "wb+" );

Author Comment

ID: 10828459
There is no file to open.  Im just writing to standard out like so:
fwrite(buff,sizeof(char),len,stdout);// to standard out.  

The mechanism to send it to the Client is handled by the server via the CHTTP Class.
The input file I'm sending is an ASCII file so I wouldn't want  to open it in binary I don't think.

KurtVon, why would the server parse the file and add a carriage return?  Is there a way to tell the server when I request using CHTTP that I want binary?  And would that mess up my return file since it is ascii and requires the spacing?

I have a fix for the problem by just stripping the extra carriage return out of the data, but I sure would like to know a way around this.
LVL 10

Accepted Solution

Mercantilum earned 500 total points
ID: 10829044
I guess your server is Windows based.

Try this before calling fwrite.

    #include <io.h>
    #include <fcntl.h>

   /* Set "stdout" to have binary mode */
   int result;

   result = _setmode( _fileno( stdout ), _O_BINARY );
   if( result == -1 )
      perror( "Cannot set mode" );
      printf( "'stdin' successfully changed to binary mode\n" );

From the help of VC++ 6.

(  If it does not work, you can try using  freopen, using "con" as a file name (windows) or "/dev/tty" (unix/linux), with "wb" attribute.  )


Expert Comment

ID: 10830838
In your question, you are telling us that there is a file containing received data, and you're checking it out, seeing \r\r\n there.. That's why I wanted you to open that file in binary mode. Or, are you using shell redirection > or >> to save the received data?
You say, you receive 1024 bytes from CHttpFile::Read() and you find 1037 bytes in the output file. That means, there is a problem saving this data. Why don't you just open a binary file like we all mentioned above, and write the received data in there using fwrite()? Then check that file size, data in it etc..
LVL 11

Expert Comment

ID: 10833547
Binary mode isn't just for opening binary files.  Ascii mode exists solely so that the operating system knows how to translate the text for proper processing.  As such, it's a bit of a throwback to when systems used different text formats (well, some still do).  Binary format still reads ascii, but it tells the OS (or whatever library is handling the transfer) not to alter the bytes in any way.

I'm afraid I can't be specific because I have no idea what a CHTTP is.  I assume that is the library you are using to make the connection, but I don't know any library where you make a connection with a CHTTP object and then transmit using stdout.

Author Comment

ID: 10861254
Thanks guys for your comments. I'm going to pick Mercantilum's comment but I wanted to clarify a few things.

Caner elci: There is no file to open in binary mode.  I have to read the data from the CHTTPfile and write the file.  I've stated above that when the data reaches the server there are already characters added to the datastream.  So, I can't open the file in binary, its already been modified.

KurtVon:  CHttp class in MFC is what I'm using.  Its usually used to request a web page or something of that nature and receives a CHttpFile in return.  Instead, I'm requesting an executable which is then run on the server and writes to standardout which will be returned to the CHTTP class as a response.  The data leaving the executable is correct, but the data received by the client has the extra carriage return in it.  

Thanks for your input guys!

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Show shut-down message as Windows 8 shuts down. 9 106
Expand macro to ask for filename column 8 37
Looking for a specific application/software 2 119
centeredAverage challenge 8 186
Introduction: Hints for the grid button.  Nested classes, templated collections.  Squash that darned bug! Continuing from the sixth article about sudoku.   Open the project in visual studio. First we will finish with the SUD_SETVALUE messa…
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
Attackers love to prey on accounts that have privileges. Reducing privileged accounts and protecting privileged accounts therefore is paramount. Users, groups, and service accounts need to be protected to help protect the entire Active Directory …

726 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question