[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

What is the best way to read text files ?

Posted on 1998-10-31
5
Medium Priority
?
1,172 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 400 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: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

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

Introduction: Displaying information on the statusbar.   Continuing from the third article about sudoku.   Open the project in visual studio. Status bar – let’s display the timestamp there.  We need to get the timestamp from the document s…
Introduction: Ownerdraw of the grid button.  A singleton class implentation and usage. Continuing from the fifth article about sudoku.   Open the project in visual studio. Go to the class view – CGridButton should be visible as a class.  R…
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.
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…

656 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