mfc 文件

feixiangdeyu12 2009-09-04 09:27:33
问一下大家,看我的代码有什么问题:
i_Count=0;
i_FileCount=0;
ifstream fin("index.html");
while(!m_pRecordset->adoEOF)
{
i_Count++;
var = m_pRecordset->GetCollect("title");
if(var.vt != VT_NULL)
strTitle = _com_util::ConvertBSTRToString((_bstr_t)var);

var = m_pRecordset->GetCollect("class");
if(var.vt != VT_NULL)
strClass = _com_util::ConvertBSTRToString((_bstr_t)var);

var = m_pRecordset->GetCollect("num");
if(var.vt != VT_NULL)
strNum = _com_util::ConvertBSTRToString((_bstr_t)var);

if (i_Count>=20)
{
i_FileCount++;
i_Count=0;
}
m_FileName.Format("indexq[%d].html",i_FileCount);
ofstream fout(m_FileName,ios::app);

string str,str1;
str1=" <p><a href=\"/index.htm\">首页</a> / 新闻</p>";

if(i_Count==1)
{
while (getline (fin, str) )
{
fstream fout(m_FileName,ios::app);
if(fout)
{
fout<<str<<endl;
}
if(0==strcmp(str1.c_str(),str.c_str()) )
{
int m=1;
if(m)
{
ofstream fout(m_FileName,ios::app);
fout<<"<div>"<<endl;
fout<<"<h2>全部新闻</h2>"<<endl;
fout.close();
goto here;
}

}
}

here: fin.close();
}
fout<<strTitle<<"-"<<strClass<<" - "<<strNum<<" <br/>"<<endl;
fout.close();
m_pRecordset->MoveNext();

}






我是希望这个程序从数据库读数据,每20条数据库记录产生一个页面文件,
if(i_Count==1),把一个已有文件内容输入产生的页面文件。可调式的时候,
if(i_Count==1),下面的代码只执行一次不知道为什么?明明i_Count==1了,可他就是不执行下面的代码
请大家帮我看一看什么问题
...全文
61 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
feixiangdeyu12 2009-09-04
  • 打赏
  • 举报
回复
这个问题解决了,我是把文件给关闭了,所以写不进去了。
我的代码:
while(!m_pRecordset->adoEOF)
{
i_Count++;
n++;
var = m_pRecordset->GetCollect("title");
if(var.vt != VT_NULL)
strTitle = _com_util::ConvertBSTRToString((_bstr_t)var);

var = m_pRecordset->GetCollect("class");
if(var.vt != VT_NULL)
strClass = _com_util::ConvertBSTRToString((_bstr_t)var);

var = m_pRecordset->GetCollect("num");
if(var.vt != VT_NULL)
strNum = _com_util::ConvertBSTRToString((_bstr_t)var);

if (i_Count>=20)
{
i_FileCount++;
i_Count=0;

m_FileName.Format("indexq[%d].html",i_FileCount);
DeleteFile(m_FileName);
}
m_FileName.Format("indexq[%d].html",i_FileCount);
ofstream fout(m_FileName,ios::app);

string str,str1;
str1=" <p><a href=\"/index.htm\">首页</a> / 新闻</p>";

if(0==i_Count)
{
ifstream fin("index.html");
while (getline (fin, str) )
{
ofstream fout(m_FileName,ios::app);
if(fout)
{
fout<<str<<endl;
}
if(0==strcmp(str1.c_str(),str.c_str()) )
{
int m=1;
if(m)
{
ofstream fout(m_FileName,ios::app);
fout<<"<div>"<<endl;
fout<<"<h2>全部新闻</h2>"<<endl;
fout.close();
fin.close();

goto here;
}

}

}


}
here: ;
fout<<strTitle<<"-"<<strClass<<" - "<<strNum<<" <br/>"<<endl;

fout.close();
m_pRecordset->MoveNext();

}


那我现在想在每个文件的数据库的20条记录写完后,再从一个已有文件里向每个文件添加相同的内容,应该怎么写?大家帮我想想
feilinhe 2009-09-04
  • 打赏
  • 举报
回复
第一次进这个函数体就不执行吗?,看代码是可以执行的
不过你有个这个
if (i_Count>=20)
{
i_FileCount++;
i_Count=0;
}
像LS说的debug单步调试看看吧
fandh 2009-09-04
  • 打赏
  • 举报
回复
建议设置断点,一步一步调试一下!有可能已经执行了!
feixiangdeyu12 2009-09-04
  • 打赏
  • 举报
回复
啊,解决了,我刚才代码问题就是忘了写 m_pRecordset->MoveNext(); 了,加上就好了,实在很感谢你
simghost 2009-09-04
  • 打赏
  • 举报
回复
那你是不是应该考虑一下生成批量文件的时候写入所需数据?
feixiangdeyu12 2009-09-04
  • 打赏
  • 举报
回复
这样写就有问题,我就不知道怎么办了
feixiangdeyu12 2009-09-04
  • 打赏
  • 举报
回复
我看你写的代码主要是讲把数据写到文件里,可我现在主要是想怎么一次性的把同一文件数据输到产生的一批文件中,我不知道一次产生多少文件那,代码如下:
void CHtmlDlg::OnAll()
{
_variant_t var;
int i_Count=-1;

CString m_FileName;
ifstream fin("index.html");
char * strClass,* strNum,* strTitle;

try
{
if(!m_pRecordset->BOF)
m_pRecordset->MoveFirst();
else
{
AfxMessageBox("表内数据为空");
return;
}
m_pRecordset->MoveFirst();
int i_Count=-1;
int i_FileCount=0;
DeleteFile("indexq[0].html");

while(!m_pRecordset->adoEOF)
{
i_Count++;

var = m_pRecordset->GetCollect("title");
if(var.vt != VT_NULL)
strTitle = _com_util::ConvertBSTRToString((_bstr_t)var);

var = m_pRecordset->GetCollect("class");
if(var.vt != VT_NULL)
strClass = _com_util::ConvertBSTRToString((_bstr_t)var);

var = m_pRecordset->GetCollect("num");
if(var.vt != VT_NULL)
strNum = _com_util::ConvertBSTRToString((_bstr_t)var);

if (i_Count>=20)
{
i_FileCount++;
i_Count=0;

m_FileName.Format("indexq[%d].html",i_FileCount);
DeleteFile(m_FileName);
}
m_FileName.Format("indexq[%d].html",i_FileCount);
ofstream fout(m_FileName,ios::app);

string str,str1;
str1=" <p><a href=\"/index.htm\">首页</a> / 新闻</p>";

if(0==i_Count)
{
ifstream fin("index.html");
while (getline (fin, str) )
{
ofstream fout(m_FileName,ios::app);
if(fout)
{
fout<<str<<endl;
}
if(0==strcmp(str1.c_str(),str.c_str()) )
{
int m=1;
if(m)
{
ofstream fout(m_FileName,ios::app);
fout<<"<div>"<<endl;
fout<<"<h2>全部新闻</h2>"<<endl;
fout<<" <h5>共有"<<m_pRecordset->GetRecordCount()<<"条新闻"<<endl;
fout<<" 第"<<i_FileCount+1<<"页"<<endl;
int n,m,i=1;
n=(m_pRecordset->GetRecordCount())/20;
m=(m_pRecordset->GetRecordCount())%20;
if(m!=0)
{
n=n+1;
}

while(n)
{
n--;
fout<<i<<endl;
i++;
}
fout<<" </h2>"<<endl;
fout.close();
fin.close();

goto here;
}

}

}


}
here: ;
fout<<strTitle<<"-"<<strClass<<" - "<<strNum<<" <br/>"<<endl;

fout.close();
m_pRecordset->MoveNext();

}



m_pRecordset->MoveFirst();
i_Count=-1;
while(!m_pRecordset->adoEOF)
{
i_Count++;
if (i_Count>=20)
{
i_FileCount++;
i_Count=0;
}
m_FileName.Format("indexq[%d].html",i_FileCount);
ofstream fout(m_FileName,ios::app);
ifstream fin("index.html");
if(0==i_Count)
{
fout<<"</div>"<<endl;

string str1,str2;
str2="<div id=\"table_top_GD_r\" class=\"full-captop_r\" style=\"height:620px\">";
while(1)
{
getline(fin,str1);
if(0==strcmp(str1.c_str(),str2.c_str()))
break;

}
while (getline (fin, str1) )
{
ofstream fout(m_FileName,ios::app);
if(fout)
{
fout<<str1<<endl;
fout.close();
}
}



fout.close();
fin.close();


}

}












}
catch(_com_error *e)
{
AfxMessageBox(e->ErrorMessage());
}







}
simghost 2009-09-04
  • 打赏
  • 举报
回复
CStdioFile file1,file2,out;
file1.Open(m_Path1,CFile::modeRead);
out.Open("tmp.tmp",CFile::modeCreate|CFile::modeWrite);
CString strTemp;
while (file1.ReadString(strTemp))
{
if (strTemp == ""/*应该插入时的匹配字符串*/)
{
file1.Open(m_Path2,CFile::modeRead);
CString strTemp2;
strTemp="";
while (file2.ReadString(strTemp2))
{
strTemp+=strTemp2+_T("\n");
}
file2.Close();
}
out.WriteString(strTemp);
}
file1.Close();
DeleteFile(m_Path1);
out.Close();
out.Rename("tmp.tmp",m_Path1);

16,551

社区成员

发帖
与我相关
我的任务
社区描述
VC/MFC相关问题讨论
社区管理员
  • 基础类社区
  • Creator Browser
  • encoderlee
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

        VC/MFC社区版块或许是CSDN最“古老”的版块了,记忆之中,与CSDN的年龄几乎差不多。随着时间的推移,MFC技术渐渐的偏离了开发主流,若干年之后的今天,当我们面对着微软的这个经典之笔,内心充满着敬意,那些曾经的记忆,可以说代表着二十年前曾经的辉煌……
        向经典致敬,或许是老一代程序员内心里面难以释怀的感受。互联网大行其道的今天,我们期待着MFC技术能够恢复其曾经的辉煌,或许这个期待会永远成为一种“梦想”,或许一切皆有可能……
        我们希望这个版块可以很好的适配Web时代,期待更好的互联网技术能够使得MFC技术框架得以重现活力,……

试试用AI创作助手写篇文章吧