LEN of a file

This is a discussion on LEN of a file within the C++ Programming forums, part of the General Programming Boards category; What is the best way to determine the size of a file? I've opened it for read access using fopen(), ...

  1. #1
    Pursuing knowledge confuted's Avatar
    Join Date
    Jun 2002
    Posts
    1,916

    LEN of a file

    What is the best way to determine the size of a file? I've opened it for read access using fopen(), and would like to know the size of the file before I proceed further. I checked MSDN and found a list of stream i/o functions, but didn't see anything for this. I suppose that I could do something like this
    Code:
    cout<<"Reading data from input file..."<<endl;
    int len=0;
    char temp[1];
    while(fread(temp, 1, 1, input))
    	len++;
    cout<<len<<endl;
    But that isn't working. I also tried
    Code:
    int len=0;
    char temp[1];
    len=fread(temp, 1, 100000000, input);
    cout<<len<<endl;
    But for some reason, that always gave me 0, even though fread(), according to MSDN, "returns the number of full items actually read, which may be less than countif an error occurs or if the end of the file is encountered before reaching count.

    So, how do you determine the size of a file in bytes? Thanks.
    Away.

  2. #2
    Toaster Zach L.'s Avatar
    Join Date
    Aug 2001
    Posts
    2,686
    I think fseek and fgetpos are probably your best bet. Just seek to the end, and then get the position.

    Edit: I don't use these functions much, but I think this'll work (might want some error checking in there, but anyways...)

    Code:
    fpos_t position;
    fseek(input, 0, SEEK_END);
    fgetpos(input, &position);
    Last edited by Zach L.; 07-02-2003 at 11:54 AM.
    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.

  3. #3
    Toaster Zach L.'s Avatar
    Join Date
    Aug 2001
    Posts
    2,686
    >> The second problem is that SEEK_END with fseek() is ``undefined'' for text files.

    Out of curiosity, why is that?
    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.

  4. #4
    Registered User
    Join Date
    Oct 2002
    Posts
    291
    This should do the trick.
    Code:
    int main()
    {
        ifstream file("C:\\AUTOEXEC.BAT");
        if ( file.is_open() )
        {
          file.seekg (0, ios::end);
          int len = file.tellg();
          cout << "The file is " << len << " bytes." << endl;
        }
        else
            cout << "Error opning file." << endl;
    
        system("PAUSE");
        return 0;
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problems passing a file pointer to functions
    By smitchell in forum C Programming
    Replies: 4
    Last Post: 09-30-2008, 03:29 PM
  2. sequential file program
    By needhelpbad in forum C Programming
    Replies: 80
    Last Post: 06-08-2008, 02:04 PM
  3. System
    By drdroid in forum C++ Programming
    Replies: 3
    Last Post: 06-28-2002, 11:12 PM
  4. Need a suggestion on a school project..
    By Screwz Luse in forum C Programming
    Replies: 5
    Last Post: 11-27-2001, 02:58 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21