×
Namespaces

Variants
Actions

如何读取CSV文件

From Nokia Developer Wiki
Jump to: navigation, search
Article Metadata

文章
翻译:
hoolee
最后由 hamishwillee 在 22 Dec 2011 编辑


下列内容演示了如何了如何读取一个Comma Spearated Values (CSV)文件

CSV文件

示例CSV文件

下面是一个CSV demo文件,我们将读取它的内容,并填入数据库

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

这个文件可以在记事本中输入内容,并保存为unicode格式

读取文件的代码

下列函数读取上述文件并填入数据库

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
}
This page was last modified on 22 December 2011, at 07:59.
149 page views in the last 30 days.

Was this page helpful?

Your feedback about this content is important. Let us know what you think.

 

Thank you!

We appreciate your feedback.

×