Thread: file length

  1. #1
    ‡ †hë Ö†hÈr sîÐè ‡ Nor's Avatar
    Join Date
    Nov 2001
    Posts
    299

    file length

    how do you get the length of a file after you open it with fopen?

    tried a search but it gave me about 600pages
    Code:
        // open file
        fp = fopen( szFileName, "rb");
            if(!fp)
                return 1; //error        
        
        // get the length
        fp_length = _filelength((int)fp);    //doesnt work. not made for FILE*
    Try to help all less knowledgeable than yourself, within
    the limits provided by time, complexity and tolerance.
    - Nor

  2. #2
    Registered User moonwalker's Avatar
    Join Date
    Jul 2002
    Posts
    282
    ifstream infile("somefile.ext", ios :: in | ios :: binary);

    infile.seekg(0, ios :: end);

    int filesize = infile.tellg();


    a very recently learned trick

  3. #3
    ‡ †hë Ö†hÈr sîÐè ‡ Nor's Avatar
    Join Date
    Nov 2001
    Posts
    299
    thx for the help
    Code:
        // open file
        fp = fopen( szFileName, "rb");
            if(!fp)
                return 1; //error        
        
        // get the length
        fseek(fp, 0, SEEK_END); //move pointer to the end of file
        fp_length = ftell(fp);  //pointer now == filelen
        fseek(fp, 0, SEEK_SET); //move pointer to the start of the file
    from msdn
    The value returned by ftell may not reflect the physical byte offset for streams opened in text mode, because text mode causes carriage return–linefeed translation.
    if your using this you need to open the file for binary reading.
    Try to help all less knowledgeable than yourself, within
    the limits provided by time, complexity and tolerance.
    - Nor

  4. #4
    Toaster Zach L.'s Avatar
    Join Date
    Aug 2001
    Posts
    2,686
    You can always use a platform specific utility, and cover it up with a uniform interface. For UNIX/Linux, you'd use 'stat'... not sure about Windows.
    The word rap as it applies to music is the result of a peculiar phonological rule which has stripped the word of its initial voiceless velar stop.

  5. #5
    C++ Developer XSquared's Avatar
    Join Date
    Jun 2002
    Location
    Ontario, Canada
    Posts
    2,718
    Originally posted by Zach L.
    You can always use a platform specific utility, and cover it up with a uniform interface. For UNIX/Linux, you'd use 'stat'... not sure about Windows.
    OR, you could just go with the standard way and not have to worry about portability.
    Naturally I didn't feel inspired enough to read all the links for you, since I already slaved away for long hours under a blistering sun pressing the search button after typing four whole words! - Quzah

    You. Fetch me my copy of the Wall Street Journal. You two, fight to the death - Stewie

  6. #6
    I lurk
    Join Date
    Aug 2002
    Posts
    1,361
    Apparently, using SEEK_END with fseek for text files is undefined: http://cboard.cprogramming.com/showt...ighlight=fseek
    With that in mind, i'd say your best bet would be to use to use GetFileSize() / GetFileSizeEx() on Windows, or, as Zach said stat() on UNIX/Linux.

  7. #7
    ‡ †hë Ö†hÈr sîÐè ‡ Nor's Avatar
    Join Date
    Nov 2001
    Posts
    299
    both GetFileSize() / GetFileSizeEx() reqquire file handles from CreateFile().
    in using fopen() which returns a FILE*. thats y i couldnt find anything on the msdn cd
    Try to help all less knowledgeable than yourself, within
    the limits provided by time, complexity and tolerance.
    - Nor

  8. #8
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    >>Apparently, using SEEK_END with fseek for text files is undefined:
    Just to clarify, "text files" are simply those that aren't opened in binary mode. If you do use binary mode, you should be OK, no matter what contents your file has.

    That said, I still agree, a implementation specific routine would be a better way to go. You can always hide it behind a generic function anyway to make it more portable.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  9. #9
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    I think stat is implemented in VC++'s CRT too.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Formatting the contents of a text file
    By dagorsul in forum C++ Programming
    Replies: 2
    Last Post: 04-29-2008, 12:36 PM
  2. Possible circular definition with singleton objects
    By techrolla in forum C++ Programming
    Replies: 3
    Last Post: 12-26-2004, 10:46 AM
  3. System
    By drdroid in forum C++ Programming
    Replies: 3
    Last Post: 06-28-2002, 10:12 PM
  4. simulate Grep command in Unix using C
    By laxmi in forum C Programming
    Replies: 6
    Last Post: 05-10-2002, 04:10 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