Industrialactivity
Industrialactivity

Reputation: 146

Checking hard disk memory before file writes

I have a program which simulates the orbits of planets. The information for each time step is written to a .txt file with each iteration requiring 64 bytes of memory (8 doubles). The time step is chosen by the user and the final time is chosen by the user. This allows me to calculate the amount of memory required on the disk. E.g, a step of 10 with a final time of 1000 gives 100 set of info, implying at least 6400 bytes of memory.

Is there a way of using this information to, for lack of a better word, check the drive to see if there is enough space before allowing the program write to the file, as i would like to prevent files which are too large from being written to disk. Ideally this should be standard C if possible.

Upvotes: 1

Views: 61

Answers (1)

fjardon
fjardon

Reputation: 7996

If you know in advance exactly how much space you need, you can try for all the needed files:

  1. fopen the file and fill it with blanks until you reach the exact size you need.
  2. fflush the file and check for error.
  3. rewind the file pointer to the beginning and overwrite the content with the real data.

In case of error when fflush'ing, remove all files.

When you're finished, fclose the files.

Upvotes: 1

Related Questions