I have a significant amount of data being generated in a C program. I want to write the data out into 1gb files.
How would I got about doing this?
At the moment, I go through a while loop (with a condition that will go on until 5 gbs data is created). It creates a struct and writes it to a file:
fwrite(storedVal, sizeof(keyEncode),1,fp);
//storedVal being the struct (which contains my data)
I would like each file to be called codes1, codes2 and increment on until the while loop is finished. I am sure it would include an if statement based on the size of the current file it is writing to. Then when it reaches 1gb, it begins on a new file.
edit //
actually it should be some sort of while statement , that keeps writting to the size, untill the file reaches 1gb and starts on the new one
My file at the moment is opened prior to my while loop:
fp = fopen("keys.dat", "wb");
while(condition is true) {
//create a new struct with data and
fwrite(storedVal, sizeof(keyEncode),1,fp);
}
Something along the lines of the following – note that there are some parts left out to fill in.
Update
You could incorporate logic like this into a helper function to write, as per Aaron’s response. Note that you would have to make some of the data members global and it would not be thread safe.