关于CFile类的几点疑问,请高手随手赐教,该怎么解决
关于CFile类的几点疑问,请高手随手赐教
CString str;
str=str_room+" ";
str=str+str_ip+" "+"yes";
CFile file("IP_ROOM.txt",CFile::modeCreate|CFile::modeWrite);
file.SeekToEnd();
file.Write(str,strlen(str));
file.Close();
我要实现的是每次添加信息,是在原来文件中所添加信息的下面;
例如第一次文本中是“1#101 111.111.111.111”
第二次想添加“2#202 222.222.222.222”
希望文本中此时的内容是:
1#101 111.111.111.111
2#202 222.222.222.222
但每次文本内容都是从头开始替换掉了原来的内容;
还有如果我要添加1#101 111.111.111.111,如何判断文本文件中是不是已经存在了1#101 111.111.111.111
------解决方案--------------------
CFile::modeCreate Directs the constructor to create a new file. If the file exists already, it is truncated to 0 length
CFile::modeNoTruncate Combine this value with modeCreate. If the file being created already exists, it is not truncated to 0 length. Thus the file is guaranteed to open, either as a newly created file or as an existing file. This might be useful, for example, when opening a settings file that may or may not exist already. This option applies to CStdioFile as well.
写的时候应该加上CFile::modeNoTruncate
判断是否存在就先读出来再判断
------解决方案--------------------
用CFile::modeCreate | CFile::modeNoTruncate
CFile::modeCreate|CFile::modeWrite相当于每次都删除了IP_ROOM.txt,然后重新创建了IP_ROOM.txt。
Read()后保存在一个CString里面,然后用CString.Find查找是否有指定的字字符串。
CString str;
str=str_room+" ";
str=str+str_ip+" "+"yes";
CFile file("IP_ROOM.txt",CFile::modeCreate|CFile::modeWrite);
file.SeekToEnd();
file.Write(str,strlen(str));
file.Close();
我要实现的是每次添加信息,是在原来文件中所添加信息的下面;
例如第一次文本中是“1#101 111.111.111.111”
第二次想添加“2#202 222.222.222.222”
希望文本中此时的内容是:
1#101 111.111.111.111
2#202 222.222.222.222
但每次文本内容都是从头开始替换掉了原来的内容;
还有如果我要添加1#101 111.111.111.111,如何判断文本文件中是不是已经存在了1#101 111.111.111.111
------解决方案--------------------
CFile::modeCreate Directs the constructor to create a new file. If the file exists already, it is truncated to 0 length
CFile::modeNoTruncate Combine this value with modeCreate. If the file being created already exists, it is not truncated to 0 length. Thus the file is guaranteed to open, either as a newly created file or as an existing file. This might be useful, for example, when opening a settings file that may or may not exist already. This option applies to CStdioFile as well.
写的时候应该加上CFile::modeNoTruncate
判断是否存在就先读出来再判断
------解决方案--------------------
用CFile::modeCreate | CFile::modeNoTruncate
CFile::modeCreate|CFile::modeWrite相当于每次都删除了IP_ROOM.txt,然后重新创建了IP_ROOM.txt。
Read()后保存在一个CString里面,然后用CString.Find查找是否有指定的字字符串。