Solved

What is the best way to read text files ?

Posted on 1998-10-31
5
1,129 Views
Last Modified: 2013-11-20
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
0
Comment
Question by:atari
  • 3
5 Comments
 

Expert Comment

by:jeggy
Comment Utility
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 ?
0
 
LVL 1

Author Comment

by:atari
Comment Utility
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 ?
0
 
LVL 86

Accepted Solution

by:
jkr earned 100 total points
Comment Utility
Simply use 'ReadString()' specifying _no_ buffer size, so the function will remove the LF itself (reading to the '\n' delimiter is the default behaviour, but a given buffer will be filled without removing the delimiting characters).
0
 
LVL 1

Author Comment

by:atari
Comment Utility
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
0
 
LVL 1

Author Comment

by:atari
Comment Utility
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
0

Featured Post

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

This is to be the first in a series of articles demonstrating the development of a complete windows based application using the MFC classes.  I’ll try to keep each article focused on one (or a couple) of the tasks that one may meet.   Introductio…
Introduction: Database storage, where is the exe actually on the disc? Playing a game selected randomly (how to generate random numbers).  Error trapping with try..catch to help the code run even if something goes wrong. Continuing from the seve…
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

763 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now