Thread: Trouble getting file size...

  1. #1
    C++ Developer XSquared's Avatar
    Join Date
    Jun 2002
    Location
    Ontario, Canada
    Posts
    2,718

    Trouble getting file size...

    Whenever I attempt to use the following code, it returns an invalid length if I try to open a BMP or other binary file, but it works fine with text files. Any ideas why?
    PHP Code:
    #include <stdio.h>
    #include <conio.h>
    #include <fstream.h>

    long GetFileLen(const char *);

    long GetFileLen(const char *FilePath)
    {
         
    long RetVal 0;
        
    unsigned char TempChar;
        
    ifstream TheFile;
        
    TheFile.open(FilePath);
        while(!
    TheFile.eof())
        {
            
    TheFile.get(TempChar);        
            
    RetVal++;
        }
        
    TheFile.close();
        return 
    RetVal;
    }

    int main()
    {
        
    long FileLen;        
        
    FileLen GetFileLen("C:/asdf.bmp");
        
    printf("%ld",FileLen);
        
    getch();
        return 
    0;

    Last edited by XSquared; 07-06-2002 at 08:36 PM.

  2. #2
    C++ Developer XSquared's Avatar
    Join Date
    Jun 2002
    Location
    Ontario, Canada
    Posts
    2,718
    Never mind, figured it out.

    PHP Code:
    #include <stdio.h>
    #include <conio.h>
    #include <fstream.h>

    long GetFileLen(const char *);

    long GetFileLen(const char *FilePath)
    {
         
    long RetVal 0;
        
    unsigned char TempChar;
        
    ifstream TheFile(FilePath,ios::binary); //Forgot to open in binary
        
    while(!TheFile.eof())
        {
            
    TheFile.get(TempChar);        
            
    RetVal++;
        }
        
    TheFile.close();
        return 
    RetVal;
    }

    int main()
    {
        
    long FileLen;        
        
    FileLen GetFileLen("C:/asdf.bmp");
        
    printf("%ld",FileLen);
        
    getch();
        return 
    0;


  3. #3
    Sir Mister Insane Sako Klinerr1's Avatar
    Join Date
    May 2002
    Posts
    608
    in your signiture you shoudl sya what compiler you use os when you ask problems invlolving codes people can look at your sig and know if they have to same compiler and help you or how to help you
    Email: [email protected] || AIM: MisterSako || MSN: [email protected]

    -the shroom has spoken

  4. #4
    Has a Masters in B.S.
    Join Date
    Aug 2001
    Posts
    2,263
    because a BMP or binary can actually contain many EOF values, so EOF is not unfortunatly a valid way to find the files end.

    and since i don't think there's a standard way of doing this,

    there are three options as i see it others will no doubt add to them,

    1. see if your compiler has the sys/stat.h include file, and use the stat function to get the filesize.

    2. find the file format, and use its internal value

    3. snake it with something like this...

    im not exactly sure how you'd do this with C++ File streams but i'll give it a go.

    Code:
    long GetFileLen(const char *FilePath)
    {
        ifstream TheFile;
        TheFile.open(FilePath);
    
        TheFile.seekp(0,ios::end); // seek to the end of the file.
        return TheFile.tellp(); // return the size of the file.
    }
    ADVISORY: This users posts are rated CP-MA, for Mature Audiences only.

  5. #5
    C++ Developer XSquared's Avatar
    Join Date
    Jun 2002
    Location
    Ontario, Canada
    Posts
    2,718
    Actually, it works fine now that i changed the open line to 'fstream TheFile("c:/asdf.bmp",ios::binary);'. It returns the correct size and everything.

  6. #6
    Its not rocket science vasanth's Avatar
    Join Date
    Jan 2002
    Posts
    1,683
    I too have problems with binary files ::binary does not seem to work..

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. Inventory records
    By jsbeckton in forum C Programming
    Replies: 23
    Last Post: 06-28-2007, 04:14 AM
  3. Invalid conversion from 'void*' to 'BYTE' help
    By bikr692002 in forum C++ Programming
    Replies: 9
    Last Post: 02-22-2006, 11:27 AM
  4. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 09:54 AM
  5. simulate Grep command in Unix using C
    By laxmi in forum C Programming
    Replies: 6
    Last Post: 05-10-2002, 04:10 PM