如何在MFC中编辑文本文件 [英] How to editing a text file in MFC

查看:109
本文介绍了如何在MFC中编辑文本文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


我的文件有数字和名字。在我的对话框中有编辑框,它们收到了数字和名字。现在我想当按钮添加时点击如果editBoxes中的信息是新的,那么在最后添加它其他文件(数字已存在于文件中)找到该行并替换名称。

我知道如何添加新信息但是对于编辑文件我需要帮助。



我写一个代码在文件中逐行搜索并将每个单词保存在一个数组的房间但是没有没有如何替换它

如果你可以请帮助我要完成这个鳕鱼(如果是真的)其他请以新的方式帮助我



提前谢谢你我们的工作:



m_status是我的CStdioFile,m_Number是第一个editBox,m_Name是第二个editBox



Hi I have file that have numbers and names.in my dialog there are editBoxes that received numbers and names.now I want to when button Add clicked if the information in editBoxes are new so add it at the end of the file else(number has already exist in file) find the line and replace the name.
I know how to add new information but for editing file I need help.

I write a code to search in file line by line and save each word in a room of array but don't no how replace it
if u can please help me to complete this cod(if it is true) else please help me in new way

thank u in advance :)

m_status is my CStdioFile and m_Number is the first editBox and m_Name is second editBox

    CString str6;
CString str7;
CString strToken;
CString x[2];
int length=0;
int nTokenPos = 0;
int xcounter=0;

m_status.Open("E:\\3.txt",CStdioFile::modeReadWrite); //open file
while(m_status.ReadString(str6))
{
    length++;   //get the number of line in file
}
m_status.SeekToBegin();

for(int i=0;i<length;i++)
{
    m_status.ReadString(str6);
    str7 = str6;
    strToken = str7.Tokenize(_T(" "),nTokenPos); //each word between spaces save in
                                                         //strToken
    while (!strToken.IsEmpty())
    {
         x[xcounter] = strToken;  //save each word in array
         xcounter++;
         strToken = str7.Tokenize(_T(" "), nTokenPos);
    }
    if (x[1]==m_Number)
    {
        x[2]=m_Name;
        //here I want if number in file was same as number in editBox1 so change
                    //the name in file to name in editbox2
    }
}
m_status.Close();

UpdateData(FALSE);

推荐答案

你不能像编写代码那样去做,因为,如果新数据不完全相同作为原始大小你将最终得到一个损坏的文件。您应该将所有数据读入集合,然后修改其中的数据。完成所有更新后,您将重写整个文件。
You cannot do it the way your code is written, because, if the new data is not exactly the same size as the original you will end up with a corrupt file. You should read all the data into a collection, then modify the data in there. When all updates are complete you rewrite the entire file.


这篇关于如何在MFC中编辑文本文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆