Trouble getting file size...

This is a discussion on Trouble getting file size... within the C++ Programming forums, part of the General Programming Boards category; Whenever I attempt to use the following code, it returns an invalid length if I try to open a BMP ...

  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 09: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: Klinerr1@nc.rr.com || AIM: MisterSako || MSN: sakotheinsane@hotmail.com

    -the shroom has spoken

  4. #4
    Has a Masters in B.S.
    Join Date
    Aug 2001
    Posts
    2,267
    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, 11:39 PM
  2. Inventory records
    By jsbeckton in forum C Programming
    Replies: 23
    Last Post: 06-28-2007, 05: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, 10:54 AM
  5. simulate Grep command in Unix using C
    By laxmi in forum C Programming
    Replies: 6
    Last Post: 05-10-2002, 05:10 PM

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