Thread: is this the correct/best way to set file size?

  1. #1
    Registered User
    Join Date
    May 2006
    Posts
    1,579

    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. #2
    Registered User Micko's Avatar
    Join Date
    Nov 2003
    Posts
    715
    Your code is cetrainly portable and will work on both linux and windows.
    Gotta love the "please fix this for me, but I'm not going to tell you which functions we're allowed to use" posts.
    It's like teaching people to walk by first breaking their legs - muppet teachers! - Salem

  3. #3
    Just kidding.... fnoyan's Avatar
    Join Date
    Jun 2003
    Location
    Still in the egg
    Posts
    275
    This is the way i use to get the size of a file! So, it is ok for me...

  4. #4
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    I've never understood the purpose of creating a file bigger than it needs to be. Care to explain?

    I mean, if you're not storing anything in all 100 bytes then why make the file that big? And if you are storing data there then why not just write the data and let the file end up however big it needs to end up?
    If you understand what you're doing, you're not learning anything.

  5. #5
    Registered User
    Join Date
    Aug 2001
    Posts
    244
    sometimes i wish my filesharing client would just make the files as big as they will be when they are completed - instead of letting me slowly run out of diskspace.

    also, you might reserve a certain space for the header which cannot be filled until other stuff has been written. (e.g. if you want to cache some positions of some objects but you don't know where these objects will reside in the file).

    i like streaming solutions though - where you can just read or write in one go.
    signature under construction

  6. #6
    Registered User
    Join Date
    May 2006
    Posts
    1,579
    Cool, Micko! Thank you!


    Quote Originally Posted by Micko
    Your code is cetrainly portable and will work on both linux and windows.

    regards,
    George

  7. #7
    Registered User
    Join Date
    May 2006
    Posts
    1,579
    Thank you fnoyan!


    Quote Originally Posted by fnoyan
    This is the way i use to get the size of a file! So, it is ok for me...

    regards,
    George

  8. #8
    Registered User
    Join Date
    May 2006
    Posts
    1,579
    Hi itsme86,


    Quote Originally Posted by itsme86
    I've never understood the purpose of creating a file bigger than it needs to be. Care to explain?

    I mean, if you're not storing anything in all 100 bytes then why make the file that big? And if you are storing data there then why not just write the data and let the file end up however big it needs to end up?
    In my application logics, the file size is definitely 100 byte. Has this answered your question?


    regards,
    George

  9. #9
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    Quote Originally Posted by George2
    Hi itsme86,




    In my application logics, the file size is definitely 100 byte. Has this answered your question?


    regards,
    George
    Not really. If you're storing 100 bytes of data in the file then why not just let it grow naturally as you write data instead of growing it ahead of time?

    I can see the point where you might need to store some header data and you're not sure what it is yet, but that's just normal seek()ing use. I'm talking about if your file size will only ever be 100 bytes and you need it all I don't see the point in making it 100 bytes before it needs to be. It seems like wasted effort. If you're just seek()ing to put data in a particular place in the file then it of course makes sense. But just seek()ing to make the file a particular size is what gets me :P
    Last edited by itsme86; 07-18-2006 at 01:35 AM.
    If you understand what you're doing, you're not learning anything.

  10. #10
    Registered User
    Join Date
    May 2006
    Posts
    1,579
    Thank you Raven!


    Quote Originally Posted by Raven Arkadon
    also, you might reserve a certain space for the header which cannot be filled until other stuff has been written. (e.g. if you want to cache some positions of some objects but you don't know where these objects will reside in the file).
    Interested. Could you show me more details or show me an example please?

    Quote Originally Posted by Raven Arkadon
    i like streaming solutions though - where you can just read or write in one go.
    Do you mean in streaming based solution, we can only move file pointer in one direction (either forward or backward)?


    regards,
    George

  11. #11
    Registered User
    Join Date
    May 2006
    Posts
    1,579
    Thank you itsme86,


    Quote Originally Posted by itsme86
    Not really. If you're storing 100 bytes of data in the file then why not just let it grow naturally as you write data instead of growing it ahead of time?

    I can see the point where you might need to store some header data and you're not sure what it is yet, but that's just normal seek()ing use. I'm talking about if your file size will only ever be 100 bytes and you need it all I don't see the point in making it 100 bytes before it needs to be. It seems like wasted effort.
    I am using mmap to map file into memory, and mmap needs to have a fixed size of an output file before memory mapping. Has this answered your question?


    regards,
    George

  12. #12
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    I see. Thanks.
    If you understand what you're doing, you're not learning anything.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. A development process
    By Noir in forum C Programming
    Replies: 37
    Last Post: 07-10-2011, 10:39 PM
  2. Replies: 3
    Last Post: 03-04-2005, 02:46 PM
  3. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 09:54 AM
  4. archive format
    By Nor in forum A Brief History of Cprogramming.com
    Replies: 0
    Last Post: 08-05-2003, 07:01 PM
  5. Need a suggestion on a school project..
    By Screwz Luse in forum C Programming
    Replies: 5
    Last Post: 11-27-2001, 02:58 AM