[MinGW] Control characters differences ?

classic Classic list List threaded Threaded
15 messages Options
Reply | Threaded
Open this post in threaded view
|

[MinGW] Control characters differences ?

Maxime Lecourt
Hi,

for a project I'm generating and reading a file that is an Intermediate Representation.
Generating it is no problem, under Linux or Windows with mingw.

I have problems when reading it on windows only.
I can read a file generated either on Windows or Linux from Linux (Ubuntu), but not with windows.

    if (read(fd, &head, sizeof(IR_Header)) != sizeof(IR_Header))
        return 1;
#if defined(__unix)
    if (read(fd, &metatable, NUM_OF_TABLES * sizeof(MetaTable_t)) !=  NUM_OF_TABLES * sizeof(MetaTable_t))
        return 2;
#elif defined(__WIN32) || defined(__WIN64)
    int budile = read(fd, &metatable, NUM_OF_TABLES * sizeof(MetaTable_t));
    printf("real size: %d, theory size %d", budile, NUM_OF_TABLES * sizeof(MetaTable_t));
    if ( budile !=  NUM_OF_TABLES * sizeof(MetaTable_t))
            return 2;
#endif

That is basically the code incriminated. I modified it under windows so I could check the values, and I get as a result :
real size: 321; theory size 624
(function returns non zero value in case of error)

I'm guessing it might come from some difference linked to Control Characters, but what can I do in this case ?


Regards,

Maxime

------------------------------------------------------------------------------
WhatsUp Gold - Download Free Network Management Software
The most intuitive, comprehensive, and cost-effective network
management toolset available today.  Delivers lowest initial
acquisition cost and overall TCO of any competing solution.
http://p.sf.net/sfu/whatsupgold-sd
_______________________________________________
MinGW-users mailing list
[hidden email]

This list observes the Etiquette found at
http://www.mingw.org/Mailing_Lists.
We ask that you be polite and do the same.  Disregard for the list etiquette may cause your account to be moderated.

_______________________________________________
You may change your MinGW Account Options or unsubscribe at:
https://lists.sourceforge.net/lists/listinfo/mingw-users
Also: mailto:[hidden email]?subject=unsubscribe
Reply | Threaded
Open this post in threaded view
|

Re: [MinGW] Control characters differences ?

Steve Simon
This is not reccomended practice, the compiler for any
machine/architecture/os is free to change structure packing
for its own reasons. Thus a struct fwrite'en on one machine
may well (often won't) be correct when fread back on another.

Use a noddy function to read the data as bytes and it will always
work given any packing or endian-ness. Here is a quote from
Rob Pike who has a better turn of phrase than I.


        ulong
        getlong(void)
        {
                ulong l;

                l = (getchar()&0xFF)<<24;
                l |= (getchar()&0xFF)<<16;
                l |= (getchar()&0xFF)<<8;
                l |= (getchar()&0xFF)<<0;
                return l;
        }

        Efficiency hounds will argue that this method is
        unnecessarily slow and clumsy when the executing
        machine has the same byte order (and padding and
        alignment) as the data.

        The CPU cost of I/O processing is rarely the bottleneck
        for an application, however, and the gain in simplicity
        of porting and maintaining the code greatly outweighs the
        minor speed loss from handling data in this general way.

This email and any attachments are confidential, may be legally privileged and are intended for the use of the addressee only. If you are not the intended recipient, please note that any use, disclosure, printing or copying of this email is strictly prohibited and may be unlawful. If received in error, please delete this email and any attachments and confirm this to the sender.

Snell Limited, registered number 1160119
Registered in England, registered office at Hartman House, Danehill, Lower Earley, Reading, Berkshire RG6 4PB

------------------------------------------------------------------------------
WhatsUp Gold - Download Free Network Management Software
The most intuitive, comprehensive, and cost-effective network
management toolset available today.  Delivers lowest initial
acquisition cost and overall TCO of any competing solution.
http://p.sf.net/sfu/whatsupgold-sd
_______________________________________________
MinGW-users mailing list
[hidden email]

This list observes the Etiquette found at
http://www.mingw.org/Mailing_Lists.
We ask that you be polite and do the same.  Disregard for the list etiquette may cause your account to be moderated.

_______________________________________________
You may change your MinGW Account Options or unsubscribe at:
https://lists.sourceforge.net/lists/listinfo/mingw-users
Also: mailto:[hidden email]?subject=unsubscribe
Reply | Threaded
Open this post in threaded view
|

Re: [MinGW] Control characters differences ?

Maxime Lecourt
I believe my problem is slightly different.
A file written on windows system can't be read by the same windows system (same PC, same user, same session).
It's only after that that I realized I could read that file on Linux system.

Still, you give me some matters to think and work on, thank you.

Maxime


2011/5/5 Steve Simon <[hidden email]>
This is not reccomended practice, the compiler for any
machine/architecture/os is free to change structure packing
for its own reasons. Thus a struct fwrite'en on one machine
may well (often won't) be correct when fread back on another.

Use a noddy function to read the data as bytes and it will always
work given any packing or endian-ness. Here is a quote from
Rob Pike who has a better turn of phrase than I.


       ulong
       getlong(void)
       {
               ulong l;

               l = (getchar()&0xFF)<<24;
               l |= (getchar()&0xFF)<<16;
               l |= (getchar()&0xFF)<<8;
               l |= (getchar()&0xFF)<<0;
               return l;
       }

       Efficiency hounds will argue that this method is
       unnecessarily slow and clumsy when the executing
       machine has the same byte order (and padding and
       alignment) as the data.

       The CPU cost of I/O processing is rarely the bottleneck
       for an application, however, and the gain in simplicity
       of porting and maintaining the code greatly outweighs the
       minor speed loss from handling data in this general way.

This email and any attachments are confidential, may be legally privileged and are intended for the use of the addressee only. If you are not the intended recipient, please note that any use, disclosure, printing or copying of this email is strictly prohibited and may be unlawful. If received in error, please delete this email and any attachments and confirm this to the sender.

Snell Limited, registered number 1160119
Registered in England, registered office at Hartman House, Danehill, Lower Earley, Reading, Berkshire RG6 4PB

------------------------------------------------------------------------------
WhatsUp Gold - Download Free Network Management Software
The most intuitive, comprehensive, and cost-effective network
management toolset available today.  Delivers lowest initial
acquisition cost and overall TCO of any competing solution.
http://p.sf.net/sfu/whatsupgold-sd
_______________________________________________
MinGW-users mailing list
[hidden email]

This list observes the Etiquette found at
http://www.mingw.org/Mailing_Lists.
We ask that you be polite and do the same.  Disregard for the list etiquette may cause your account to be moderated.

_______________________________________________
You may change your MinGW Account Options or unsubscribe at:
https://lists.sourceforge.net/lists/listinfo/mingw-users
Also: mailto:[hidden email]?subject=unsubscribe


------------------------------------------------------------------------------
WhatsUp Gold - Download Free Network Management Software
The most intuitive, comprehensive, and cost-effective network
management toolset available today.  Delivers lowest initial
acquisition cost and overall TCO of any competing solution.
http://p.sf.net/sfu/whatsupgold-sd
_______________________________________________
MinGW-users mailing list
[hidden email]

This list observes the Etiquette found at
http://www.mingw.org/Mailing_Lists.
We ask that you be polite and do the same.  Disregard for the list etiquette may cause your account to be moderated.

_______________________________________________
You may change your MinGW Account Options or unsubscribe at:
https://lists.sourceforge.net/lists/listinfo/mingw-users
Also: mailto:[hidden email]?subject=unsubscribe
Reply | Threaded
Open this post in threaded view
|

Re: [MinGW] Control characters differences ?

Tor Lillqvist
Do you open the file in binary mode?

--tml

------------------------------------------------------------------------------
WhatsUp Gold - Download Free Network Management Software
The most intuitive, comprehensive, and cost-effective network
management toolset available today.  Delivers lowest initial
acquisition cost and overall TCO of any competing solution.
http://p.sf.net/sfu/whatsupgold-sd
_______________________________________________
MinGW-users mailing list
[hidden email]

This list observes the Etiquette found at
http://www.mingw.org/Mailing_Lists.
We ask that you be polite and do the same.  Disregard for the list etiquette may cause your account to be moderated.

_______________________________________________
You may change your MinGW Account Options or unsubscribe at:
https://lists.sourceforge.net/lists/listinfo/mingw-users
Also: mailto:[hidden email]?subject=unsubscribe
Reply | Threaded
Open this post in threaded view
|

Re: [MinGW] Control characters differences ?

Maxime Lecourt
I'm not sure, but I don't think so.
I open the file with the open() function (should I rather rewrite the code and use the fopen function ?).

    fd = open(Ir_File, O_RDONLY);

Maxime

2011/5/6 Tor Lillqvist <[hidden email]>
Do you open the file in binary mode?

--tml

------------------------------------------------------------------------------
WhatsUp Gold - Download Free Network Management Software
The most intuitive, comprehensive, and cost-effective network
management toolset available today.  Delivers lowest initial
acquisition cost and overall TCO of any competing solution.
http://p.sf.net/sfu/whatsupgold-sd
_______________________________________________
MinGW-users mailing list
[hidden email]

This list observes the Etiquette found at
http://www.mingw.org/Mailing_Lists.
We ask that you be polite and do the same.  Disregard for the list etiquette may cause your account to be moderated.

_______________________________________________
You may change your MinGW Account Options or unsubscribe at:
https://lists.sourceforge.net/lists/listinfo/mingw-users
Also: mailto:[hidden email]?subject=unsubscribe


------------------------------------------------------------------------------
WhatsUp Gold - Download Free Network Management Software
The most intuitive, comprehensive, and cost-effective network
management toolset available today.  Delivers lowest initial
acquisition cost and overall TCO of any competing solution.
http://p.sf.net/sfu/whatsupgold-sd
_______________________________________________
MinGW-users mailing list
[hidden email]

This list observes the Etiquette found at
http://www.mingw.org/Mailing_Lists.
We ask that you be polite and do the same.  Disregard for the list etiquette may cause your account to be moderated.

_______________________________________________
You may change your MinGW Account Options or unsubscribe at:
https://lists.sourceforge.net/lists/listinfo/mingw-users
Also: mailto:[hidden email]?subject=unsubscribe
Reply | Threaded
Open this post in threaded view
|

Re: [MinGW] Control characters differences ?

Tor Lillqvist
>> Do you open the file in binary mode?

> I'm not sure, but I don't think so.
> I open the file with the open() function (should I rather rewrite the code
> and use the fopen function ?).
>
>     fd = open(Ir_File, O_RDONLY);

No need to use fopen, just use O_BINARY in addition to the O_RDONLY
for binary files. On Windows, the C library makes a difference between
"binary" and "text" files.

(In fopen(), add a "b" to the mode string.)

--tml

------------------------------------------------------------------------------
WhatsUp Gold - Download Free Network Management Software
The most intuitive, comprehensive, and cost-effective network
management toolset available today.  Delivers lowest initial
acquisition cost and overall TCO of any competing solution.
http://p.sf.net/sfu/whatsupgold-sd
_______________________________________________
MinGW-users mailing list
[hidden email]

This list observes the Etiquette found at
http://www.mingw.org/Mailing_Lists.
We ask that you be polite and do the same.  Disregard for the list etiquette may cause your account to be moderated.

_______________________________________________
You may change your MinGW Account Options or unsubscribe at:
https://lists.sourceforge.net/lists/listinfo/mingw-users
Also: mailto:[hidden email]?subject=unsubscribe
Reply | Threaded
Open this post in threaded view
|

Re: [MinGW] Control characters differences ?

Maxime Lecourt
I see.
I changed it (still using open), but problem is still there.
    ir = open(Ir_File, O_RDONLY|O_BINARY);


I went in fcntl.h file, to see how O_BINARY was defined
#define    _O_BINARY    0x8000    /* Input and output is not translated. */
#define    O_BINARY    _O_BINARY

So that should have solved my problem, but did not.
Does that mean I have another problem than that translation of character ?

Maxime


2011/5/6 Tor Lillqvist <[hidden email]>
>> Do you open the file in binary mode?

> I'm not sure, but I don't think so.
> I open the file with the open() function (should I rather rewrite the code
> and use the fopen function ?).
>
>     fd = open(Ir_File, O_RDONLY);

No need to use fopen, just use O_BINARY in addition to the O_RDONLY
for binary files. On Windows, the C library makes a difference between
"binary" and "text" files.

(In fopen(), add a "b" to the mode string.)

--tml

------------------------------------------------------------------------------
WhatsUp Gold - Download Free Network Management Software
The most intuitive, comprehensive, and cost-effective network
management toolset available today.  Delivers lowest initial
acquisition cost and overall TCO of any competing solution.
http://p.sf.net/sfu/whatsupgold-sd
_______________________________________________
MinGW-users mailing list
[hidden email]

This list observes the Etiquette found at
http://www.mingw.org/Mailing_Lists.
We ask that you be polite and do the same.  Disregard for the list etiquette may cause your account to be moderated.

_______________________________________________
You may change your MinGW Account Options or unsubscribe at:
https://lists.sourceforge.net/lists/listinfo/mingw-users
Also: mailto:[hidden email]?subject=unsubscribe


------------------------------------------------------------------------------
WhatsUp Gold - Download Free Network Management Software
The most intuitive, comprehensive, and cost-effective network
management toolset available today.  Delivers lowest initial
acquisition cost and overall TCO of any competing solution.
http://p.sf.net/sfu/whatsupgold-sd
_______________________________________________
MinGW-users mailing list
[hidden email]

This list observes the Etiquette found at
http://www.mingw.org/Mailing_Lists.
We ask that you be polite and do the same.  Disregard for the list etiquette may cause your account to be moderated.

_______________________________________________
You may change your MinGW Account Options or unsubscribe at:
https://lists.sourceforge.net/lists/listinfo/mingw-users
Also: mailto:[hidden email]?subject=unsubscribe
Reply | Threaded
Open this post in threaded view
|

Re: [MinGW] Control characters differences ?

Steve Simon
Was the file opened in binary mode when you wrote the data too?
if you write the file on unix/linux/etc binary mode has no meaning,
all files are binary in effect, however if you write a file which contains
a newline in ascii mode on windows it will have a carriage return appended to
it.

you need to both read and writethe file in binary mode on windows
if the data is somthing like a structure.

-Steve

This email and any attachments are confidential, may be legally privileged and are intended for the use of the addressee only. If you are not the intended recipient, please note that any use, disclosure, printing or copying of this email is strictly prohibited and may be unlawful. If received in error, please delete this email and any attachments and confirm this to the sender.

Snell Limited, registered number 1160119
Registered in England, registered office at Hartman House, Danehill, Lower Earley, Reading, Berkshire RG6 4PB

------------------------------------------------------------------------------
WhatsUp Gold - Download Free Network Management Software
The most intuitive, comprehensive, and cost-effective network
management toolset available today.  Delivers lowest initial
acquisition cost and overall TCO of any competing solution.
http://p.sf.net/sfu/whatsupgold-sd
_______________________________________________
MinGW-users mailing list
[hidden email]

This list observes the Etiquette found at
http://www.mingw.org/Mailing_Lists.
We ask that you be polite and do the same.  Disregard for the list etiquette may cause your account to be moderated.

_______________________________________________
You may change your MinGW Account Options or unsubscribe at:
https://lists.sourceforge.net/lists/listinfo/mingw-users
Also: mailto:[hidden email]?subject=unsubscribe
Reply | Threaded
Open this post in threaded view
|

Re: [MinGW] Control characters differences ?

Maxime Lecourt
In reply to this post by Maxime Lecourt
I'm an idiot.

I have two programs opening the file, and the one I made the changes in is not the one I use.
Problem solved, thank you.

Maxime

2011/5/6 Maxime Lecourt <[hidden email]>
I see.
I changed it (still using open), but problem is still there.
    ir = open(Ir_File, O_RDONLY|O_BINARY);


I went in fcntl.h file, to see how O_BINARY was defined
#define    _O_BINARY    0x8000    /* Input and output is not translated. */
#define    O_BINARY    _O_BINARY

So that should have solved my problem, but did not.
Does that mean I have another problem than that translation of character ?

Maxime


2011/5/6 Tor Lillqvist <[hidden email]>
>> Do you open the file in binary mode?

> I'm not sure, but I don't think so.
> I open the file with the open() function (should I rather rewrite the code
> and use the fopen function ?).
>
>     fd = open(Ir_File, O_RDONLY);

No need to use fopen, just use O_BINARY in addition to the O_RDONLY
for binary files. On Windows, the C library makes a difference between
"binary" and "text" files.

(In fopen(), add a "b" to the mode string.)

--tml

------------------------------------------------------------------------------
WhatsUp Gold - Download Free Network Management Software
The most intuitive, comprehensive, and cost-effective network
management toolset available today.  Delivers lowest initial
acquisition cost and overall TCO of any competing solution.
http://p.sf.net/sfu/whatsupgold-sd
_______________________________________________
MinGW-users mailing list
[hidden email]

This list observes the Etiquette found at
http://www.mingw.org/Mailing_Lists.
We ask that you be polite and do the same.  Disregard for the list etiquette may cause your account to be moderated.

_______________________________________________
You may change your MinGW Account Options or unsubscribe at:
https://lists.sourceforge.net/lists/listinfo/mingw-users
Also: mailto:[hidden email]?subject=unsubscribe



------------------------------------------------------------------------------
WhatsUp Gold - Download Free Network Management Software
The most intuitive, comprehensive, and cost-effective network
management toolset available today.  Delivers lowest initial
acquisition cost and overall TCO of any competing solution.
http://p.sf.net/sfu/whatsupgold-sd
_______________________________________________
MinGW-users mailing list
[hidden email]

This list observes the Etiquette found at
http://www.mingw.org/Mailing_Lists.
We ask that you be polite and do the same.  Disregard for the list etiquette may cause your account to be moderated.

_______________________________________________
You may change your MinGW Account Options or unsubscribe at:
https://lists.sourceforge.net/lists/listinfo/mingw-users
Also: mailto:[hidden email]?subject=unsubscribe
Reply | Threaded
Open this post in threaded view
|

Re: [MinGW] Control characters differences ?

Tor Lillqvist
In reply to this post by Maxime Lecourt
> I changed it (still using open), but problem is still there.

Then I don't know.

Did you say that the file was produced on Linux? If you produce the
file on Windows using a C program, you need to open it for writing in
that program in binary mode, too, of course;)

--tml

------------------------------------------------------------------------------
WhatsUp Gold - Download Free Network Management Software
The most intuitive, comprehensive, and cost-effective network
management toolset available today.  Delivers lowest initial
acquisition cost and overall TCO of any competing solution.
http://p.sf.net/sfu/whatsupgold-sd
_______________________________________________
MinGW-users mailing list
[hidden email]

This list observes the Etiquette found at
http://www.mingw.org/Mailing_Lists.
We ask that you be polite and do the same.  Disregard for the list etiquette may cause your account to be moderated.

_______________________________________________
You may change your MinGW Account Options or unsubscribe at:
https://lists.sourceforge.net/lists/listinfo/mingw-users
Also: mailto:[hidden email]?subject=unsubscribe
Reply | Threaded
Open this post in threaded view
|

Re: [MinGW] Control characters differences ?

Michael T.
In reply to this post by Tor Lillqvist
>
> No need to use fopen, just use O_BINARY in addition to the O_RDONLY
> for binary files. On Windows, the C library makes a difference between
> "binary" and "text" files.
>
> (In fopen(), add a "b" to the mode string.)
>
> --tml


Why is that? I once ported an application to Windows and reading binary files was a problem.
I spent quite a time searching for the cause and than adding the "b" helped.
Apparantly, it was working on Linux without it.
 Is it not defined by a standard? I initially thought the former Linux compiler was just more benevolent.

michael



>
> ------------------------------------------------------------------------------
> WhatsUp Gold - Download Free Network Management Software
> The most intuitive, comprehensive, and cost-effective network
> management toolset available today.  Delivers lowest initial
> acquisition cost and overall TCO of any competing solution.
> http://p.sf.net/sfu/whatsupgold-sd
> _______________________________________________
> MinGW-users mailing list
> [hidden email]
>
> This list observes the Etiquette found at
> http://www.mingw.org/Mailing_Lists.
> We ask that you be polite and do the same.  Disregard for the list etiquette may
> cause your account to be moderated.
>
> _______________________________________________
> You may change your MinGW Account Options or unsubscribe at:
> https://lists.sourceforge.net/lists/listinfo/mingw-users
> Also: mailto:[hidden email]?subject=unsubscribe
>
>

------------------------------------------------------------------------------
Achieve unprecedented app performance and reliability
What every C/C++ and Fortran developer should know.
Learn how Intel has extended the reach of its next-generation tools
to help boost performance applications - inlcuding clusters.
http://p.sf.net/sfu/intel-dev2devmay
_______________________________________________
MinGW-users mailing list
[hidden email]

This list observes the Etiquette found at
http://www.mingw.org/Mailing_Lists.
We ask that you be polite and do the same.  Disregard for the list etiquette may cause your account to be moderated.

_______________________________________________
You may change your MinGW Account Options or unsubscribe at:
https://lists.sourceforge.net/lists/listinfo/mingw-users
Also: mailto:[hidden email]?subject=unsubscribe
Reply | Threaded
Open this post in threaded view
|

Re: [MinGW] Control characters differences ?

Maxime Lecourt
>From what I've read, Linux doesn't make any difference between text or binary files.
As to why, I don't know (maybe because all files are ultimately binary data).

Maxime

2011/5/11 Michael T. <[hidden email]>
>
> No need to use fopen, just use O_BINARY in addition to the O_RDONLY
> for binary files. On Windows, the C library makes a difference between
> "binary" and "text" files.
>
> (In fopen(), add a "b" to the mode string.)
>
> --tml


Why is that? I once ported an application to Windows and reading binary files was a problem.
I spent quite a time searching for the cause and than adding the "b" helped.
Apparantly, it was working on Linux without it.
 Is it not defined by a standard? I initially thought the former Linux compiler was just more benevolent.

michael



>
> ------------------------------------------------------------------------------
> WhatsUp Gold - Download Free Network Management Software
> The most intuitive, comprehensive, and cost-effective network
> management toolset available today.  Delivers lowest initial
> acquisition cost and overall TCO of any competing solution.
> http://p.sf.net/sfu/whatsupgold-sd
> _______________________________________________
> MinGW-users mailing list
> [hidden email]
>
> This list observes the Etiquette found at
> http://www.mingw.org/Mailing_Lists.
> We ask that you be polite and do the same.  Disregard for the list etiquette may
> cause your account to be moderated.
>
> _______________________________________________
> You may change your MinGW Account Options or unsubscribe at:
> https://lists.sourceforge.net/lists/listinfo/mingw-users
> Also: mailto:[hidden email]?subject=unsubscribe
>
>

------------------------------------------------------------------------------
Achieve unprecedented app performance and reliability
What every C/C++ and Fortran developer should know.
Learn how Intel has extended the reach of its next-generation tools
to help boost performance applications - inlcuding clusters.
http://p.sf.net/sfu/intel-dev2devmay
_______________________________________________
MinGW-users mailing list
[hidden email]

This list observes the Etiquette found at
http://www.mingw.org/Mailing_Lists.
We ask that you be polite and do the same.  Disregard for the list etiquette may cause your account to be moderated.

_______________________________________________
You may change your MinGW Account Options or unsubscribe at:
https://lists.sourceforge.net/lists/listinfo/mingw-users
Also: mailto:[hidden email]?subject=unsubscribe


------------------------------------------------------------------------------
Achieve unprecedented app performance and reliability
What every C/C++ and Fortran developer should know.
Learn how Intel has extended the reach of its next-generation tools
to help boost performance applications - inlcuding clusters.
http://p.sf.net/sfu/intel-dev2devmay
_______________________________________________
MinGW-users mailing list
[hidden email]

This list observes the Etiquette found at
http://www.mingw.org/Mailing_Lists.
We ask that you be polite and do the same.  Disregard for the list etiquette may cause your account to be moderated.

_______________________________________________
You may change your MinGW Account Options or unsubscribe at:
https://lists.sourceforge.net/lists/listinfo/mingw-users
Also: mailto:[hidden email]?subject=unsubscribe
Reply | Threaded
Open this post in threaded view
|

Re: [MinGW] Control characters differences ?

JonY-3
In reply to this post by Michael T.
On 5/11/2011 16:31, Michael T. wrote:

>>
>> No need to use fopen, just use O_BINARY in addition to the O_RDONLY
>> for binary files. On Windows, the C library makes a difference between
>> "binary" and "text" files.
>>
>> (In fopen(), add a "b" to the mode string.)
>>
>> --tml
>
>
> Why is that? I once ported an application to Windows and reading binary files was a problem.
> I spent quite a time searching for the cause and than adding the "b" helped.
> Apparantly, it was working on Linux without it.
>  Is it not defined by a standard? I initially thought the former Linux compiler was just more benevolent.
Standard or not is irrelevant, MS's choice to define it that way.

MSVCRT stdio is normally text mode that translates \n to \r\n when
writing, vice versa when reading. Telling your IO to use binary mode
makes it behave close to linux versions.

Compilers don't do anything "benevolent", its up to the system C library
to define its own runtime implementation behavior.


------------------------------------------------------------------------------
Achieve unprecedented app performance and reliability
What every C/C++ and Fortran developer should know.
Learn how Intel has extended the reach of its next-generation tools
to help boost performance applications - inlcuding clusters.
http://p.sf.net/sfu/intel-dev2devmay
_______________________________________________
MinGW-users mailing list
[hidden email]

This list observes the Etiquette found at
http://www.mingw.org/Mailing_Lists.
We ask that you be polite and do the same.  Disregard for the list etiquette may cause your account to be moderated.

_______________________________________________
You may change your MinGW Account Options or unsubscribe at:
https://lists.sourceforge.net/lists/listinfo/mingw-users
Also: mailto:[hidden email]?subject=unsubscribe

0xED74C077.asc (1K) Download Attachment
signature.asc (203 bytes) Download Attachment
Reply | Threaded
Open this post in threaded view
|

Re: [MinGW] Control characters differences ?

Greg Chicares
In reply to this post by Michael T.
On 2011-05-11 08:31Z, Michael T. wrote:

>>
>> No need to use fopen, just use O_BINARY in addition to the O_RDONLY
>> for binary files. On Windows, the C library makes a difference between
>> "binary" and "text" files.
>>
>> (In fopen(), add a "b" to the mode string.)
>>
>> --tml
>
> Why is that?

Different operating systems have different line-ending conventions for
text files.

> I once ported an application to Windows and reading binary files was a problem.
> I spent quite a time searching for the cause and than adding the "b" helped.

Without "b", those binary files were opened in text mode.

> Apparantly, it was working on Linux without it.

When a file is read in text mode, the operating system's line delimiters
are translated to linefeed characters; when it's written, they're translated
back. C and Unix were designed together, so they happen to use the same
delimiter--a single linefeed--so text and binary modes work the same way.

>  Is it not defined by a standard?

This is specified by C99 7.19.2/2.

> I initially thought the former Linux compiler was just more benevolent.

Unix happens to store physical files in C's internal format. If you ignore
the difference between text and binary modes, your program will still work
correctly on Unix, but it won't be portable.

The usual msw convention delimits lines with a carriage-return--line-feed
pair, which is how lines were written on a physical teletype. The most
benevolent thing the C library can do is translate that delimiter to C's
internal representation on input, and the reverse on output. But you don't
have to follow that convention for your own files: just adopt a single
newline as your own delimiter, and open all files in binary mode, and then
your programs and files will work together on any operating system.


------------------------------------------------------------------------------
Achieve unprecedented app performance and reliability
What every C/C++ and Fortran developer should know.
Learn how Intel has extended the reach of its next-generation tools
to help boost performance applications - inlcuding clusters.
http://p.sf.net/sfu/intel-dev2devmay
_______________________________________________
MinGW-users mailing list
[hidden email]

This list observes the Etiquette found at
http://www.mingw.org/Mailing_Lists.
We ask that you be polite and do the same.  Disregard for the list etiquette may cause your account to be moderated.

_______________________________________________
You may change your MinGW Account Options or unsubscribe at:
https://lists.sourceforge.net/lists/listinfo/mingw-users
Also: mailto:[hidden email]?subject=unsubscribe
Reply | Threaded
Open this post in threaded view
|

Re: [MinGW] Control characters differences ?

Earnie Boyd
In reply to this post by Michael T.
Michael T. wrote:

>>
>> No need to use fopen, just use O_BINARY in addition to the O_RDONLY
>> for binary files. On Windows, the C library makes a difference between
>> "binary" and "text" files.
>>
>> (In fopen(), add a "b" to the mode string.)
>>
>> --tml
>
>
> Why is that? I once ported an application to Windows and reading binary files was a problem.
> I spent quite a time searching for the cause and than adding the "b" helped.
> Apparantly, it was working on Linux without it.
>  Is it not defined by a standard? I initially thought the former Linux compiler was just more benevolent.
>

https://www.securecoding.cert.org/confluence/display/seccode/FIO14-C.+Understand+the+difference+between+text+mode+and+binary+mode+with+file+streams

http://www.open-std.org/JTC1/SC22/wg14/www/docs/n1124.pdf (see section
7.19.5.3 and the description of the mode argument)

It is defined as a standard and is needed if you do block mode
operations or contain data that isn't specifically a text document.
NotePad opens the files it is given in binary mode to do block mode
operations and uses the \r to determine the EOL.  It mimics much the old
teletypes and printers that require a \r to move the carriage to the
beginning of the line.  Microsoft adapted the \r\n for text mode files
because when printing the document there wasn't anything special the
device driver had to do to know to move the carriage to the BOL since it
was in the document.  Today we've adapted printer drivers to add a BOL
when EOL is encountered.

--
Earnie
-- http://www.for-my-kids.com

------------------------------------------------------------------------------
Achieve unprecedented app performance and reliability
What every C/C++ and Fortran developer should know.
Learn how Intel has extended the reach of its next-generation tools
to help boost performance applications - inlcuding clusters.
http://p.sf.net/sfu/intel-dev2devmay
_______________________________________________
MinGW-users mailing list
[hidden email]

This list observes the Etiquette found at
http://www.mingw.org/Mailing_Lists.
We ask that you be polite and do the same.  Disregard for the list etiquette may cause your account to be moderated.

_______________________________________________
You may change your MinGW Account Options or unsubscribe at:
https://lists.sourceforge.net/lists/listinfo/mingw-users
Also: mailto:[hidden email]?subject=unsubscribe