c - how do i suspend file writing when the file gets to a certain size and and then create a new file -


i have significant amount of data being generated in c program. want write data out 1gb files.

how got doing this?

at moment, go through while loop (with condition go on until 5 gbs data created). creates struct , writes file:

fwrite(storedval, sizeof(keyencode),1,fp); //storedval being struct (which contains data) 

i each file called codes1, codes2 , increment on until while loop finished. sure include if statement based on size of current file writing to. when reaches 1gb, begins on new file.

edit //

actually should sort of while statement , keeps writting size, untill file reaches 1gb , starts on new one

my file @ moment opened prior while loop:

fp = fopen("keys.dat", "wb"); while(condition true) {     //create new struct data ,     fwrite(storedval, sizeof(keyencode),1,fp); } 

something along lines of following - note there parts left out fill in.

int write_count = 0; int file_idx = 1; file *fp; char filename[20];  sprintf( filename, "codes%d", file_idx ); file_idx++; fp = fopen(filename, "wb");  while( foo ) {      //update data      if( write_count + sizeof(keyencode) > one_gigabyte ) {         fclose(fp);         write_count = 0;         sprintf( filename, "codes%d", file_idx );         file_idx++;         fp = fopen(filename, "wb");     }      fwrite(storedval, sizeof(keyencode),1,fp);     write_count += sizeof(keyencode); }  fclose(fp); 

update

you incorporate logic helper function write, per aaron's response. note have make of data members global , not thread safe.


Comments

Popular posts from this blog

android - Spacing between the stars of a rating bar? -

html - Instapaper-like algorithm -

c# - How to execute a particular part of code asynchronously in a class -