Re: reading string from a text file from vc++ without MFC support

Tech-Archive recommends: Repair Windows Errors & Optimize Windows Performance



On Wed, 20 Jun 2007 07:26:52 -0500, David Wilkinson
<no-reply@xxxxxxxxxxxx> wrote:


You only need one while loop.

FILE* file1;
FILE* file2
// open the files
TCHAR line1[ maxLineChars ];
TCHAR line2[ maxLineChars ];

// Read line by line from file
while ( (_fgetts( line1, maxLineChars, file1 ) != NULL) ) &&
(_fgetts( line2, maxLineChars, file2 ) != NULL) )
{
// Process line1, line2
}

Note that, as a discussed yesterday in this thread, testing for eof() is
not the correct way to write the while condition for reading text lines
from a file.

Yes, it is important that David has marked this point, because the
original version of the code that I posted was *wrong* about feof()
use.

(The second version I posted with the C++ helper class has been
corrected thanks to David's note.)


Note also that you can avoid specifying a (possibly wrong) maximum file
length by using std::getline():

std::ifstream file1;
std::ifstream file2
// open the files
std::string line1;
std::string line2;

I posted in this same thread a reading code using C++ iostreams, but
the OP wrote that there was a requirement that he could not use C++
iostreams nor MFC CFile/CStdioFile.

I agree with you that C++ iostreams tend to be more elegant and robust
than C FILE*.

MrAsm
.



Relevant Pages