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. Detecting the end of the file

Detecting the end of the file

Scheduled Pinned Locked Moved C / C++ / MFC
csshelpquestion
5 Posts 3 Posters 0 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.
  • A Offline
    A Offline
    Aint
    wrote on last edited by
    #1

    Hello. These are the codes that I have used to read a bin file and to store in 3 variables. Most of the files read have 1024 columns, hence iI < 1024. If i try to open a file that is less than 1024, the program will give me unexpected file format error. How can detect the end of the file and break from the loop if a file of less than 1024 column is opened? Thanks. void CVecDoc::Serialize(CArchive& ar) { int iI, iK; unsigned char cDataOne, cDataTwo; short sSwap, as[3]; if (ar.IsStoring()) { // TODO: add storing code here } else { // TODO: add loading code here for (iI = 0; iI < 1024; iI++) { for (iK = 0; iK < 3; iK++) { ar >> cDataOne >> cDataTwo; sSwap = ((short)cDataTwo << 8) + (short)cDataOne; as[iK] = sSwap; m_a1[iI] = as[0]; m_a2[iI] = as[1]; m_a3[iI] = as[2]; } } } }

    M D 2 Replies Last reply
    0
    • A Aint

      Hello. These are the codes that I have used to read a bin file and to store in 3 variables. Most of the files read have 1024 columns, hence iI < 1024. If i try to open a file that is less than 1024, the program will give me unexpected file format error. How can detect the end of the file and break from the loop if a file of less than 1024 column is opened? Thanks. void CVecDoc::Serialize(CArchive& ar) { int iI, iK; unsigned char cDataOne, cDataTwo; short sSwap, as[3]; if (ar.IsStoring()) { // TODO: add storing code here } else { // TODO: add loading code here for (iI = 0; iI < 1024; iI++) { for (iK = 0; iK < 3; iK++) { ar >> cDataOne >> cDataTwo; sSwap = ((short)cDataTwo << 8) + (short)cDataOne; as[iK] = sSwap; m_a1[iI] = as[0]; m_a2[iI] = as[1]; m_a3[iI] = as[2]; } } } }

      M Offline
      M Offline
      Moonis Ahmed
      wrote on last edited by
      #2

      Hi, There is an end of file function eof() which you can use. It is a boolean function which returns a non-zero number when the end of file is reached otherwise it returns zero (meaning false). You will have to use a stream variable to use the function. A very simple article explaining the function is here: http://www.mathbits.org/MathBits/CompSci/Files/End.htm Hope this helps. - Moonis

      1 Reply Last reply
      0
      • A Aint

        Hello. These are the codes that I have used to read a bin file and to store in 3 variables. Most of the files read have 1024 columns, hence iI < 1024. If i try to open a file that is less than 1024, the program will give me unexpected file format error. How can detect the end of the file and break from the loop if a file of less than 1024 column is opened? Thanks. void CVecDoc::Serialize(CArchive& ar) { int iI, iK; unsigned char cDataOne, cDataTwo; short sSwap, as[3]; if (ar.IsStoring()) { // TODO: add storing code here } else { // TODO: add loading code here for (iI = 0; iI < 1024; iI++) { for (iK = 0; iK < 3; iK++) { ar >> cDataOne >> cDataTwo; sSwap = ((short)cDataTwo << 8) + (short)cDataOne; as[iK] = sSwap; m_a1[iI] = as[0]; m_a2[iI] = as[1]; m_a3[iI] = as[2]; } } } }

        D Offline
        D Offline
        David Crow
        wrote on last edited by
        #3

        Aint wrote:

        Most of the files read have 1024 columns... How can...break from the loop if a file of less than 1024 column is opened?

        Which is a concept known only to your code. It's up to you to determine if the file contains 1024 columns or not.

        Aint wrote:

        If i try to open a file that is less than 1024, the program will give me unexpected file format error.

        Have you stepped through the code to find out at what point this error occurs?


        "A good athlete is the result of a good and worthy opponent." - David Crow

        "To have a respect for ourselves guides our morals; to have deference for others governs our manners." - Laurence Sterne

        A 1 Reply Last reply
        0
        • D David Crow

          Aint wrote:

          Most of the files read have 1024 columns... How can...break from the loop if a file of less than 1024 column is opened?

          Which is a concept known only to your code. It's up to you to determine if the file contains 1024 columns or not.

          Aint wrote:

          If i try to open a file that is less than 1024, the program will give me unexpected file format error.

          Have you stepped through the code to find out at what point this error occurs?


          "A good athlete is the result of a good and worthy opponent." - David Crow

          "To have a respect for ourselves guides our morals; to have deference for others governs our manners." - Laurence Sterne

          A Offline
          A Offline
          Aint
          wrote on last edited by
          #4

          Yes. It shows that the error occur before the loop end, when iI = 300+. Can I use ar.??? to detect the end of the file?

          D 1 Reply Last reply
          0
          • A Aint

            Yes. It shows that the error occur before the loop end, when iI = 300+. Can I use ar.??? to detect the end of the file?

            D Offline
            D Offline
            David Crow
            wrote on last edited by
            #5

            Aint wrote:

            It shows that the error occur before the loop end, when iI = 300+.

            How large are m_a1, m_a2, and m_a3?


            "A good athlete is the result of a good and worthy opponent." - David Crow

            "To have a respect for ourselves guides our morals; to have deference for others governs our manners." - Laurence Sterne

            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