2

I have to write a virtualdisk file handling system as part of a university project using C(I am running windows), I'm running into a problem when I am writing to the FAT table where I try to create a FAT chain containing a reference to FAT block 10(0A 00) and it is instead writing 0D 0A 00.

The disk is comprised of 1024 blocks of size 1024, the first block is used for arbitrary information, the second and third blocks are for the FAT and the fourth block is for the root directory, the remaining blocks are for farther data.

The FAT table is defined as:

    fatentry_t FAT[MAXBLOCKS];

Fatentry_t is defined as:

    typedef short fatentry_t;

And I access it using:

    typedef union block{
        datablock_t data;
        dirblock_t  dir;
        fatblock_t  fat;
    }diskblock_t;

I am copying the FAT to the disk using:

    void copyFAT(){
        diskblock_t block = getEmptyDiskBlock();
        int pos = 0;
        for(pos = 0; pos < 512; pos++){
            block.fat[pos] = FAT[pos];
        }
        writeblock(&block, 1);
        for(pos = 512; pos < 1024; pos++){
            block.fat[pos-512] = FAT[pos];
        }
        writeblock(&block, 2);
        writedisk("src\\virtualdiskD3_D1");
    }

When making a FAT chain, if there is any reference to 0A I will get a 0D, for example:

00 00 02 00 00 00 00 00  05 00 06 00 07 00 08 00
09 00 0D 0A 00 0B 00 0C  00 0D 00 0E 00 0F 00 10
00 ...

When I am expecting:

00 00 02 00 00 00 00 00  05 00 06 00 07 00 08 00
09 00 0A 00 0B 00 0C 00  0D 00 0E 00 0F 00 10 00
11 ...

Has anyone got any idea what is causing this?

My current thought is that it is windows, however I have no idea how to fix this in eclipse.

Thanks in advance.

EDIT:

    void writedisk(const char *filename){
        printf("writedisk> virtualdisk[0] = %s\n", virtualDisk[0].data);
        FILE *dest = fopen( filename, "w" );
        if(fwrite(virtualDisk, sizeof(virtualDisk), 1, dest) < 0){
            fprintf(stderr, "write virtual disk to disk failed\n");
        }
        fclose(dest);

    }

    void writeblock(diskblock_t * block, int block_address){
        memmove(virtualDisk[block_address].data, block->data, BLOCKSIZE);
    }

EDIT2:

Thanks to all who commented and answered this, I did not write the writedisk function and as such did not notice this, the guide who will be marking will be using linux which I don't believe he well need it to be in binary mode as linux only uses 0A rather than 0D0A, but for me this will make developing on windows much easier.

Nick is tired
  • 6,860
  • 20
  • 39
  • 51
  • 3
    You have a text-binary problem. Are you using `fopen` at some point ? Please show the code for `writeblock` and `writedisk`. – Jabberwocky Oct 29 '15 at 13:36
  • 2
    It looks like you have opened the file in text mode on Windows, so it inserts a 0x0D before every 0x0A character. You need to open it in binary mode. – Ian Abbott Oct 29 '15 at 13:40

1 Answers1

6

Use:

fopen( filename, "wb" );

instead of:

fopen( filename, "w" );

This will open your file in binary mode instead of text mode.

Look at the documentation of fopen.

Jabberwocky
  • 48,281
  • 17
  • 65
  • 115
  • 1
    This is a classic one on Windows. On Linux/Unix etc. there are no binary or text modes because line ending in text files are "\n" (0x0a) whereas in Windows line ending are "\r\n" (0xd 0xa). – Jabberwocky Oct 29 '15 at 13:44
  • Is it possible to use fopen( filename, "wb" ); on linux as well as windows? Or will I need to make a note to him of this. – Nick is tired Oct 29 '15 at 13:45
  • 1
    "wb" is possibly ignored on linux, have a look ath [this SO question](http://stackoverflow.com/questions/2174889/whats-the-differences-between-r-and-rb-in-fopen). – Jabberwocky Oct 29 '15 at 13:51
  • 1
    Note some OS's have use `'\r'` as a line ending and `'\r'` --> `'\n'` occurred on reading. Some early system had a `^Z` on the end-of-file in text mode, stripping it on the read. IOWs, opening in binary/text may have various perpendicularities aside from `"\r\n"`, though certainly that is most common in 2015. – chux - Reinstate Monica Oct 29 '15 at 14:02