修改文件头内容:
通过使用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个字符,,,
时间: 2024-11-29 01:41:27