Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. File size in standard way

File size in standard way

Scheduled Pinned Locked Moved C / C++ / MFC
c++ioshelpquestion
11 Posts 3 Posters 12 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • C CodingLover

    Hi all, One of my application I used MFC(actually CFile) to find the file size in easy way. Now I thought to do it using standard C++. Here is my try.

    size\_t file\_size;
    ifstream in\_file;
    
        in\_file.open("C:\\\\temo\_file.txt", ios\_base::in);
    
    if(in\_file.is\_open())
    {
    	file\_size = in\_file.seekg(-1, ios\_base::end).tellg();
    	cout << file\_size;
    }        
    

    My question is, this code gives misses one byte. Can you guys give me any reason for it. Thanks

    I appreciate your help all the time... CodingLover :)

    D Offline
    D Offline
    Doc Lobster
    wrote on last edited by
    #2

    I guess with seekg you are looking for the position -1 from the end. Therefore you are missing the byte. Additionally look here: http://www.codeproject.com/KB/files/filesize.aspx[^] http://www.cplusplus.com/reference/iostream/istream/seekg.html[^]

    C 1 Reply Last reply
    0
    • C CodingLover

      Hi all, One of my application I used MFC(actually CFile) to find the file size in easy way. Now I thought to do it using standard C++. Here is my try.

      size\_t file\_size;
      ifstream in\_file;
      
          in\_file.open("C:\\\\temo\_file.txt", ios\_base::in);
      
      if(in\_file.is\_open())
      {
      	file\_size = in\_file.seekg(-1, ios\_base::end).tellg();
      	cout << file\_size;
      }        
      

      My question is, this code gives misses one byte. Can you guys give me any reason for it. Thanks

      I appreciate your help all the time... CodingLover :)

      CPalliniC Offline
      CPalliniC Offline
      CPallini
      wrote on last edited by
      #3

      CodingLover wrote:

      file_size = in_file.seekg(-1, ios_base::end).tellg();

      should be

      file_size = in_file.seekg(0, ios_base::end).tellg();

      BTW you may also use GetFileSize [^]. :)

      If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
      This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke

      In testa che avete, signor di Ceprano?

      C 1 Reply Last reply
      0
      • CPalliniC CPallini

        CodingLover wrote:

        file_size = in_file.seekg(-1, ios_base::end).tellg();

        should be

        file_size = in_file.seekg(0, ios_base::end).tellg();

        BTW you may also use GetFileSize [^]. :)

        If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
        This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke

        C Offline
        C Offline
        CodingLover
        wrote on last edited by
        #4

        Thanks, so my attempt is not bad. :) Is that GetFileName is standard C++. I'm confusing with those.

        I appreciate your help all the time... CodingLover :)

        CPalliniC 1 Reply Last reply
        0
        • D Doc Lobster

          I guess with seekg you are looking for the position -1 from the end. Therefore you are missing the byte. Additionally look here: http://www.codeproject.com/KB/files/filesize.aspx[^] http://www.cplusplus.com/reference/iostream/istream/seekg.html[^]

          C Offline
          C Offline
          CodingLover
          wrote on last edited by
          #5

          Thanks for links.

          I appreciate your help all the time... CodingLover :)

          1 Reply Last reply
          0
          • C CodingLover

            Thanks, so my attempt is not bad. :) Is that GetFileName is standard C++. I'm confusing with those.

            I appreciate your help all the time... CodingLover :)

            CPalliniC Offline
            CPalliniC Offline
            CPallini
            wrote on last edited by
            #6

            CodingLover wrote:

            Is that GetFileName is standard C++. I'm confusing with those.

            Nope. It is provided by Win32 API. If you just need to avoid MFC that's is fine, on the other hand, if you need to be stuck with C++ standard, then use ifstream. :)

            If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
            This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke

            In testa che avete, signor di Ceprano?

            C 1 Reply Last reply
            0
            • CPalliniC CPallini

              CodingLover wrote:

              Is that GetFileName is standard C++. I'm confusing with those.

              Nope. It is provided by Win32 API. If you just need to avoid MFC that's is fine, on the other hand, if you need to be stuck with C++ standard, then use ifstream. :)

              If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
              This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke

              C Offline
              C Offline
              CodingLover
              wrote on last edited by
              #7

              Since I start to learn C++, confusing with this. Is that best way to work with standard C++? Sometimes I feel that use of MFC easy than standard C++, and other way too. Appreciate your explanation on it.

              I appreciate your help all the time... CodingLover :)

              CPalliniC 1 Reply Last reply
              0
              • C CodingLover

                Since I start to learn C++, confusing with this. Is that best way to work with standard C++? Sometimes I feel that use of MFC easy than standard C++, and other way too. Appreciate your explanation on it.

                I appreciate your help all the time... CodingLover :)

                CPalliniC Offline
                CPalliniC Offline
                CPallini
                wrote on last edited by
                #8

                Using C++ standard you write code that will execute also on different platforms (for instance Linux). On the other hand, if you need to exploit Windows not-portable functionalities (like the GUI components1) the you have to use Win32 API or MFC. GUI development with MFC is usually simpler. :) (1) There are exceptions: for instance QT [^] is a framework that enable you to write portable GUI code.

                If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
                This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke

                In testa che avete, signor di Ceprano?

                C 1 Reply Last reply
                0
                • CPalliniC CPallini

                  Using C++ standard you write code that will execute also on different platforms (for instance Linux). On the other hand, if you need to exploit Windows not-portable functionalities (like the GUI components1) the you have to use Win32 API or MFC. GUI development with MFC is usually simpler. :) (1) There are exceptions: for instance QT [^] is a framework that enable you to write portable GUI code.

                  If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
                  This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke

                  C Offline
                  C Offline
                  CodingLover
                  wrote on last edited by
                  #9

                  Ok, as I said now I have mixture of them. If I want to learn standard C++ from where I should begin.

                  I appreciate your help all the time... CodingLover :)

                  CPalliniC 1 Reply Last reply
                  0
                  • C CodingLover

                    Ok, as I said now I have mixture of them. If I want to learn standard C++ from where I should begin.

                    I appreciate your help all the time... CodingLover :)

                    CPalliniC Offline
                    CPalliniC Offline
                    CPallini
                    wrote on last edited by
                    #10

                    What about "The C++ Programming Language" [^] book of Stroustrup? :)

                    If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
                    This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke

                    In testa che avete, signor di Ceprano?

                    C 1 Reply Last reply
                    0
                    • CPalliniC CPallini

                      What about "The C++ Programming Language" [^] book of Stroustrup? :)

                      If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
                      This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke

                      C Offline
                      C Offline
                      CodingLover
                      wrote on last edited by
                      #11

                      Thanks a lot. :)

                      I appreciate your help all the time... CodingLover :)

                      1 Reply Last reply
                      0
                      Reply
                      • Reply as topic
                      Log in to reply
                      • Oldest to Newest
                      • Newest to Oldest
                      • Most Votes


                      • Login

                      • Don't have an account? Register

                      • Login or register to search.
                      • First post
                        Last post
                      0
                      • Categories
                      • Recent
                      • Tags
                      • Popular
                      • World
                      • Users
                      • Groups