| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Display Modes |
|
#1
|
|||
|
|||
|
Is this the correct/best way to set file size?
Hello everyone,
I have verified that the following approach works to set the size of a file (newly created file) to be 100 bytes, but I am not sure whether it is the correct/best way to have a maximum portability (I need to write code on both Windows and Linux). Could anyone give me any comments? Code:
#include <fcntl.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <io.h>
#include <stdio.h>
int main()
{
FILE* file = fopen ("foo123", "w+");
fseek (file, 99, SEEK_SET);
fprintf (file, "x");
fclose (file);
return 0;
}
thanks in advance, George |
|
#2
|
||||
|
||||
|
Why would you want to make your files a fixed size? Make 'em as small as possible I always say..
If it's really critical, this seems as a good a method as any, I doubt there are any standard functions for this...
__________________
This is my code. Is it not nifty? "The biggest problem encountered while trying to design a system that was completely foolproof, was, that people tended to underestimate the ingenuity of complete fools." ---Douglas Adams Join the Itsacon fanclub! Zero Tolerance: Spammers banned so far: 280
![]() |
|
#3
|
|||
|
|||
|
Thank you Itsacon!
Quote:
You mean a standard for what? regards, George |
|
#4
|
||||
|
||||
|
I mean there is not a preset library-function like:
Code:
FILE* create_fixed_size_file(char* filename, double filesize); So, your trick is as good as any. |
|
#5
|
|||
|
|||
|
Thank you Itsacon!
Quote:
regards, George |
|
#6
|
||||
|
||||
|
you're welcome.
|
![]() |
| Viewing: Dev Articles Community Forums > Programming > C/C++ Help > Is this the correct/best way to set file size? |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|
|