Basically, I have a program that gives a compressed file of 4 megabytes in size, it should decode this file into uncompressed ~ 100 megagrams, and then compress it back to ~ 4 mega file. I need to save this intermediate 100 megabyte file somewhere on disk (I don't want to store it in memory).
The program is written in C and will be executed in MS Windows 7. At the time of decompression, a guaranteed folder (with write access) is not provided (the folder with the source file can be read-only and the folder with the target file may not be specified yet).
This turned out to be a daunting task:
1) I read about the C function, which creates a temporary file that will disappear when you close or close the program. However, from what I understand, he is trying to make a file on drive C in the root directory, so this obviously will not succeed if the user does not have rights to this (which a regular user does not)
2) I had an idea to use the environment variable / system variable TEMP and create a file there, BUT looking at a random Win7 PC that has not been changed, I see that this variable points to c: / windows / temp and this folder has certain rights for "users", that is, they have the right to read, execute, create and write files, but not delete them, check their attributes, etc. This means that I assume that if the program starts with user privileges, it will be able to create the file, but will not be able to delete it, so the only way to βdeleteβ it is to open the file for writing and then close it, making it a file of length 0. This is also undesirable, and I do not know how to request system variables from C
3) So basically, the only idea that I have now is to create a function to open a file that:
- tries to create a temporary file in the output directory, if possible
- if failed, tries to create a temporary file in the input directory
- if failed, try to create a temporary file in the TEMP directory from the system variable
- if failed, tries to create a temporary file in the TMP directory from the system variable
and a delete function, which:
- trying to delete () a file (by its name, which is stored somewhere)
- if it fails, it tries to open the file for writing and close it, so it becomes a file of size 0 bytes
Are there any better ideas?
Any help is appreciated, thanks!
PS: The program should not use any external libraries, such as MFC or anything else, only the built-in standard C functions