What can make Labwindows / CVI C hate the number 2573?

Using Windows

So, I am reading a list of unsigned int data values ​​from a binary file. The file contains several sets of data, listed sequentially. Here's a function to read one dataset from a char * points to its start:

function read_dataset(char* stream, t_dataset *dataset){

    //...some init, including setting dataset->size;

    for(i=0;i<dataset->size;i++){
        dataset->samples[i] = *((unsigned int *) stream);
        stream += sizeof(unsigned int);
    }
    //...
}

      

Where is read_dataset in a context like this:

//...
char buff[10000];
t_dataset* dataset = malloc( sizeof( *dataset) );
unsigned long offset = 0;

for(i=0;i<number_of_datasets; i++){

    fseek(fd_in, offset, SEEK_SET);

    if( (n = fread(buff, sizeof(char), sizeof(*dataset), fd_in)) != sizeof(*dataset) ){
        break;
    }

    read_dataset(buff, *dataset);

    // Do something with dataset here.  It screwed up before this, I checked.


    offset += profileSize;
}
//...

      

Everything goes smoothly until my loop reads number 2573. Suddenly it starts spilling out random and huge numbers.

For example what should be

...
1831
2229
2406
2637
2609
2573
2523
2247
...

      

becomes

...
1831
2229
2406
2637
2609
0xDB00000A
0xC7000009
0xB2000008
...

      

If you think these hexadecimal numbers look suspicious, you are correct. Turns off hexadecimal values ​​for values ​​that have been changed are really familiar:

2573 -> 0xA0D
2523 -> 0x9DB
2247 -> 0x8C7

      

So obviously this number 2573 is causing my stream pointer to receive bytes. This stays on until the next dataset is loaded and parsed, and God forbid it contains the number 2573. I checked several places where this happens, and each one that I checked started at 2573.

I admit that I am not that talented in the C world, which can lead to it being completely and completely opaque to me.

+2


source to share


2 answers


You didn't mention how you got the bytes in memory (pointed to a stream), nor what platform you are working on, but I wouldn't be surprised if you find it on Windows and you used a C stdio library call fopen(filename "r");

Try using fopen(filename, "rb");

. On Windows (and MS-DOS) fopen () converts MS-DOS line endings "\ r \ n" (hex 0x0D 0x0A) to a Unix-style file "\ n" unless you add "b" to file mode to indicate binary file.



+11


source


Several irrelevant points.

sizeof (* dataset) doesn't do what you think it does.



No need to use search every time you read

I don't understand how you call a function that only takes one parameter, but you give it two (or at least I don't understand why your compiler doesn't mind)

0


source







All Articles