Lesson26 PoEdu培训第二课 C语言篇(18) 文件处理函数 随堂作业
文章类别: 培训作业 0 评论

Lesson26 PoEdu培训第二课 C语言篇(18) 文件处理函数 随堂作业

文章类别: 培训作业 0 评论

老师未批改

在文本模式下, 以f系函数(fgets, fputs...)完成如下作业, 不能使用标准输入输出流函数.

1. 让用户输入文件名(.txt结尾), 按文件的目录建立当前文件名的文档, 随后让用户输入信息, 每次只保存本次信息.

程序源代码:

/*************************************************************************
    > 文件名: lesson26.hw.01.c
    > 作者: 花心胡萝卜
    > 邮箱: hxhlb@hxcarrot.com 
    > 创建时间: 2016-10-12 21:01:06
 ************************************************************************/

#include <stdio.h>
#include <string.h>

#define LEN 255
#define TRUE 1

int deleteLastCrLf(char* str) {
    if (*(str + strlen(str) - 1) == '\n') {
        *(str + strlen(str) - 1) = '\0';
    }
    return 0;
}

int main() {
    
    fputs("请输入文件名(.txt结尾):\n", stdout);
    char strFileName[LEN] = { 0 };
    char strInput[LEN] = { 0 };
    fgets(strFileName, LEN, stdin);
    deleteLastCrLf(strFileName);

    FILE* fp = fopen(strFileName, "w+");
    if (fp) {
        do {
            fputs("请输入新文件内容(输入#退出..): ", stdout);
            memset(strInput, 0x00, sizeof(strInput));
            fgets(strInput, LEN, stdin);
            if (strcmp("#\n", strInput) != 0) {
                fputs(strInput, fp);
            } else {
                break;
            }
        } while (TRUE);
        fclose(fp);
    } else {
        fputs("创建文件失败!程序退出...\n", stdout);
    }
    return 0;
}

运行效果:
Alt 运行效果

2. 让用户输入文件名(.txt结尾), 如果文件不存在就建立该文件, 如果文件存在则覆盖写入之后用户输入的信息.

程序源代码:

/*************************************************************************
    > 文件名: lesson26.hw.02.c
    > 作者: 花心胡萝卜
    > 邮箱: hxhlb@hxcarrot.com 
    > 创建时间: 2016-10-12 21:27:02
 ************************************************************************/

#include <stdio.h>
#include <string.h>

#define LEN 255
#define TRUE 1

int deleteLastCrLf(char* str) {
    if (*(str + strlen(str) - 1) == '\n') {
        *(str + strlen(str) - 1) = '\0';
    }
    return 0;
}

int writeFile(FILE* fp) {
    char strInput[LEN] = { 0 };    
    do {
        fputs("请输入新文件内容(输入#退出..): ", stdout);
        memset(strInput, 0x00, sizeof(strInput));
        fgets(strInput, LEN, stdin);
        if (strcmp("#\n", strInput) != 0) {
            fputs(strInput, fp);
        } else {
            break;
        }
    } while (TRUE);
    fclose(fp);
    return 0;
}

int main() {
    
    fputs("请输入文件名(.txt结尾):\n", stdout);
    char strFileName[LEN] = { 0 };
    fgets(strFileName, LEN, stdin);
    deleteLastCrLf(strFileName);

    FILE* fp = fopen(strFileName, "r+");
    if (fp) {
        writeFile(fp);
    } else {
        fputs("文件不存在, 创建文件...\n", stdout);
        fp = fopen(strFileName, "w+");
        if (fp) {
            writeFile(fp);
        } else {
            fputs("文件创建失败...\n", stdout);
        }
    }
    return 0;
}

运行效果:

Alt 运行效果

3. 完成如下要求

    让用户输入文件名(.txt结尾), 如果文件存在则创建一个同名的.bak文件, 
    在bak文件中追加保存用户输入的信息, 输入#结束输入,
    输入结束后打印bak的信息, 然后要求用户确认.
    用户确认后, 将bak中的信息覆盖至源文件.

程序源代码:

/*************************************************************************
    > 文件名: lesson26.hw.03.c
    > 作者: 花心胡萝卜
    > 邮箱: hxhlb@hxcarrot.com 
    > 创建时间: 2016-10-12 21:38:05
 ************************************************************************/

#include <stdio.h>
#include <string.h>

#define LEN 255
#define TRUE 1

int deleteLastCrLf(char* str) {
    if (*(str + strlen(str) - 1) == '\n') {
        *(str + strlen(str) - 1) = '\0';
    }
    return 0;
}

int writeFile(FILE* fp) {
    char strInput[LEN] = { 0 };    
    do {
        fputs("请输入文件内容(输入#退出..): ", stdout);
        memset(strInput, 0x00, sizeof(strInput));
        fgets(strInput, LEN, stdin);
        if (strcmp("#\n", strInput) != 0) {
            fputs(strInput, fp);
        } else {
            break;
        }
    } while (TRUE);
    fclose(fp);
    return 0;
}

int displayFileContent(FILE* fp) {
    char strContent[LEN] = { 0 };
    while (fgets(strContent, LEN, fp) != NULL) {
        fputs(strContent, stdout);
        memset(strContent, 0x00, sizeof(strContent));
    }
    return 0;
}

int main() {
    
    fputs("请输入文件名(.txt结尾):\n", stdout);
    char strFileName[LEN] = { 0 };
    char strBakFileName[LEN] = { 0 };
    char strInput[LEN] = { 0 };
    fgets(strFileName, LEN, stdin);
    deleteLastCrLf(strFileName);
    sprintf(strBakFileName, "%s.bak", strFileName);
    char strContent[LEN] = { 0 };

    FILE* fpOrig = fopen(strFileName, "r");
    FILE* fpBak = NULL;
    if (fpOrig) { // 文件存在
        // 备份源文件
        fpBak = fopen(strBakFileName, "w+");
        if (fpBak) {
            fseek(fpOrig, 0L, SEEK_SET);
            while (fgets(strContent, LEN, fpOrig) != NULL) {
                fputs(strContent, fpBak);
                memset(strContent, 0x00, sizeof(strContent));
            }
            fflush(fpBak);
            fclose(fpOrig);
            // 输入新内容
            writeFile(fpBak);
            // fflush(fpBak); // 不好用
            fclose(fpBak);
            fpBak = fopen(strBakFileName, "r");
            // 打印新内容
            fseek(fpBak, 0L, SEEK_SET);
            fputs("您输入后的文件内容为:\n===============\n", stdout);
            displayFileContent(fpBak);
            fputs("\n==========\n以上是新的文件内容, 确认请输入1进行保存, 否则退出:\n", stdout);
            char strInp[LEN] = { 0 };
            fgets(strInp, LEN, stdin);
            // 确认保存
            if (strcmp("1\n", strInp) == 0) {
                fpOrig = fopen(strFileName, "w+");
                fseek(fpBak, 0L, SEEK_SET);
                memset(strContent, 0x00, sizeof(strContent));
                while (fgets(strContent, LEN, fpBak) != NULL) {
                    fputs(strContent, fpOrig);
                    memset(strContent, 0x00, sizeof(strContent));
                }
            }
            // 关闭打开的文件
            fclose(fpBak);
            fclose(fpOrig);
        } else {
            fputs("常见备份文件失败!\n", stdout);
        }
    } else {
        fputs("文件不存在, 创建文件...\n", stdout);
        fpOrig = fopen(strFileName, "w+");
        if (fpOrig) {
            writeFile(fpOrig);
        } else {
            fputs("文件创建失败...\n", stdout);
        }
    }
    return 0;
}

运行效果:

Alt 运行效果

如有错误,请提出指正!谢谢.

回复