void insert() //中間插入函數(shù){ int line_index;char c[100]="this is a text!"; char temp_str[100];printf("請輸入你的內(nèi)容的行位置:");scanf("%d",&line_index);//char line[100];//printf("請輸入數(shù)據(jù)\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))//將寫位置以后的所有數(shù)據(jù)存入緩存文件{fwrite((void *)temp_str,sizeof(char),1,fp2);}seekline(line_index,fp);//重定位源文件printf("%d\n",ftell(fp));fwrite(c,strlen(c),1,fp);//把數(shù)據(jù)寫入源文件printf("%d\n",ftell(fp));fseek(fp2,0,0);//把緩存文件定位到頭部while(fread((void *)temp_str,sizeof(char),1,fp2))//把緩存的數(shù)據(jù)寫回源文件{fwrite((void *)temp_str,sizeof(char),1,fp);} printf("%d",strlen(c));}void seekline(int line,FILE *fp) /****表示定位到line行的第col個(gè)字符,fp是文件指針***/{char temp[100];//數(shù)據(jù)緩沖int n=1;fseek(fp,0,SEEK_SET);//將文件指針移到頭部do{if(line==n)//當(dāng)計(jì)數(shù)器與所需行號相同時(shí),退出循環(huán),說明行號找到{break;}n++;//行計(jì)數(shù)器加1}while(fgets(temp,100,fp));//從文件讀取一行數(shù)據(jù),保存在tmp中,在這里假定一行數(shù)據(jù)最大有100個(gè)字符fseek(fp,0,SEEK_CUR);//將文件指針移到第col個(gè)字符}
關(guān)于C語言fwrite()函數(shù)總是寫不進(jìn)文件,下面是我的函數(shù),請問該怎么解決?
函數(shù)式編程
2021-10-20 18:15:03