Why does my fwrite command only run once?

158 Views Asked by At

I'm using fwrite to store the values of a QVector called rawData.

Here is my code:

unsigend short* ptr = rawdata();    

FILE * pFile;
pFile = fopen("arrayValues.txt", "a");
fwrite(ptr, sizeof(unsigned short), sizeof(rawData), pFile);
fclose(pFile);

The program above "works" meaning I was able to write to a file and when I open the file with a hex editor I can see stuff in there. However, it only write the values for one frame and then stops even though the program is calling that method over and over again.

Even stranger, if I close the program and run it again, it will open the file (which already has values from the last run inside it) and append a new set of values to the end. So I know that I'm opening the file in append mode.

1

There are 1 best solutions below

2
On BEST ANSWER

Since you mentioned rawData is a QVector, replace:

sizeof(rawData)

with

rawData.size()

If this does not work, I don't think the issue is with the code you showed us. Please provide more information/code.

Explanation: The sizeof operator returns the size in bytes of the type you give as its argument. In your case, it is returning the number of bytes used by type QVector as you are not giving it any pointer as an argument but an object of type QVector. The member funciton size() is a good way to get the number of elements for the class QVector.