How to read a CSV file using Symbian C++
This article shows how to read a Comma Separated Values (CSV) file and save it into a database using Symbian C++.
Article Metadata
The CSV File
Demo CSV file Below is a Demo CSV file whose contents will be read and a database will be populated.
avi,E:\Others\avi.amr
joe,E:\Others\joe.amr
jill,E:\Others\jill.amr
microsoft hotmail,E:\Others\msn.amr
google gmail,E:\Others\gmail.amr
yahoo yahoomail,E:\Others\ymail.amr
Winamp MP3,E:\Others\winamp.mp3
MySong MP3,E:\Others\mysong.mp3
The file is edited in notepad and saved with the Unicode formatting.
Code to read the file
The following function reads the above file and populates the database
iSearchDb = CBookstoreDb::NewL();
if(!BaflUtils::FileExists(CCoeEnv::Static()->FsSession(), iDatabaseFile))
{
iSearchDb->CreateDbL(iDatabaseFile);
///////////////////Read CSV File
RFs fs;
fs.Connect();
RFile hist;
TBuf<300> iLine;
if (hist.Open(fs,KCSVFile,EFileRead) == KErrNone)
{
TFileText text;
text.Set(hist);
iLine.Copy( _L(" "));
while (iLine.Length() > 0)
{
text.Read(iLine);
iLine.ZeroTerminate();
if (iLine.Length() > 0)
{
TInt i = iLine.Locate(',');
TBuf<50> iTempDes;
TBuf<128> iTempPath;
iTempDes.Copy(iLine.Left(i));
iTempPath.Copy(iLine.Mid(i+1));
iSearchDb->AddWithSqlL(iTempPath,iTempDes);
}
}
hist.Close();
}
fs.Close();
// Read CSV File Done
}


(no comments yet)