LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 07-17-2006, 05:00 AM   #1
George2
Member
 
Registered: Oct 2003
Posts: 354

Rep: Reputation: 30
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
 
Old 07-17-2006, 06:00 AM   #2
ugenn
Member
 
Registered: Apr 2002
Posts: 549

Rep: Reputation: 30
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.
 
Old 07-17-2006, 09:06 AM   #3
jim mcnamara
Member
 
Registered: May 2002
Posts: 964

Rep: Reputation: 36
George -

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.

Try this:
Code:
#include <stdio.h>
#include <stdlib.h>
int main()
{
     FILE *out=fopen("filename","w");
     char ch=0xff;
      
     if(out==NULL){
     {
         perror("Error on file open");
         exit(EXIT_FAILURE);
     }
     if( !fwrite(&ch,sizeof(char),100,out) || fclose(out)==EOF)
     {
         perror("File I/O failure);
         exit(EXIT_FAILURE);
     }
     return 0;
}
If this is meant to be production code you have to check return codes to assure success, especially with disk operations.

This also uses code that will compile/run on Windows.

File holes: http://www.scit.wlv.ac.uk/~jphb/cp3025/fcons.html

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.
 
1 members found this post helpful.
Old 07-18-2006, 02:41 AM   #4
George2
Member
 
Registered: Oct 2003
Posts: 354

Original Poster
Rep: Reputation: 30
Thank you ugenn,


Quote:
Originally Posted by ugenn
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?


regards,
George
 
Old 07-18-2006, 02:46 AM   #5
George2
Member
 
Registered: Oct 2003
Posts: 354

Original Poster
Rep: Reputation: 30
Thank you jim,


Quote:
Originally Posted by jim mcnamara
George -

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.

Try this:
Code:
#include <stdio.h>
#include <stdlib.h>
int main()
{
     FILE *out=fopen("filename","w");
     char ch=0xff;
      
     if(out==NULL){
     {
         perror("Error on file open");
         exit(EXIT_FAILURE);
     }
     if( !fwrite(&ch,sizeof(char),100,out) || fclose(out)==EOF)
     {
         perror("File I/O failure);
         exit(EXIT_FAILURE);
     }
     return 0;
}
If this is meant to be production code you have to check return codes to assure success, especially with disk operations.

This also uses code that will compile/run on Windows.

File holes: http://www.scit.wlv.ac.uk/~jphb/cp3025/fcons.html

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?


regards,
George
 
Old 07-18-2006, 06:44 AM   #6
ugenn
Member
 
Registered: Apr 2002
Posts: 549

Rep: Reputation: 30
Quote:
Originally Posted by George2
Thank you ugenn,
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.
Sorry, my bad. I misread you original post.

Quote:
Do you mean on Windows, end of line is \r\n and on Linux end of line is \n?
On windows, when opened in "text" mode, '\n' is expanded into ascii 0xD and 0xA.
 
Old 07-18-2006, 06:46 AM   #7
ugenn
Member
 
Registered: Apr 2002
Posts: 549

Rep: Reputation: 30
George,
to find out more about "holey" files. Go research on sparce files.
 
Old 07-18-2006, 07:32 AM   #8
AnanthaP
Member
 
Registered: Jul 2004
Location: Chennai, India
Posts: 952

Rep: Reputation: 217Reputation: 217Reputation: 217
Maximum portability?

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.

End
 
Old 07-18-2006, 11:19 PM   #9
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,359

Rep: Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751
I think you meant 'sparse' files ;-)
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
HD not showing up as correct size, please help!! chriserickson Linux - Hardware 6 09-23-2005 01:04 PM
df not reporting correct size. mdarby Slackware 2 11-10-2004 07:36 AM
size of shared folder not correct VMWare buldir Linux - Software 0 09-01-2004 04:10 AM
How to set correct file/directory permissions pat.delaney Linux - Networking 5 12-02-2003 09:39 AM
screen size not correct? blither Slackware 9 12-16-2002 09:53 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 10:26 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration