有一个TXT文件,每一行是一条记录,记录的每一个字段之间用tab隔开
用ReadString可以把一行读入一个缓冲区,我怎么把单个字段的值读出来呢?
将每行读入CString中,再用CString的Find进入拆分int nPos = strBufer.Find( "\t ",0);循环提取,结束后再处理文件的下一行。
cstdiofile file;
CString str; while(file.readstring(str)){ int i= 0,j=0;
Cstring temp = _T( " ");
while(j!=-1){ j = str.find( "\t ");
if(j == -1) break; temp += str.left(j); str = str.right(str.getlength()-j);
} //每一行处理完的string自己找地方保存temp = " ";
}
举个例子,有一文本文件test.txt如下:
192.168.1.1:1,60.000,60.000
192.168.1.1:2,60.000,60.000
192.168.1.1:3,60.000,60.000
192.168.1.1:5,60.000,60.000
192.168.1.1:21,60.000,60.000
192.168.1.1:34,60.000,60.000
192.168.1.1:35,60.000,60.000
192.168.1.1:66,60.000,60.000
192.168.1.1:123,60.000,60.000
192.168.1.1:234,60.000,60.000
我想找到192.168.1.1:21这行,并将其后面的值改为40.000,40.000,即这行值变为“192.168.1.1:21,40.000,40.000”
如何能够只在一个文件里操作,不用写第二个文件?
用CStdioFile.ReadString()和CStdioFile.WriteString()能够实现吗?借助CFile.Seek()可不可以?
CStdioFile file; file.Open("a.txt",CFile::modeReadWrite); CString str; while(true){ file.ReadString(str); if(str.Left(str.Find(",",0))==CString("192.168.1.1:21")){ file.Seek(-2-str.GetLength(),CFile::current); //新建文本文档 //file.Seek(0,CFile::current); //file.Write("192.168.1.1:21,40.000,40.000",strlen("192.168.1.1:21,40.000,40.000")); file.Write("192.168.1.1:21,40.000,40.000\n",strlen("192.168.1.1:21,40.000,40.000")); break; } } file.Close();