修改文件内容

修改文件头内容:

  通过使用rb+模式,代码如下:

  

void CfileTestDlg::OnBnClickedButton1()
{

    struct student
    {
        char name [96];
        int   nId ;
        char  desc[1024];

    };

    FILE *fp = fopen("D:\\20080507.log ", "rb+");

    if (fp==0)
    {
        printf("can't open file\n");
        return ;
    }

    char szBuf[4096];
    memset (szBuf,0,sizeof(szBuf));
    struct student   stud;
    stud.nId  = 96;
    memcpy(stud.name,"iqaeuqioweuqeruurururuu89999999999999",strlen("iqaeuqioweuqeruurururuu8888888888"));

    memcpy(szBuf, &stud, sizeof(student));

    fseek(fp, 0, SEEK_SET);

    fwrite(szBuf, 4096, 1, fp);
    fflush(fp);
    fclose(fp);
    return ;
}

 

void CfileTestDlg::OnBnClickedButton2()
{
    // TODO: Add your control notification handler code here
    struct student
    {
        char name [96];
        int   nId ;
        char  desc[1024];

    };


    FILE *fp = fopen("D:\\20080507.log ", "a+");

    if (fp==0)
    {
        printf("can't open file\n");
        return ;
    }

    char szBuf[4096];
    memset (szBuf,0,sizeof(szBuf));
    struct student   stud;
    stud.nId  = 96;
    memcpy(stud.name,"iqaeuqioweuqeruurururuu8888888888",strlen("iqaeuqioweuqeruurururuu8888888888"));

    memcpy(szBuf, &stud, sizeof(student));

    fseek(fp, 0, SEEK_END);

    fwrite(szBuf, 4096, 1, fp);

    fwrite(szBuf, 4096, 1, fp);

    fflush(fp);
    fclose(fp);
    return ;
}

  函数OnBnClickedButton2:往文件中写入内容

  函数OnBnClickedButton1,修改文件前4096个字符,,,

posted @ 2016-05-09 11:20  风狼啸天  阅读(211)  评论(0编辑  收藏  举报