CFile Question
-
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. -
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.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
-
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.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();