Link to home
Start Free TrialLog in
Avatar of atari
atari

asked on

What is the best way to read text files ?

Asked that question long times ago,
and got the answer to do it with CStdioFile something like this:

CStdioFile file;
if(!file.Open(log_file_name,CFile::modeRead|CFile::typeText)) return;

  TCHAR buffer[1024];
  while (file.ReadString(buffer,1024)!=0);
  file.Close();

But there is always a very disturbing \n linefeed-sign at the end of each line.

So I tried it with CArchive::ReadString something like this:

char txt[SDR_MAXTXT];
if (ar.ReadString(txt,count)!=NULL);

and so on, but with some texts I got problem, ReadString returned an error.

So is there a more easier way to read text files, that I didn't know and that I should try ?

Thanks,
bye,
atari
Avatar of jeggy
jeggy

How about including something like (use a CString for the line buffer)

 int iLFPos=csLine.ReverseFind('\n');
 if (iLFPos>=0)
    csLine=csLine.Left(iLFPos);

in the CStdioFile solution after reading the line ?
Avatar of atari

ASKER

Sorry, I have implemented something like this:

char *CutReadString(char *buffer)
{
  int string_length=strlen(buffer);

  if (buffer[string_length-1]=='\n')
      buffer[string_length-1]='\0';

  return buffer;
}

but I think, it is rather slow to read large text files only this way. Maybe there is another solution for reading large text files, that is faster than the CStdioFile::ReadString-method ?
ASKER CERTIFIED SOLUTION
Avatar of jkr
jkr
Flag of Germany image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of atari

ASKER

Hi,
I don't know why I implemented the ReadString-function with a buffer, but with a CString it also works and has no \n at the end of the string. Maybe it was the cause, that I was new in MFC when I wrote this and there was an example in online-help:

// example for CStdioFile::ReadString
extern CStdioFile f;
char buf[100];

f.ReadString( buf, 99 );

so I implemented it with a buffer.

Thanks for your help.
Bye atari
Avatar of atari

ASKER

Hi,
I don't know why I implemented the ReadString-function with a buffer, but with a CString it also works and has no \n at the end of the string. Maybe it was the cause, that I was new in MFC when I wrote this and there was an example in online-help:

// example for CStdioFile::ReadString
extern CStdioFile f;
char buf[100];

f.ReadString( buf, 99 );

so I implemented it with a buffer.

Thanks for your help.
Bye atari