写入二进制文件失败(重写文件)



我不确定我做错了什么,试图写一个简单的程序,加密后运行一次重命名。当该方法运行两次时,它应该做同样的事情,但它却解密了文件。第一个版本可以工作,但它离开了旧版本并制作了一个新版本,我想要一个可以运行一次加密并再次解密的程序,允许我在此过程中更改文件扩展名,以方便。

当我运行程序时,它在fwrite()时崩溃。

    #include <stdio.h>
    #include <stdlib.h>
    #define KEY '&'
    int main(void)
    {
        FILE *fp; // file pointer
        size_t size, test; // file size
        char src_file[FILENAME_MAX], dst_file[FILENAME_MAX];
        int orig_char, new_char;
        int i = 0;
        printf("Enter the name of the source file "file.ext": ");
        scanf("%s", src_file);
        if ((fp = fopen(src_file, "rb")) == NULL) { // open file
            fprintf(stderr, "Can't open "%s"n", src_file);
            exit(EXIT_FAILURE);
        }
        fseek(fp, 0, SEEK_END); // find the end of file
        size = ftell(fp);       // file size
        fseek(fp, 0, SEEK_SET); // set file position to start
        unsigned char buffer[size], *temp = buffer; //buffer
        test = fread(buffer, sizeof(buffer[0]), size, fp);
        printf("size written: %d, size of file: %dn", test, size);
        if (test != size) {
            fprintf(stderr, "Error: operation fwrite failed!n");
            exit(EXIT_FAILURE);
        }
        fclose(fp);
        printf("Enter the name of the destination file "file.ext": ");
        scanf("%s", dst_file);
        if ((fp = fopen(src_file, "wb")) == NULL) {
            fprintf(stderr, "Can't open "%s"n", dst_file);
            exit(EXIT_FAILURE);
        }
        puts("Test1");
        for (i = 0; (size_t)i < size && orig_char != EOF; i++, temp++) {
            orig_char = (int) *temp;
            new_char = orig_char ^ KEY;
            *temp = new_char;
        }
        puts("Test3");
        test = fwrite(buffer, sizeof(buffer[0]), size, fp);
        fclose(fp);
        free(buffer);
        if ((rename(src_file, dst_file)) != 0)
            fprintf(stderr, "Failed to rename file, make sure file doesn't" 
                    "already exist!n");
        return 0;
    }

这是我的最终代码,以防其他人遇到这个问题,我稍微清理了一下,并添加了一个循环功能来抵消多个文件。我还添加了一些错误恢复。

    #include <stdio.h>
    #include <stdlib.h>
    #include <ctype.h>
    #define KEY '&'
    int main(void)
    {
        FILE *fp; // file pointer
        size_t size, test; // file size
        char src_file[FILENAME_MAX], dst_file[FILENAME_MAX];
        char ch = 'Y';
        int orig_char, new_char;
        int i = 0;
        while (toupper(ch) != 'N') {
            printf("Enter the name of the source file "file.ext": ");
            scanf("%s", src_file);
            while ((fp = fopen(src_file, "rb")) == NULL) { // open file
                fprintf(stderr, "Can't open "%s"n", src_file);
                printf("Enter the name of the source file "file.ext": ");
                scanf("%s", src_file);
            }
            fseek(fp, 0, SEEK_END); // find the end of file
            size = ftell(fp);       // file size
            fseek(fp, 0, SEEK_SET); // set file position to start
            unsigned char buffer[size], *temp = buffer; // buffer
            // send file to buffer
            test = fread(buffer, sizeof(buffer[0]), size, fp);
            printf("size written: %d, size of file: %dn", test, size);
            if (test != size) {
                fprintf(stderr, "Error: operation fwrite failed!n");
                system("Press any key to continue");
                exit(EXIT_FAILURE);
            }
            fclose(fp);
            while ((fp = fopen(src_file, "wb")) == NULL) {
                fprintf(stderr, "Can't open "%s"n", src_file);
                printf("Enter the name of the source file "file.ext": ");
                scanf("%s", src_file);
            }
            // offset buffer data
            for (i = 0; (size_t)i < size && orig_char != EOF; i++, temp++) {
                orig_char = (int) *temp;
                new_char = orig_char ^ KEY;
                *temp = new_char;
            }
            // write buffer to file
            test = fwrite(buffer, sizeof(buffer[0]), size, fp);
            fclose(fp);
            printf("Enter the file's new name "file.ext": ");
            scanf("%s", dst_file);
            while ((rename(src_file, dst_file)) != 0) {
                fprintf(stderr, "Failed to rename file, make sure file doesn't already                         exist!n");
            }
            printf("size written: %d, size of file: %dn", test, size);
            printf("File Successfully offsetnn");
            printf("Would you like to continue: ");
            scanf(" %c", &ch);
        }
        return 0;
    }

相关内容

  • 没有找到相关文章

最新更新