Re: reading string from a text file from vc++ without MFC support
On Wed, 20 Jun 2007 02:15:18 -0700, rindam2002@yahoo.com wrote:
I am having two text files.
Now what I want is that I want to read the first lines from both file
at a time and do some calculations on them,then I want to read the
second lines from both file and do some calculations on them and so on
for all the lines in both files.This two files will have same number
of lines.
You might want to use my TextFileReader class (at the end of this post
- note that the class needs testing), like so:
<CODE>
TextFileReader file1( _T("c:\\data1.txt") );
TextFileReader file2( _T("c:\\data2.txt") );
ASSERT( file1.IsOpen() );
ASSERT( file2.IsOpen() );
// Check the file are open successfully:
//
//if ( ! file1.IsOpen() )
// ... error file 1 open
//if ( ! file2.IsOpen() )
// ... error file 2 open
// Stop when find an empty line
while ( true )
{
// Read lines from files
CString line1 = file1.ReadLine();
CString line2 = file2.ReadLine();
if ( line1.IsEmpty() || line2.IsEmpty() )
break;
// Process the lines...
CString msg;
msg = CString( _T("Line from File #1 : ") ) + line1;
msg += CString( _T("\nLine from File #2 : ") ) + line2;
AfxMessageBox(msg, MB_OK);
}
</CODE>
<FILE name="TextFileReader.h">
#pragma once
class TextFileReader
{
public:
TextFileReader();
// Tries to open the file with given name.
// Check with IsOpen() after constructor for operation result.
explicit TextFileReader( LPCTSTR fileName );
// Closes the file, if open
virtual ~TextFileReader();
// Opens the file.
// Returns 'true' on success, 'false' on error
bool Open( LPCTSTR fileName );
// Reads line from file.
// On error or end of file, returns empty string.
// (Use EoF/FileError methods to check EOF or error condition)
CString ReadLine( bool removeNewLine = true );
// Close the file (if open)
void Close();
// Check if the file is opened
bool IsOpen() const;
// Return file name
LPCTSTR GetFileName() const;
// End of file reached?
bool EoF();
// Error condition?
bool FileError();
//
// IMPLEMENTATION
//
protected:
FILE * m_file; // C file handle
CString m_fileName; // File name
// Helper to remove last '\n
TCHAR * RemoveEndingNewLine( TCHAR * line );
//
// Ban copy
//
private:
TextFileReader( const TextFileReader & );
TextFileReader & operator=( const TextFileReader & );
};
//
// INLINE METHODS
//
inline bool TextFileReader::IsOpen() const
{
return ( ( m_file != NULL ) ? true : false );
}
inline LPCTSTR TextFileReader::GetFileName() const
{
return static_cast<LPCTSTR>( m_fileName );
}
inline TCHAR * TextFileReader::RemoveEndingNewLine( TCHAR * line )
{
const size_t len = _tcslen(line);
if ( len != 0 &&
( line[ len - 1 ] == _T('\n') ) )
{
line[ len - 1 ] = _T('\0');
}
return line;
}
inline bool TextFileReader::EoF()
{
ASSERT( m_file != NULL );
return ( feof(m_file) ? true : false );
}
inline bool TextFileReader::FileError()
{
ASSERT( m_file != NULL );
return ( ferror(m_file) ? true : false );
}
</FILE>
<FILE name="TextFileReader.cpp">
#include "StdAfx.h"
#include "TextFileReader.h"
TextFileReader::TextFileReader()
: m_file(NULL)
{
return;
}
TextFileReader::TextFileReader( LPCTSTR fileName )
: m_file(NULL)
{
ASSERT( fileName != NULL );
// Check result with IsOpen()
Open( fileName );
}
TextFileReader::~TextFileReader()
{
Close();
}
bool TextFileReader::Open( LPCTSTR fileName )
{
// Check name
ASSERT( fileName != NULL );
if ( fileName == NULL )
return false;
// The file must be closed
if ( m_file != NULL )
return false;
// Try open the file
m_file = _tfopen( fileName, _T("rt") );
if ( m_file == NULL )
return false;
// Store file name
m_fileName = fileName;
// All right
return true;
}
CString TextFileReader::ReadLine( bool removeNewLine )
{
// Check that the file is open
ASSERT( m_file != NULL );
if ( m_file == NULL )
return _T("");
// Buffer to read the line
static const int maxLineBufferChars = 260;
TCHAR lineBuffer[ maxLineBufferChars ];
// Clear whole buffer before reading
::ZeroMemory( lineBuffer, sizeof(lineBuffer) );
// Read the line from file
if ( _fgetts( lineBuffer, maxLineBufferChars, m_file ) == NULL )
return _T("");
// Remove new line, if requested
if ( removeNewLine )
RemoveEndingNewLine(lineBuffer);
return CString( lineBuffer );
}
void TextFileReader::Close()
{
// Safely close the file
if ( m_file != NULL )
{
fclose( m_file );
m_file = NULL;
m_fileName.Empty();
}
ASSERT( m_fileName.IsEmpty() );
}
</FILE>
MrAsm