Solved

What is the best way to read text files ?

Posted on 1998-10-31
5
1,149 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
5 Comments
 

Expert Comment

by:jeggy
ID: 1324081
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
ID: 1324082
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
ID: 1324083
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
ID: 1324084
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
ID: 1324085
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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Adapt this command to show who installed 29 112
pre4 challenge 19 110
scoreUp challenge 14 69
Unix Command -- Challenging  question 7 101
In this article, I'll describe -- and show pictures of -- some of the significant additions that have been made available to programmers in the MFC Feature Pack for Visual C++ 2008.  These same feature are in the MFC libraries that come with Visual …
If you use Adobe Reader X it is possible you can't open OLE PDF documents in the standard. The reason is the 'save box mode' in adobe reader X. Many people think the protected Mode of adobe reader x is only to stop the write access. But this fe…
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.
How to Install VMware Tools in Red Hat Enterprise Linux 6.4 (RHEL 6.4) Step-by-Step Tutorial

749 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