Hello there!

I have an exercise that askes me to create a binary file .dat with a structure in it. I have to run it like this: a.out something.dat. Also I have to create a command prompt for the user (input). I am very confused since I don't have linux! How can I do that?

Do it the same way you'd do it in Windows (open the file in binary mode and use the read/write functions). This can be done with standard code, though the contents of the file will not be portable due to implementation-defined padding between structure members. So your file can't be read with a program compiled on a different compiler than the writing program was compiled on.

Do it the same way you'd do it in Windows (open the file in binary mode and use the read/write functions). This can be done with standard code, though the contents of the file will not be portable due to implementation-defined padding between structure members. So your file can't be read with a program compiled on a different compiler than the writing program was compiled on.

You mean that after creating the file in Windows I can't use it on Linux? What I am going to do is to just write the code in CodeBlocks in Windows and then copy the code to a Linux compiler.

You mean that after creating the file in Windows I can't use it on Linux?

Not portably. You can compile the code on Linux and then create the file on Linux, but if you create the file on Windows and then try to read it on Linux, there's a chance that it won't work.

Here's why. Consider a structure definition:

struct foo
{
    char a;
    int b;
    short c;
    double d;
};

Each member would normally be aligned to a specific boundary, such as multiples of 4. To maintain this alignment, hidden padding between members is added by the compiler. So the structure might really look like this to place objects on multiples of the byte size (char=1, short=2, int=4, double=8, for this example):

struct foo
{
    char a;           /* 1 byte at 1 */
    unsigned char[3]; /* Align for an int */
    int b;            /* 4 bytes at 4 */
    short c;          /* 2 bytes at 8 */
    unsigned char[6]; /* Align for a double */
    double d;         /* 8 bytes at 16 */
};

The alignment requirements are platform dependent, and the padding choices are implementation-dependent. So the structure you write to file on Windows could very likely not be the same structure you expect to read from the same file on Linux.

Most compilers will offer a way to force that padding away and pack the members as tightly as possible, but that's typically a code change (in the form of a pragma) which would make your code non-portable. The real lesson here isn't this specific portability issue, it's the fact that such issues exist and you can't take anything for granted. ;)

What I am going to do is to just write the code in CodeBlocks in Windows and then copy the code to a Linux compiler.

That's fine. As long as you write standard code, you shouldn't have any trouble moving the code from Windows to Linux. Unless there's something you haven't mentioned, the problem can be solved with 100% standard code.

Edited 6 Years Ago by Narue: n/a

Since I am a beginner I don't know what's happening behind the machines and the compilers. Thanks once more dude!

This article has been dead for over six months. Start a new discussion instead.