使用fgets配合sscanf读文件

(178, 2, 'views', '2856'),
(180, 128, 'views', '586'),
(182, 136, 'views', '542'),
(184, 139, 'views', '521'),
(186, 146, 'views', '973'),
(188, 158, 'views', '932'),
(189, 162, 'views', '447'),
(190, 198, 'views', '1512'),
(192, 206, 'views', '733'),
(193, 214, 'views', '549'),
(195, 289, 'views', '1419'),
(198, 357, 'views', '1558'),
(200, 372, 'views', '769'),
(202, 1128, 'views', '743'),
(204, 2669, 'views', '377'),
(208, 3843, 'views', '292'),
(211, 3876, 'views', '116'),
(236, 160, 'views', '566'),
(237, 551, 'views', '1195'),
(239, 561, 'views', '26'),
(241, 596, 'views', '1837')

数据如上图,代码如下:

    CRect rc;
m_list.GetClientRect(rc);
int nWidth=rc.Width()/4;
m_list.InsertColumn(0,"meta_id",LVCFMT_LEFT,nWidth);
m_list.InsertColumn(1,"post_id",LVCFMT_LEFT,nWidth);
m_list.InsertColumn(2,"meta_key",LVCFMT_LEFT,nWidth);
m_list.InsertColumn(3,"meta_value",LVCFMT_LEFT,nWidth);
FILE *f=fopen("1.sql","r");
int meta_id,post_id,meta_value;
char meta_key[6];
char ch[50];
CString strSql="";
CString str;
int nIndex=0;
while (NULL!=fgets(ch,50,f))
{
sscanf(ch,"(%d, %d, '%[^',']', '%d'),",&meta_id,&post_id,meta_key,&meta_value);
str.Format("%d",meta_id);
m_list.InsertItem(nIndex,str);
str.Format("%d",post_id);
m_list.SetItemText(nIndex,1,str);
m_list.SetItemText(nIndex,2,meta_key);
str.Format("%d",meta_value);
m_list.SetItemText(nIndex,3,str);
nIndex++;
}

使用这一句也一样:

while(EOF!=fscanf(f,"(%d, %d, '%[^',']', '%d'),\n",&meta_id,&post_id,meta_key,&meta_value))
posted @ 2011-10-18 19:52  likebeta  阅读(1098)  评论(0编辑  收藏  举报