2014-06-17 50 views
0

我不确定我在做什么错,试图编写一个简单的程序,在重命名后运行后加密。当该方法运行两次时,它应该做同样的事情,但它解密文件。第一个版本可以工作,但是它留下了旧版本,并且创建了一个新版本,并且我想要一个程序,我可以运行一次来​​加密并再次解密,以允许我在过程中更改文件扩展名,以方便起见。C - 写入二进制失败(重写文件)

当我运行该程序时,它在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: %d\n", 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; 
    } 
+0

对不起,我想通了,我忘了删除免费(缓冲区);一旦我将其改为指针 –

+0

我仍然很感激任何关于更改我的代码的输入以清除它并使其看起来更好。 –

+1

你应该做的是在http://codereview.stackexchange.com/上发布工作代码。堆栈溢出重点解决特定的技术问题。 – Lundin

回答

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: %d\n", 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: %d\n", test, size); 
      printf("File Successfully offset\n\n"); 
      printf("Would you like to continue: "); 
      scanf(" %c", &ch); 
     } 

     return 0; 
    }