首页 > 其他分享 >关于上一个fprintf覆写问题进行了实验探究

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

时间:2022-12-25 03:22:23浏览次数:59  
标签:fp return students 覆写 探究 fprintf str printf

问题见:关于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........
*/

标签:fp,return,students,覆写,探究,fprintf,str,printf
From: https://www.cnblogs.com/faf4r/p/17003619.html

相关文章

  • 《探究游戏设计中VR技术的优势与实践应用》读后随笔
    VR技术是结合计算机应用、多媒体、信息技术和仿真技术等综合在一起的应用型技术,在广告、体育、游戏设计等多个领域都有着广阔的市场发展空间。目前在美国和欧洲等发达国家......
  • 探究map为什么不能遍历的同时进行增删操作
    持续创作,加速成长!这是我参与「掘金日新计划·6月更文挑战」的第14天,点击查看活动详情前段时间,同事在代码中KW扫描的时候出现这样一条:上面出现这样的原因是在使用fore......
  • 【并发技术系列】「多线程并发编程」技术体系和并发模型的基础探究(夯实基础)
    让我们通过本篇文章一同进入并发编程技术的世界里面,相信通过这篇文文章一定会对话你的并发技术体系有一定帮助以及夯实你的基础功底。基本概念并发concurrency并行paralleli......
  • SpringBoot2.x系列教程09--新纪元之SpringBoot原理探究(重点)
    SpringBoot系列教程09--新纪元之SpringBoot原理探究(重点)作者:一一哥一.SpringBoot工作原理概述Springboot应用程序采用各种Starters启动器,入口类是包含​​@SpringBootA......
  • 值类型和引用类型的赋值及深拷贝探究
    本文主要讲述的是值类型和引用类型的赋值及深拷贝对值类型和引用类型的定义以及由来不清晰的可以看我之前的随笔,链接如下:https://www.cnblogs.com/ShawBlack/p/16997772.......
  • Clickhouse表引擎探究-ReplacingMergeTree
    作者:耿宏宇1表引擎简述1.1官方描述MergeTree系列的引擎被设计用于插入极大量的数据到一张表当中。数据可以以数据片段的形式一个接着一个的快速写入,数据片段在后台按......
  • Hessian探究(一)Hessian入门示例
    一、hessian的maven信息:[html] ​​viewplain​​ ​​copy​​ ​​print​​​?​<dependency>  <groupId>com.caucho</groupId>  <artifac......
  • fprintf()\fscanf()\sprintf() 函数的灵活应用
     fprintf函数和fscanf函数调用方式:fprintf(文件结构指针,格式字符串,输出表列)fscanf(文件结构指针,格式字符串,输出表列) voidmain(){   FILE*pWrite,*pRead; ......
  • 探究:普通人都是怎么入门编程
    目录前景提要研究思考1.术语才是编程入门的领路人2.马上着手胜于做个目标,写个计划3.动起手来,比用眼睛看更有作用4.开发语言林立,你的武道何去何从总结前景提要很多人......
  • Kafka数据可靠性探究
    概述Kafka作为商业级消息中间件,消息的可靠性保障是非常重要。那Kafka是怎么保障消息的可靠性的呢?上图是Kafka的消息发送基础架构,一条消息的完整生命周期是:生产者发送消息至K......