I'm trying to write to a file and want to use Write System Call to make it faster. Basically I have a QVector and I want to store the results in a file. Originally I was just iterating through the array but it's too slow. So I did some research and found something called Write System Call, but I can't figure out how to set up the code.
This is what I have tried so far:
/*Header File*/
QVector<unsigned short> rawData;
/*Implementation File*/
int fd = open("output.txt", O_WRONLY)L
write(fd, &rawData, rawData.size());
close(fd);
While the above code does not crash on me it doesn't actually write anything to the output file. Any ideas what I'm doing wrong?
EDIT:
Using fwrite
I am able to write to the file but the data in the file is some strange Unicode. Basically it's not number which is what I'm trying to get. Here is what I'm doing:
FILE * pFile;
pfile = fopen("pixelValues.txt", "wb");
fwrite(&rawData, sizeof(unsigned short), sizeof(rawData), pFile);
fclose(pFile);
You're assuming that you need to write your data directly using system calls. Without seeing your code I'd argue that it probably is not the case. Most likely you were doing something very obnoxiously inefficient.
You can do better: In Qt, the simplest way would be to leverage
QDataStream
. It will do everything necessary to give you the same vector back when you read the data later. It perfoms quite adequately:Other issues:
The file is not a text file, so don't give it a
.txt
extension.Use types with explicit sizes, such as
uint16_t
orquint16
instead ofunsigned short
.