is this the correct/best way to set file size? [modified]
-
It's probably a lot more portable than
SetEndOfFile()
. ;)
"Money talks. When my money starts to talk, I get a bill to shut it up." - Frank
"Judge not by the eye but by the heart." - Native American Proverb
Thank you DavidCrow! What do you mean SetEndOfFile()? Is there a method called SetEndOfFile() in C? regards, George
-
Thank you DavidCrow! What do you mean SetEndOfFile()? Is there a method called SetEndOfFile() in C? regards, George
George_George wrote:
Is there a method called SetEndOfFile() in C?
There's a function called SetEndOfFile().
"Money talks. When my money starts to talk, I get a bill to shut it up." - Frank
"Judge not by the eye but by the heart." - Native American Proverb
-
George_George wrote:
Is there a method called SetEndOfFile() in C?
There's a function called SetEndOfFile().
"Money talks. When my money starts to talk, I get a bill to shut it up." - Frank
"Judge not by the eye but by the heart." - Native American Proverb
Thank you DavidCrow! Why my method is better than SetEndOfFile()? regards, George
-
Thank you DavidCrow! Why my method is better than SetEndOfFile()? regards, George
George_George wrote:
Why my method is better than SetEndOfFile()?
You wanted portability, didn't you?
"Money talks. When my money starts to talk, I get a bill to shut it up." - Frank
"Judge not by the eye but by the heart." - Native American Proverb
-
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?
#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 -- modified at 6:11 Monday 17th July, 2006There's two ways to get portability; the first is to use a common API and test it enough to make sure it works the same way on both systems; the second is to do such:
int main(int argc, char** argv)
{
#if defined(WINDOWS)
//put windows code here#elif defined(LINUX)
//code that does the same thing in linux
#endifreturn 0;
}The former tends to make for much more readable code... at least IMO. earl
-
George_George wrote:
Why my method is better than SetEndOfFile()?
You wanted portability, didn't you?
"Money talks. When my money starts to talk, I get a bill to shut it up." - Frank
"Judge not by the eye but by the heart." - Native American Proverb
Thank you DavidCrow! Yes, I mean why SetEndOfFile() has worse portability? regards, George
-
There's two ways to get portability; the first is to use a common API and test it enough to make sure it works the same way on both systems; the second is to do such:
int main(int argc, char** argv)
{
#if defined(WINDOWS)
//put windows code here#elif defined(LINUX)
//code that does the same thing in linux
#endifreturn 0;
}The former tends to make for much more readable code... at least IMO. earl
Thank you earl! Your sample is only the general principle to develop portable code. In my case, to set the length of a file, is my method showed above has good portability? Do you have any better ideas or any comments? regards, George
-
Thank you DavidCrow! Yes, I mean why SetEndOfFile() has worse portability? regards, George
George_George wrote:
...why SetEndOfFile() has worse portability?
It's only for a Windows platform.
"Money talks. When my money starts to talk, I get a bill to shut it up." - Frank
"Judge not by the eye but by the heart." - Native American Proverb
-
Thank you earl! Your sample is only the general principle to develop portable code. In my case, to set the length of a file, is my method showed above has good portability? Do you have any better ideas or any comments? regards, George
Why do you need the file to be 100b? I imagine your example should work, unless there are examples of sparse filesystems in use? I doubt it, but I don't keep up with the various file systems in use under linux. Alternatively, you could just fwrite 100b of zeroes...
unsigned char buff[100];
memset(buff, 0x0, 100 * sizeof(unsigned char));fwrite(buff, sizeof(unsigned char), 100, fp);
earl
-
George_George wrote:
...why SetEndOfFile() has worse portability?
It's only for a Windows platform.
"Money talks. When my money starts to talk, I get a bill to shut it up." - Frank
"Judge not by the eye but by the heart." - Native American Proverb
Thank you DavidCrow! regards, George
-
Why do you need the file to be 100b? I imagine your example should work, unless there are examples of sparse filesystems in use? I doubt it, but I don't keep up with the various file systems in use under linux. Alternatively, you could just fwrite 100b of zeroes...
unsigned char buff[100];
memset(buff, 0x0, 100 * sizeof(unsigned char));fwrite(buff, sizeof(unsigned char), 100, fp);
earl
Thank you earl! Your method is using more memory compared with my method -- 100 more bytes for the local char array. Agree? regards, George -- modified at 4:28 Wednesday 19th July, 2006
-
Thank you earl! Your method is using more memory compared with my method -- 100 more bytes for the local char array. Agree? regards, George -- modified at 4:28 Wednesday 19th July, 2006
-
Sure, but who cares? As written it'll be allocated on the stack and disappear after the function is finished. You also could call fwrite 100 times on a single byte, if you're working in a space where 100b matters. earl
Thank you earl! regards, George