ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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).
There's no portable (as far as the ISO C specs are concerned) way to determine file size.
You should have a look at stat() (non ISO C, but part of POSIX/SUS and supported as an extension in VC++).
And besides, your code will produce different results on windows and unix when text files are used since windows differentiates between the 2, so it's not too portable either.
Don't do that. You create a file with holes in it. It screws up backups.
If there is a sysadmin he/she will throw rocks at you
There is no way to effectively pre-allocate a file in either Windows or Linux.
Windows (MS) does NOT follow any ANSI or ISO standards for it's compilers, so if your code ports it's pure luck. There are other free compilers that do a better job...
You have also included several extra header files for the code you show us. It does not need those header files.
When "hoely" files are backed up, they don't always restore back to disk correctly, depneidng on the file system. Therefore they essentially report incorrect file sizes to the file system. If you want to look at it that way. This drives sysadmins insane when they think they are restoring files of one size and get different results.
Last edited by jim mcnamara; 07-17-2006 at 09:12 AM.
There's no portable (as far as the ISO C specs are concerned) way to determine file size.
You should have a look at stat() (non ISO C, but part of POSIX/SUS and supported as an extension in VC++).
You mean using stat to set file size? As far as I know, stat can only get the information of the file, other than set its size.
Quote:
Originally Posted by ugenn
And besides, your code will produce different results on windows and unix when text files are used since windows differentiates between the 2, so it's not too portable either.
Do you mean on Windows, end of line is \r\n and on Linux end of line is \n?
Don't do that. You create a file with holes in it. It screws up backups.
If there is a sysadmin he/she will throw rocks at you
There is no way to effectively pre-allocate a file in either Windows or Linux.
Windows (MS) does NOT follow any ANSI or ISO standards for it's compilers, so if your code ports it's pure luck. There are other free compilers that do a better job...
You have also included several extra header files for the code you show us. It does not need those header files.
When "hoely" files are backed up, they don't always restore back to disk correctly, depneidng on the file system. Therefore they essentially report incorrect file sizes to the file system. If you want to look at it that way. This drives sysadmins insane when they think they are restoring files of one size and get different results.
Your reply makes senses. But from the link you provided, I can not find more information about the file hole problem and the confusing file size issue you mentioned above. Could you provide more information please?
I mean, declare a straight character literal of hundred bytes, fill it with non-white space (presumably handles 0d, 0a and ctrl+z in doze) and write it with fopen, fclose etc - the works.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.