类型:转载 责任编辑:asp.net 日期:2007/05/23
热门软件下载:
我先用fstream open打开了一个文件,然后用seek定位到相应位置,问题是现在不知道怎么替换,如果用cout<<的话会破坏文件中其他不需要替换的内容?谢谢了
网友回答:
看帮助里的
TReplaceFlags replaceflags;
replaceflags << rfReplaceAll;
StringReplace
可以替代指定的内容
如
StringReplace(TmpStr1,"[总记录数]",IntToStr(jlcount),replaceflags);
把tmpstr1中的"[总记录数]"用IntToStr(jlcount)替换
//写了一个很简单的例子,只修改了文件中的一个字节,你可以自己改一下。
void __fastcall TForm1::Button1Click(TObject *Sender)
{
int iFileHandle;
int Length = 1;
char *Buffer;
AnsiString DataFile = "D:\\a.txt";
if (FileExists(DataFile))
{
iFileHandle = FileOpen(DataFile, fmOpenReadWrite);
}
if (iFileHandle)
{
Buffer = new char[1];
FileSeek(iFileHandle, 5, 0);
FileRead(iFileHandle, Buffer, Length);
*Buffer = a;
FileWrite(iFileHandle, Buffer, Length);
FileClose(iFileHandle);
delete Buffer;
}
}
以下是删除文件中某行
CStringArray strarray;
CStdioFile file;
if( !file.Open("c:\\1.txt", CFile::modeRead) )
{
AfxMessageBox("can not open file!");
return;
}
CString strLine;
while(file.ReadString(strLine))
{
strarray.Add(strLine);
}
file.Close();
CString str;
int nCount=strarray.GetSize();
for(int i=0; i<nCount; i++)
{
str = strarray[i];
if( str.Find("key") != -1)
{
strarray.RemoveAt(i);//删除
nCount--;
i--;
//这里修改
int nIndex = str.Find(f);
if(nIndex!=-1)
{
CString str1 = str.Right(str.GetLength()-nIndex-1);
}
}
}
if( !file.Open("c:\\1.txt", CFile::modeCreate|CFile::modeWrite) )
{
AfxMessageBox("can not open file!");
return;
}
for(i=0; i<strarray.GetSize(); i++)
{
str = "";
str = strarray[i] + "\n";
file.WriteString(str);
}
file.Close();