reading lines from files
Jul 18, 2008 at 6:00pm UTC
I have a text file with two lines-one begins with dbinfo and the other with dbdef.how can i put the first line into the dbinfo string and the second line in the dbdef string?Please help
Jul 19, 2008 at 7:36am UTC
You can try something like this
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34
#include<iostream>
#include<string>
#include<fstream>
int main()
{
std::string sFileName("TextFile.txt" );
std::fstream sFile;
sFile.open(sFileName.c_str());
std::string sDBINFO;
std::string sDBDEF;
std::string sTMP;
char szTMP[255];
for (int nCounter=0;nCounter<2;nCounter++)
{
sFile.getline(szTMP,255);
sTMP=szTMP;
strcpy(szTMP,"" );
size_t found;
found=sTMP.find("dbinfo" );
if (found!=std::string::npos)
{
sDBINFO=sTMP;
}
else
{
sDBDEF=sTMP;
}
}
std::cout<<sDBDEF<<std::endl;
std::cout<<sDBINFO<<std::endl;
sFile.close();
return 0;
}
Jul 20, 2008 at 7:09pm UTC
Thanks for your help
Topic archived. No new replies allowed.