Re: reading string from a text file from vc++ without MFC support
rindam2002@yahoo.com wrote:
Hi
I am having two text files.
----------------text file1----------------
apple
mango
banana
--------
------random number of line
-------------End of text file1------------
----------------text file----------------
www.wonders.com
www.magic.com
www.moris.com
--------
------random number of line
-------------End of text file1------------
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.
I tried to do like this---->
FILE *fp=NULL;
fp=_tfopen(lpstrPathToUserRss,_T("a+")); //opens Text
file 1
static const int maxLineChars = 200;
TCHAR line[ maxLineChars ];
while ( ! feof(fp) )
{
// Clear destination buffer
::ZeroMemory( line, sizeof(line) );
// Read the line
_fgetts( line, maxLineChars, fp );
CString strLine( line );
FILE *fp1=NULL;
fp1=_tfopen(lpstrPathToUserRssXmlPath,_T("a+")); //opens text file2
static const int maxLineCharsForXmlPath
= 200;
TCHAR
lineForXmlPath[ maxLineCharsForXmlPath ];
while ( ! feof(fp1) )
{
// Clear destination buffer
::ZeroMemory( lineForXmlPath,
sizeof(lineForXmlPath) );
// Read the line
_fgetts( lineForXmlPath,
maxLineCharsForXmlPath, fp1 );
// Now 'line' contains the line read from file.
// Do your processing.
// e.g.: You may store the read line into a
CString
CString strLineForXmlPath( lineForXmlPath );
} //end of second while loop
} //end of first while loop
but this code is wrong because it is getting line1 from text file1
and all lines from text file2 at first time,then it is getting line2
from text file1 and all lines from text file2 at second time,then it
is getting line3 from text file1 and all lines from text file2 at
third time and so on.But what I want is not that.I want to get only
line1 from both files at a time,then only line2 from both files at a
time ,then only line3 from both files at a time and so on upto the
end of files.Plz help me to solve this problem.
rindam:
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.
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;
// Read line by line from file
while ( (std::getline(file1, line1) &&
(std::getline(file2, line2) )
{
// Process line1, line2
}
You can use the c_str() member to get the buffer from the string.
--
David Wilkinson
Visual C++ MVP