Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Better way to read in TXT Files (now very very slow)

Status
Not open for further replies.

Insider1984

Technical User
Feb 15, 2002
132
US
I'm looking for a faster and better way to read in straight txt files (with newline characters in tact etc).

Right now I basically rip the characters out 1 by one adding them to a dynamic array. it worked for some older programs that took txt files only 100-1000 characters long but now I have 100kb txt files that I want to load into a CString.

Is there a Visual C++ class? Is it easy? Fast?

Thanks for the help.

=====================
Insider
4 year 'on the fly' programmer
C++ Basic Java
 
Try this and see if it is faster.
Code:
void CMainDlg::readfile( CString& outstr, LPCTSTR filename){

	CFile f;
	f.Open(filename, CFile::modeRead);
	char* buf = outstr.GetBufferSetLength( f.GetLength());
	f.Read( buf, f.GetLength());
	outstr.ReleaseBuffer();
	f.Close();
}

-pete
 
Okay I found CFile. Wow does it seem easy but a few problems:

MY Char -> Char allowed me to cut at the end of the file with 'EOF'.

This code below always seems to give me 5-0 characters of junk at the end. Any help would be great:

(The inactive line gave me no help and could be wrong anyway)

Code:
	CFile file_in;
	CString file_name="C:\\temp.txt";
	file_in.Open(file_name,CFile::modeRead);
	char* buffer = new char[file_in.GetLength()];
	file_in.Read(buffer,file_in.GetLength());
	//buffer1 = buffer1.Mid(0,buffer1.Find(EOF));
	AfxMessageBox(buffer,MB_OK);

=====================
Insider
4 year 'on the fly' programmer
C++ Basic Java
 
Hi,
maybe, the following code help you
Code:
CStdioFile inFile("MyFile.bin"), CFile::modeRead |
                   CFile::typebinary);

const UINT linelength = 18;
TCHAR strBuf[20]; // or CString

while(inFile.ReadString(strBuf, linelength))
{
  //search for the word in strBuf 
}
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top