请教高手,小弟写一段代码.
把1.txt文件的数据写入2.txt文件中,也需要一行挨一行的写,可是在2.txt中只能写入1.txt的最后一行,请高手指点.
代码如下,void CMy1Dlg::OnButton1()
{
int nLineCount=0;
//nLineCount是行数,初始化为1
CString str;
char buffer[256];
//buffer数组用来存储每行的数据
CStdioFile myFile("e:/vc/1.txt",CFile::typeText |CFile::modeRead);
while (myFile.ReadString(buffer,256)!=0)
{
CStdioFile file1( "e:/vc/2.txt",CFile::modeCreate|CFile::modeWrite);
nLineCount++;
str=buffer;
file1.WriteString(str);
}}
把1.txt文件的数据写入2.txt文件中,也需要一行挨一行的写,可是在2.txt中只能写入1.txt的最后一行,请高手指点.
代码如下,void CMy1Dlg::OnButton1()
{
int nLineCount=0;
//nLineCount是行数,初始化为1
CString str;
char buffer[256];
//buffer数组用来存储每行的数据
CStdioFile myFile("e:/vc/1.txt",CFile::typeText |CFile::modeRead);
while (myFile.ReadString(buffer,256)!=0)
{
CStdioFile file1( "e:/vc/2.txt",CFile::modeCreate|CFile::modeWrite);
nLineCount++;
str=buffer;
file1.WriteString(str);
}}
这一句应该提到循环外面取,
"e:/vc/2.txt"因该写成"e:\\vc\\2.txt"吧!
CStdioFile::ReadString(str);//不用指定缓冲区大小
file.WriteString("Hello World!");