void insert() //中間插入函數{ int line_index;char c[100]="this is a text!"; char temp_str[100];printf("請輸入你的內容的行位置:");scanf("%d",&line_index);//char line[100];//printf("請輸入數據\n");//scanf("%s",c);if((fp=fopen("you.txt","rt+"))==NULL)//打開要添加的文件{printf("\n打開a1.txt失敗!");}if((fp2=fopen("temp.txt","w+"))==NULL)//打開緩存文件{printf("\n打開a2.txt失敗!");}seekline(line_index,fp);while(fread((void *)temp_str,sizeof(char),1,fp))//將寫位置以后的所有數據存入緩存文件{fwrite((void *)temp_str,sizeof(char),1,fp2);}seekline(line_index,fp);//重定位源文件printf("%d\n",ftell(fp));fwrite(c,strlen(c),1,fp);//把數據寫入源文件printf("%d\n",ftell(fp));fseek(fp2,0,0);//把緩存文件定位到頭部while(fread((void *)temp_str,sizeof(char),1,fp2))//把緩存的數據寫回源文件{fwrite((void *)temp_str,sizeof(char),1,fp);} printf("%d",strlen(c));}void seekline(int line,FILE *fp) /****表示定位到line行的第col個字符,fp是文件指針***/{char temp[100];//數據緩沖int n=1;fseek(fp,0,SEEK_SET);//將文件指針移到頭部do{if(line==n)//當計數器與所需行號相同時,退出循環,說明行號找到{break;}n++;//行計數器加1}while(fgets(temp,100,fp));//從文件讀取一行數據,保存在tmp中,在這里假定一行數據最大有100個字符fseek(fp,0,SEEK_CUR);//將文件指針移到第col個字符}
這是我的函數,但是fwrite()函數總是寫不進文件
慕桂英3389331
2023-03-05 17:13:32