0

hi,

im trying to read and write an array of integer to/from a file. i use this to
write to a file:

fwrite(texel, sizeof(int), size, fp);

and this to write it:

(*out) = new int[size];
fread((*out), sizeof(int), size, fp);

but the data read from the same file is different. im not sure what happened but
from the real data, its a bunch of integer with differnece = 2 (even numbers)
but when i read it, its a sequence of number(1, 2, 3, 4). i think its halved or
something.

any idea why?

thanks in advance

2
Contributors
1
Reply
3
Views
6 Years
Discussion Span
Last Post by Ancient Dragon
0

maybe you are not opening the file in binary mode. This works ok for me

int main()
{
    int *parray = 0;
    int ay[] = {1,2,3,4,5,6,7,8,9,10};
    int size = sizeof(ay) / sizeof(ay[0]);
    FILE* fp = fopen("filename.dat","wb");
    fwrite(ay,sizeof(int), size,fp);
    fclose(fp);
    
    parray = new int[size];
    fp = fopen("filename.dat","rb");
    fread(parray, sizeof(int), size,fp);
    fclose(fp);
    for(int i = 0; i < size; i++)
        cout << parray[i] << '\n';
}
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.