Thread: fwrite for large files

  1. #1
    Registered User
    Join Date
    May 2009
    Posts
    6

    Smile fwrite for large files

    Hi,

    I am trying to write a 1 GB file in blocks of 100KB and read them back in 100KB blocks. How do I get the pointer set to the correct value to read the next block? Could anyone please help?

    Thank you!

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Are you doing all the reading consecutively? (As in, you're not trying to write to the file and then read from the same file and then write to the same file and then read from the same file etc.) If so, then there's nothing for you to do, as the file pointer is going to stay where you left it from the last time you read from the file.

  3. #3
    Making mistakes
    Join Date
    Dec 2008
    Posts
    476
    If you mean fseek and ftell, then read a manual.

    My favorite online reference:

    long ftell(FILE *stream);
    int fseek(FILE *stream, long offset, int whence);

  4. #4
    Registered User
    Join Date
    May 2009
    Posts
    6

    Smile

    Hi tabstop,

    I am trying to do it randomly. I mean..not consecutive blocks. but then i should read the whole 1 GB file.

    Thanks!

  5. #5
    Registered User
    Join Date
    May 2009
    Posts
    6

    Smile

    Tabstop:

    Also, for doing it consecutively, how to write whole 1 gb file in chunks of 100kb first and trying to read it in 100kb chunks again???
    Last edited by ccoolgoose; 06-01-2009 at 07:21 PM.

  6. #6
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    If you're reading randomly, then I guess fseek is what you're looking for. As to writing the file, just fwrite and fread and you're done. (I mean, I assume you have a 100K buffer, and you just write the buffer, replace it with the next block, write it, ad libitum.)

  7. #7
    Registered User
    Join Date
    May 2009
    Posts
    6

    Smile

    thanks!

    For writing and then reading randomly...if i use fseek..how do i get to read all the blocks tandomly summing upto 1gb. could you please post an example

  8. #8
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by ccoolgoose View Post
    For writing and then reading randomly...if i use fseek..how do i get to read all the blocks tandomly summing upto 1gb. could you please post an example
    This is kind of an interesting exercise. Can I ask *why* you want to do that and what you mean by random?
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  9. #9
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by ccoolgoose View Post
    thanks!

    For writing and then reading randomly...if i use fseek..how do i get to read all the blocks tandomly summing upto 1gb. could you please post an example
    You have to know what you want to read before you read it. People talk about random access files, which allow you to read any record out of the file at will, but they just don't read parts of the file at random. They know where they want to go, and they go there.

  10. #10
    Registered User
    Join Date
    May 2009
    Posts
    6

    Smile

    Thanks...I think i want to write the whole file but not in consecutive blocks.

  11. #11
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by ccoolgoose View Post
    Thanks...I think i want to write the whole file but not in consecutive blocks.
    You might not have a choice about that; if you're writing the file from scratch you can't leave gaps. (If you're updating an existing file then you can update the records in place.) (Edit: Or at least I don't think you can, anyway; if it is possible I suppose someone will be along shortly.)

  12. #12
    Registered User
    Join Date
    May 2009
    Posts
    6

    Smile

    Yep..you are right! lets see

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. accessing files over wireless network
    By BobMcGee123 in forum Tech Board
    Replies: 4
    Last Post: 07-29-2006, 02:25 PM
  2. Ressources files
    By mikahell in forum Windows Programming
    Replies: 4
    Last Post: 06-19-2006, 06:50 AM
  3. *.cpp and *.h files understanding
    By ElastoManiac in forum C++ Programming
    Replies: 4
    Last Post: 06-11-2006, 04:45 AM
  4. Linking header files, Source files and main program(Accel. C++)
    By Daniel Primed in forum C++ Programming
    Replies: 3
    Last Post: 01-17-2006, 11:46 AM
  5. Multiple Cpp Files
    By w4ck0z in forum C++ Programming
    Replies: 5
    Last Post: 11-14-2005, 02:41 PM