关于上一个fprintf覆写问题进行了实验探究

问题见:关于C语言fprinf的一个问题

从头开始fprintf覆写是可以的

#include <stdio.h>

int main(){
    char *str = "STUDENTS";
    FILE *fp;
    if ((fp=fopen("d.txt", "r+")) == NULL){
        printf("\a打开文件失败!\n");
        return 1;
    }

    printf("%ld",ftell(fp)); //输出为0
    fprintf(fp, "%s",str); //正常覆写

    fclose(fp);
    return 0;
}

/* d.txt
We are students. All the students study very hard.
So, these students are outstanding students.
 */

/* result  //正常覆写
STUDENTStudents. All the students study very hard.
So, these students are outstanding students.
*/

fgetc移动光标后就没有任何反应

#include <stdio.h>

int main(){
    char *str = "STUDENTS";
    FILE *fp;
    if ((fp=fopen("d.txt", "r+")) == NULL){
        printf("\a打开文件失败!\n");
        return 1;
    }

    fgetc(fp);fgetc(fp);
    fgetc(fp);fgetc(fp); //加了这个怎么就没反应了啊,覆写都不了???
    printf("%ld",ftell(fp)); //输出为4
    fprintf(fp, "%s",str); //文件没有任何变动
    
    
    fclose(fp);
    return 0;
}

/* d.txt
We are students. All the students study very hard.
So, these students are outstanding students.
 */

/* result  //文件没有任何变动
We are students. All the students study very hard.
So, these students are outstanding students.
*/

用fseek移位后可以正常覆写

#include <stdio.h>

int main(){
    char *str = "STUDENTS";
    FILE *fp;
    if ((fp=fopen("d.txt", "r+")) == NULL){
        printf("\a打开文件失败!\n");
        return 1;
    }

    printf("%ld",ftell(fp)); //输出为0
    fseek(fp, 4, SEEK_SET);
    printf("%ld",ftell(fp)); //输出为4
    fprintf(fp, "%s",str); //正常覆写
    
    fclose(fp);
    return 0;
}

/* d.txt
We are students. All the students study very hard.
So, these students are outstanding students.
 */

/* result  //移位后正常覆写
We aSTUDENTSnts. All the students study very hard.
So, these students are outstanding students.
*/

fgetc移位加上fseek也能正常覆写

#include <stdio.h>

int main(){
    char *str = "STUDENTS";
    FILE *fp;
    if ((fp=fopen("d.txt", "r+")) == NULL){
        printf("\a打开文件失败!\n");
        return 1;
    }

    fgetc(fp);fgetc(fp);
    fgetc(fp);fgetc(fp); //加了这个怎么就没反应了啊,覆写都不了???
    printf("%ld",ftell(fp)); //输出为4
    fseek(fp, ftell(fp), SEEK_SET); //用ftell保证原位
    printf("%ld",ftell(fp)); //输出为4
    fprintf(fp, "%s",str); //正常覆写
    
    fclose(fp);
    return 0;
}

/* d.txt
We are students. All the students study very hard.
So, these students are outstanding students.
 */

/* result  //移位后正常覆写
We aSTUDENTSnts. All the students study very hard.
So, these students are outstanding students.
*/

但将fseek的手法拿到之前的程序还是照常错误。迷惑???

#include <stdio.h>

int match(FILE *fp, char *str);
void upper(FILE *fp, char *str);

int main(){
    //获取输入
    char path[81]={0}, str[32]={0};
    int check;
    printf("请输入文件名:");
    check = scanf_s("%s", path, 81);
    if (check==0){
        printf("输入的文件名过长!\n");
        return 1;
    }
    printf("请输入英文字符串:");
    check = scanf_s("%s", str, 32);
    if (check==0){
        printf("输入的英文字符串过长!\n");
        return 1;
    }
    //统一将字符串str大写化
    char *c = str;
    while (*c != '\0')
    {
        if (*c>='a' && *c<='z')
            *c -= 32;
        c++;
    }
    
    //打开文件
    FILE *fp;
    if ((fp=fopen(path, "r+")) == NULL){
        printf("打开文件失败!\n");
        return 1;
    }

    //读取文件并查找替换匹配的字符
    do {
        if (match(fp, str)){
            fseek(fp, ftell(fp), SEEK_SET); //补一个这个也不行,具体为什么不知道
            fprintf(fp, "%s", str);
            continue; //已经移位了
        }

        fgetc(fp); //让光标移位到下一个
    } while (!feof(fp));
    
    fclose(fp);
    return 0;
}

int match(FILE *fp, char *str){
    int current = ftell(fp);
    char *q=str, p=fgetc(fp);
    int ret = -1; //默认-1用于判断是否被修改
    while (*q!='\0' && p!=EOF)
    {
        p = p>='a'&&p<='z' ? p-32 : p;
        if (*q != p) ret = 0; //在str或fp没结束就不匹配了
        q++;
        p = fgetc(fp);
    }
    if (*q=='\0' && ret==-1) ret = 1; //str结束前都是匹配的,所以匹配
    else ret = 0; //否则是前面已判断或fp结束,不全匹配
    //光标归位
    fseek(fp, current, SEEK_SET);
    return ret;
}

/* d.txt
We are students. All the students study very hard.
So, these students are outstanding students.
 */

//输入d.txt\nstudents
/* result  //老问题
We are STUDENTSs. All th STUDENTSs study vry hard.
So, these STUDENTSs are outtanding STUDENTSs........
*/
posted @ 2022-12-25 03:08  faf4r  阅读(36)  评论(0编辑  收藏  举报