bool CReadWrite::write(unsigned long long offset, void* pvsrc, unsigned long long nbytes)
{ int m_WriteResult;
pFile = fopen("E:\\myfile.bin","wb");
if (!pFile){
puts("Can't open file");
return false;
}
fseek(pFile,SIZE_OF_FILE-1,SEEK_SET);
fwrite(pvsrc,1,1,pFile);
fseek(pFile,offset,SEEK_SET);
printf("fseek(pFile,SIZE_OF_FILE-1,SEEK_SET); returned -- ", fseek(pFile,SIZE_OF_FILE-1,SEEK_SET));
printf( "fwrite(pvsrc,1,1,pFile); returned -- %d", fwrite(pvsrc,1,1,pFile));
printf("Current file pointer position is : %d\n",ftell(pFile));
m_WriteResult = fwrite (pvsrc, 1, nbytes, pFile);
if (m_WriteResult == nbytes){
puts("Wrote to file");
printf("The number of bytes written to the file is : %d\n\n",m_WriteResult);
fclose(pFile);
return true;
}
else{
puts("Unable to write to File.");
fclose(pFile);
return false;
}
}
main.cpp:
char* pWriteBuffer;
char* pReadBuffer;
int nbytes = 85;
int nbytes2= 10;
pWriteBuffer = new char [nbytes];
pReadBuffer = new char [nbytes];
CReadWrite test;
for(Bufferndx = 0; Bufferndx<nbytes; Bufferndx++){
pWriteBuffer[Bufferndx] = Bufferndx;
}
test.write(20,pWriteBuffer,50);
for(Bufferndx = 10; Bufferndx;Bufferndx--){
pWriteBuffer[Bufferndx] = Bufferndx;
}
test.write(30,pWriteBuffer,nbytes2);
test.read(5,pReadBuffer,85);
delete[] pWriteBuffer;
delete[] pReadBuffer;
This is a part of my program that writes to a given buffer. The write function is given an offset, a source and how many bytes to write.
It first checks to see if the file was even able to open, fseeks to my desired file size -1, then writes a byte. It then fseeks in the file to the offset given. If it successfully wrote nbytes, it prints out some stuff, then closes the file. Else it prints out that it was unable to write and closes the file.
In my main, I’m just testing that my code actually wrote what I wanted to and I’ve got two for loops that have two writes after. I’m also doing a test.read after this but I am not sure why I’m not able to see the correct results I think I’m supposed to get in the debug mode when compiling.
Just FYI, The read class function is almost identical to the write but of course fread.
My question is: Why am I not getting the results when I step through using the debug mode and why am I not able to see the buffer be filled correctly. It’s “almost” like my writes/read is not really happening.
EDIT: What I’m “trying to do” is fill a buffer with 85 bytes (my biggest buffer).
- My
test.write(20,pWriteBuffer,50)will start from offset 20 and write 50 bytes, aka. offset 20 -70. - My second write
test.write(30,pWriteBuffer,nbytes2)will start at offset 30 and write 10 bytes, aka. offset 20-30. - And 3rd, I read the whole thing and I should be able to tell where all the writes occur.
Or that is the plan, but I do not see that when I debug. Also, Maybe someone can shed some light on this but I’m reviewing over it and it looks like I’m…
fseek(pFile,SIZE_OF_FILE-1,SEEK_SET);
fwrite(pvsrc,1,1,pFile);
each time I write which is wrong… I shouldn’t be seeking out and making the file bigger each time I write. Ugh
printf( “fwrite(pvsrc,1,1,pFile); returned — %d”, fwrite(pvsrc,1,1,pFile)); //prints out 1
printf(“fseek(pFile,SIZE_OF_FILE-1,SEEK_SET); returned — “, fseek(pFile,SIZE_OF_FILE-1,SEEK_SET)); //prints out 0
Here are the problems I see:
Suggestions: Never forget to check the return codes of all functions you’re calling. Print them out. Like you did in this:
if (!pFile){ /*...*/}E.g.
Don’t get confused, I’ve just shown 3 different ways of printing out debug info for 3 different calls. YOu can pick any one way (preferably 3rd one) and use everywhere.