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

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';
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.