C语言文本操作fwrite,如和对一个字节反复修改???
#define HisALLFile "/flash/hislog.txt"/***目的:对文件hislog.txt的某个指定位置进行字节改写**测试:对hislog.txt文件的0位置反复写入'1'和 '2'*****/void testtxtwrite(void){ FILE *fp=NULL; int i=0; size_t n=0; long sek=0; unsigned char temp='1'; unsigned char temp1='2'; unsigned char temp3=0; fp=fopen(HisALLFile, "ab");//不知此处应该已何种方式打开文件?? if (fp == NULL) return -1; for (i=0; i<10; i++) { fseek(fp, 0, SEEK_SET);//移动文件读取位置到 从开始处的0偏移 sek=0; sek=ftell(fp);//打印当前读取位置 printf("sek1=0x%lx\n", sek); fwrite(&temp, 1, 1, fp); sek=0; sek=ftell(fp);//取得执行写操作后的文件读取位置 printf("T1: sek=0x%lx Da=0x%x\n", sek, temp);//打印当前读取位置,及写入的数据 fseek(fp, 0, SEEK_SET);//移动文件读取位置到 从开始处的0偏移 sek=0; sek=ftell(fp); printf("sek2=0x%lx\n", sek); temp3=0; fread(&temp3, 1, 1, fp); sek=0; sek=ftell(fp); printf("T2: sek=0x%lx Da=0x%x\n", sek, temp3);//打印当前读取位置,及读取的数据 fseek(fp, 0, SEEK_SET); sek=0; sek=ftell(fp); printf("sek3=0x%lx\n", sek); fwrite(&temp1, 1, 1, fp); sek=0; sek=ftell(fp); printf("T3: sek=0x%lx Da=0x%x\n", sek, temp1); fseek(fp, 0, SEEK_SET); sek=0; sek=ftell(fp); printf("sek4=0x%lx\n", sek); temp3=0; fread(&temp3, 1, 1, fp); sek=0; sek=ftell(fp); printf("T4: sek=0x%lx Da=0x%x\n", sek, temp3); printf("\n\n"); } fclose(fp); }