From: Yoavo on
We are trying to output a file with LF (line feed) only (ascii char 10),
but we get also the CR (carriage return) character (ascii char 13).

Here is our program:

int _tmain(int argc, _TCHAR* argv[])
{
FILE *fp = fopen("tmp.bin", "w");
char c1 = 10;
fprintf(fp, "line1 %c line2", c1 );
return 0;
}


And the binary output is:

6C 69 6E 65 31 20 0D 0A 20 6C 69 6E 65 32 line1 line2


How do we output a line with LF and not CR?

Thanks,
Yoav.



From: Victor Bazarov on
Yoavo wrote:
> We are trying to output a file with LF (line feed) only (ascii char
> 10), but we get also the CR (carriage return) character (ascii char
> 13).
> Here is our program:
>
> int _tmain(int argc, _TCHAR* argv[])
> {
> FILE *fp = fopen("tmp.bin", "w");

Do

FILE *fp = fopen("tmp.bin", "wb");

(open as *binary*)

> char c1 = 10;
> fprintf(fp, "line1 %c line2", c1 );
> return 0;
> }
>
>
> And the binary output is:
>
> 6C 69 6E 65 31 20 0D 0A 20 6C 69 6E 65 32 line1 line2
>
>
> How do we output a line with LF and not CR?

Don't open it in "text" mode (the default).

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask


From: Yoavo on
I see that when I open the file in binary mode it is OK, but I must open it
in text mode.
Is it possible to open it in text mode without getting the CR ?

"Victor Bazarov" <v.Abazarov(a)comAcast.net> wrote in message
news:ft2kvm$9er$1(a)news.datemas.de...
> Yoavo wrote:
>> We are trying to output a file with LF (line feed) only (ascii char
>> 10), but we get also the CR (carriage return) character (ascii char
>> 13).
>> Here is our program:
>>
>> int _tmain(int argc, _TCHAR* argv[])
>> {
>> FILE *fp = fopen("tmp.bin", "w");
>
> Do
>
> FILE *fp = fopen("tmp.bin", "wb");
>
> (open as *binary*)
>
>> char c1 = 10;
>> fprintf(fp, "line1 %c line2", c1 );
>> return 0;
>> }
>>
>>
>> And the binary output is:
>>
>> 6C 69 6E 65 31 20 0D 0A 20 6C 69 6E 65 32 line1 line2
>>
>>
>> How do we output a line with LF and not CR?
>
> Don't open it in "text" mode (the default).
>
> V
> --
> Please remove capital 'A's when replying by e-mail
> I do not respond to top-posted replies, please don't ask
>


From: Ben Voigt [C++ MVP] on
Yoavo wrote:
> I see that when I open the file in binary mode it is OK, but I must
> open it in text mode.
> Is it possible to open it in text mode without getting the CR ?

I don't think you understand the difference between binary and text mode.
You can read/write a text file in binary mode just fine.

>
> "Victor Bazarov" <v.Abazarov(a)comAcast.net> wrote in message
> news:ft2kvm$9er$1(a)news.datemas.de...
>> Yoavo wrote:
>>> We are trying to output a file with LF (line feed) only (ascii char
>>> 10), but we get also the CR (carriage return) character (ascii char
>>> 13).
>>> Here is our program:
>>>
>>> int _tmain(int argc, _TCHAR* argv[])
>>> {
>>> FILE *fp = fopen("tmp.bin", "w");
>>
>> Do
>>
>> FILE *fp = fopen("tmp.bin", "wb");
>>
>> (open as *binary*)
>>
>>> char c1 = 10;
>>> fprintf(fp, "line1 %c line2", c1 );
>>> return 0;
>>> }
>>>
>>>
>>> And the binary output is:
>>>
>>> 6C 69 6E 65 31 20 0D 0A 20 6C 69 6E 65 32 line1 line2
>>>
>>>
>>> How do we output a line with LF and not CR?
>>
>> Don't open it in "text" mode (the default).
>>
>> V
>> --
>> Please remove capital 'A's when replying by e-mail
>> I do not respond to top-posted replies, please don't ask


From: Yoavo on
We have a large program which writes into this file, and we do not want to
change the whole program.
It gets the character as input, and in certain cases we want this character
NOT to include the CR before LF.
Is there a way to implement this without opening the file in binary mode?

Thanks again for all your answers.

"Ben Voigt [C++ MVP]" <rbv(a)nospam.nospam> wrote in message
news:Op4t7IZlIHA.5208(a)TK2MSFTNGP04.phx.gbl...
> Yoavo wrote:
>> I see that when I open the file in binary mode it is OK, but I must
>> open it in text mode.
>> Is it possible to open it in text mode without getting the CR ?
>
> I don't think you understand the difference between binary and text mode.
> You can read/write a text file in binary mode just fine.
>
>>
>> "Victor Bazarov" <v.Abazarov(a)comAcast.net> wrote in message
>> news:ft2kvm$9er$1(a)news.datemas.de...
>>> Yoavo wrote:
>>>> We are trying to output a file with LF (line feed) only (ascii char
>>>> 10), but we get also the CR (carriage return) character (ascii char
>>>> 13).
>>>> Here is our program:
>>>>
>>>> int _tmain(int argc, _TCHAR* argv[])
>>>> {
>>>> FILE *fp = fopen("tmp.bin", "w");
>>>
>>> Do
>>>
>>> FILE *fp = fopen("tmp.bin", "wb");
>>>
>>> (open as *binary*)
>>>
>>>> char c1 = 10;
>>>> fprintf(fp, "line1 %c line2", c1 );
>>>> return 0;
>>>> }
>>>>
>>>>
>>>> And the binary output is:
>>>>
>>>> 6C 69 6E 65 31 20 0D 0A 20 6C 69 6E 65 32 line1 line2
>>>>
>>>>
>>>> How do we output a line with LF and not CR?
>>>
>>> Don't open it in "text" mode (the default).
>>>
>>> V
>>> --
>>> Please remove capital 'A's when replying by e-mail
>>> I do not respond to top-posted replies, please don't ask
>
>