NO

Author Topic: fprintf and cr \ lf when you just want cr  (Read 4556 times)

liquiz

  • Guest
fprintf and cr \ lf when you just want cr
« on: September 30, 2008, 04:40:07 PM »
I recently updated an older program of mine to create a linux shell file as well as a dos bat file.

I deal with both linux and windows systems using a common program between the two platforms.  The program is called via a batch file or shell script, and to keep life simple I wrote a program in C to generate the equivalent script for each platform. 

In the end to get things to work on the linux side of the house I need run dos2unix on the resulting file to convert the cr\lf to just cr. 

What Im trying to do is find a way to force Pelles C to just generate the cr ...

Originally I had
if ((shtarget = fopen(cScriptType, "w")) == NULL)
and
fprintf(shtarget, "#####\n", NULL);

I changed that to

if ((shtarget = fopen(cScriptType, "wb")) == NULL)
and
fprintf(shtarget, "#####\xD", NULL);

The end result looks promising, ie notepad is a mess but it looks right in wordpad, but linux is still thinking its a cr / lf file.

Im still using 4.5x, I hate upgrading things that still work but I can and will upgrade if needed.
RSRC0009.DLL: Version 4.50.1
SUPPORT.DLL: Version 4.50.4
POCFMT.DLL: Version 4.50.3
PORC.DLL: Version 4.50.1
POBR.DLL: Version 4.50.2
POBR.EXE: Version 4.50.2
IDESPAWN.EXE: Version 4.50.1
POCC.EXE: Version 4.50.15
POLINK.EXE: Version 4.50.2

Thanks

Offline Pelle

  • Administrator
  • Member
  • *****
  • Posts: 2266
    • http://www.smorgasbordet.com
Re: fprintf and cr \ lf when you just want cr
« Reply #1 on: October 01, 2008, 11:43:02 AM »
Yes, you need the 'binary' mode (wb) for this to work - in 'text' mode (w) any LF will be converted to CR+LF. AFAIK, Linux only wants LF, and this was exactly what I got when I tried this in 4.50 (CR is for 'appletosh'):

Code: [Select]
#include <stdio.h>

int main(void)
{
    FILE *f;

    if ((f = fopen("myfile.txt", "wb")) != NULL)
    {
        fprintf(f, "aaaaa\n");
        fprintf(f, "bbbbb\n");
        fprintf(f, "ccccc\n");
        fclose(f);
    }
    return 0;
}
/Pelle

liquiz

  • Guest
Re: fprintf and cr \ lf when you just want cr
« Reply #2 on: October 01, 2008, 03:44:36 PM »
Bingo ..

Switching back from \xD to \n and opening in binary mode did the trick.