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. CFile Question

CFile Question

Scheduled Pinned Locked Moved C / C++ / MFC
questionhtmljsonperformancehelp
3 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.
  • J Offline
    J Offline
    John Clump
    wrote on last edited by
    #1

    Hi everyone. I have a question about the use of CFile. I have the code below, and the filename is that of a file which I have tested and determined exists, and is not shared. The file is an HTML file, and I want to take the contents of the HTML file (the text), and put it into a CString. The string will then be parsed and the hyperlinks extracted. CFile cFile; CString strFileContents; cFile.Open(m_strFileName, CFile::modeRead | CFile::shareExclusive); cFile.Read(&strFileContents, cFile.GetLength()); cFile.Close(); The problem with the code above is that when the Read is performed, the program crashes. I tried using an old style C string in place of a CString, the data is read but a bunch of garbage chars are at the end of the string, and the program crashes when I hit Close(). Anyone know what I am doing wrong? I really want the file contents in a CString, because the necessary memory is allocated dynamically, and the CString Find functions are great for parsing.

    T D 2 Replies Last reply
    0
    • J John Clump

      Hi everyone. I have a question about the use of CFile. I have the code below, and the filename is that of a file which I have tested and determined exists, and is not shared. The file is an HTML file, and I want to take the contents of the HTML file (the text), and put it into a CString. The string will then be parsed and the hyperlinks extracted. CFile cFile; CString strFileContents; cFile.Open(m_strFileName, CFile::modeRead | CFile::shareExclusive); cFile.Read(&strFileContents, cFile.GetLength()); cFile.Close(); The problem with the code above is that when the Read is performed, the program crashes. I tried using an old style C string in place of a CString, the data is read but a bunch of garbage chars are at the end of the string, and the program crashes when I hit Close(). Anyone know what I am doing wrong? I really want the file contents in a CString, because the necessary memory is allocated dynamically, and the CString Find functions are great for parsing.

      T Offline
      T Offline
      Trin2
      wrote on last edited by
      #2

      First of all, you can't put the whole file in a single CString object. You may try to read the file line by line. You do not use the Read function correctly, first parameter is the block you read, the second is the size of that block. Usualy used for reading a binnary file. Good Luck! Trin2

      1 Reply Last reply
      0
      • J John Clump

        Hi everyone. I have a question about the use of CFile. I have the code below, and the filename is that of a file which I have tested and determined exists, and is not shared. The file is an HTML file, and I want to take the contents of the HTML file (the text), and put it into a CString. The string will then be parsed and the hyperlinks extracted. CFile cFile; CString strFileContents; cFile.Open(m_strFileName, CFile::modeRead | CFile::shareExclusive); cFile.Read(&strFileContents, cFile.GetLength()); cFile.Close(); The problem with the code above is that when the Read is performed, the program crashes. I tried using an old style C string in place of a CString, the data is read but a bunch of garbage chars are at the end of the string, and the program crashes when I hit Close(). Anyone know what I am doing wrong? I really want the file contents in a CString, because the necessary memory is allocated dynamically, and the CString Find functions are great for parsing.

        D Offline
        D Offline
        Dudi Avramov
        wrote on last edited by
        #3

        CSting doesn't allocated anything till you initialize it. Since you didn't allocated, you got crash. Change your code to the following: cFile.Read(strFileContents.GetBuffer(SIZE), cFile.GetLength()); After finishing working with the string call strFileContents.ReleaseBuffer();

        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