Solved

What is the best way to read text files ?

Posted on 1998-10-31
5
1,147 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
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: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering 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
deburging in oracle form 12 105
sumNumber challenge 16 139
Window placement 17 87
I could not set window to top 4 21
Introduction: Load and Save to file, Document-View interaction inside the SDI. Continuing from the second article about sudoku.   Open the project in visual studio. From the class view select CSudokuDoc and double click to open the header …
Have you tried to learn about Unicode, UTF-8, and multibyte text encoding and all the articles are just too "academic" or too technical? This article aims to make the whole topic easy for just about anyone to understand.
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.
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…

829 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