CStdioFile::ReadString problem carriage return when loading txt file

Started by
0 comments, last by rip-off 12 years ago
Hi
I have problem when I load text file line by line. Most of the time the code below works but whensay somewhere in the middle of a string that I am loading is hidden a carriage return then the CStdioFile::ReadString will read the string only up to the carriage return and the rest of the string after the carriage return is ignored. Is it because I amloading the file as text?


#include <windows.h>
void LoadFile( const CString &path, UINT nOpenFlags )
{
CStdioFile file;
CString row;
if( file.Open( path, nOpenFlags ) )
{
int countLines = 0;
//reads line by line untill the end of file
//here is the problem half line is read & stored in "row" up to hidden carriage return
while ( file.ReadString( row ) != NULL )
{
++countLines;
}
file.Close();
}
}

void main()
{
LoadFile( "c:\\windows\\data.txt", CFile::modeRead | CFile::shareDenyWrite);
}



Any suggestions how to resolve this so I can read entire line of text ignoring carriage return. But actually I am not reading the line but the CStdioFile::ReadString function does which means that I am a bit limited. Am I? Any suggestions how to resolve it using CStdioFile functions or any other loading tools with bigger flexibility? Samples of code would be a big help. I am loading mainly CSV files with thousands of lines of text separated with commas so I need something very effecient.
Advertisement
How is this file created? How do these carriage returns end up in the file?

What is the desired behaviour - do you define a line as any text up to a single newline character (i.e. \n)? Or is your definition of a line the same as that of windows, carriage return followed by newline (i.e. \r\n)?


Any suggestions how to resolve it using CStdioFile functions...
[/quote]
I have never used the CStdioFile class, but it appears to support "binary" access, using the CFile::typeBinary flag. So called "binary" access to files just means that it doesn't try to handle end of line sequences as being special. This means you'll get exactly the data in the file.

You could use the Read() method to get the data into a buffer, and process a "line" every time your defined end of line sequence is encountered. For skipping extraneous carriage returns, you could use a function like std::remove() on the buffer first. You can use the std::distance() between the return value of std::remove and the beginning of the buffer to keep track of the modified buffer size.

The buffer could be an instance of std::vector<>.


Any suggestions how to resolve it using ... any other loading tools with bigger flexibility? Samples of code would be a big help. I am loading mainly CSV files with thousands of lines of text separated with commas so I need something very effecient.
[/quote]
There are probably hundreds of CSV parser libraries for C++. I can't recommend any in particular, but you do not need to reinvent the wheel for this. In particular, if you need to handle arbitrary CSV, you should probably use a library that can handle quoted strings in the CSV (allowing the values to contain commas themselves).

[hr]

Sidenote: I find it extremely suspicious that this file is in the "Windows" folder. You should not put your own files there. The fact that you are doing so indicates that you are running your processes at a high privilege level. Bugs in your program could do more damage that way, corrupting critical system files, etc.

This topic is closed to new replies.

Advertisement