3

我在分配时遇到问题,我必须将一个文件的内容放入缓冲区,反转这些内容,然后将它们写入另一个文件。该程序需要使用两个如下所示的函数:

  • int read_file( char* filename, char **buffer );
  • int write_file( char* filename, char *buffer, int size);

到目前为止,我的文件如下所示:

文件实用程序.h

 #ifndef UTILS_H
 #define UTILS_H
      int read_file(char* filename, char **buffer);
      int write_file(char* filename, char *buffer, int size);
 #endif

文件实用程序.c

 #include "file_utils.h"
 #include <stdlib.h>
 #include <stdio.h>
 #include <font1.h>
 #include <string.h>
 #include <sys/stat.h>
 #include <unistd.h>

 int read_file(char* filename, char **buffer) {
      FILE* file1;
      file1 = fopen(filename, "r");

      //gets the size of the file
      struct stat st;
      stat(filename, &st);
      int size = st.st_size;

      buffer = malloc(size);
      read(file1, &buffer, 1);
      return size;
 }

 int write_file(char* filename, char*buffer, int size) {
      FILE* file2;
      file2 = fopen(filename, 'w');

      for (int k = size - 1; k >= 0; k--) {
          char* x = &buffer + k;
          fprintf(file2, "%s", x);
      }
      printf(filename, '\O');
      return 1;
 }

反向.c

 #include "file_utils.h"
 #include <stdlib.h>
 #include <stdio.h>
 #include <font1.h>
 #include <string.h>
 #include <sys/stat.h>
 #include <unistd.h>

 int main(int argc, char *argv[]) {
      char* buffer;
      char* filename1;
      char* filename2;
      int filesize;

      filename1 = argv[1];
      filename2 = argv[2];

      filesize = read_file(filename1, &buffer);
      write_file(filename2, buffer, filesize);

      return 0;
 }   

这就是全部。我使用“clang file_utils.c reverse.c”运行它,我收到类似 file_utils.c 的警告

  • incompatible integer to pointer conversion passing 'int" to parameter of type 'const char *'(对于行 file1 = fopen(filename, 'r')
  • incompatible pointer to integer conversion passing 'FILE *' (aka 'struct_IO_FILE*') to parameter of type 'int'(对于 read(file1, &buffer, 1);)
  • 与第一个警告相同,但针对 file2 = fopen(filename, 'w');
  • incompatible pointer types initializing 'char *' with an expression of type 'char **'; dereferences with *(对于 char* x = &buffer + k; 行)

最重要的是,当我继续运行可执行文件时

./a.out file1 file2

其中文件 1 的文本应该反转到文件 2 中,我得到一个分段错误。

对我可以解决的问题的任何见解将不胜感激。

4

3 回答 3

4

就在我的脑海中,未经测试,我看到了这些错误:

buffer = malloc(size);应该*buffer = malloc(size);

...因为buffer是指向 的指针char,所以您需要取消引用它一次。

read(file1, &buffer, 1);应该fread(*buffer, 1, size, file1);

...因为你打开file1fopen,所以它是FILE *read是 Unix I/O,不是流 I/O,并且不使用FILE *.

file2 = fopen(filename, 'w');应该file2 = fopen(filename, "w");

第二个参数应该是一个“字符串”(指向char或数组的 指针char)。'w'是单人char

char* x = &buffer + k;应该char *x = buffer + k;

buffer是指向 的指针char,因此您想直接使用它,而不是获取它的地址。还要注意放在*变量旁边而不是类型旁边的样式。这是一个好习惯,因为这些并不意味着同一件事:

char *a, *b, *c;   /* three pointers */
char* a, b, c;     /* one pointer, two chars */

fprintf(file2, "%s", x);应该fprintf(file2, "%c", *x);

第一种形式视为x字符串的开头,并将从该点开始输出所有内容,直到遇到 NUL 终止符。您只想输出一个char,因此使用说明%c符和取消引用x来获得一个char.

更好的方法是fwrite(x, 1, 1, file2);

printf(filename, '\O');不需要,也不会按照你的想法去做。看起来你打算在最后写一个 NUL。那将是 '\0'(零),而不是'\O'(字母 O)。在任何情况下,它都不需要或不需要。NUL 用于终止 C 中的字符串,而不是文件。如果您这样做,您的输出文件将比它应该的长一个字符。

于 2017-02-03T22:47:15.450 回答
2

您的代码最重要的问题在这里

      char* x = &buffer + k;
      fprintf(file2, "%s", x);

也许你的意思是

      char *x = buffer + k;
      fprintf(file2, "%c", *x);

您也在混合 IO 功能。对于FILE *您应该使用fread()而不是 的对象read(),编译器应该会发出不兼容的参数警告。

如果没有警告(顺便说一句char *x = &buffer + k应该触发另一个警告),那么您可能应该明确启用它们,以便您的编译器可以帮助您找出其他问题。

另外,检查file1不是NULLafter fopen(),检查是否fread()读取了请求的数量,一般检查每个可能的错误,您可以从隐含函数的返回值轻松推断出,如果您不知道此类值的含义,请阅读使用此类功能之前的文档。

于 2017-02-03T22:30:34.243 回答
0

最后一起:

#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <sys/stat.h>
#include <unistd.h>

int read_file(char* filename, char **buffer) {
  FILE* file1;
  file1 = fopen(filename, "r");

  //gets the size of the file
  struct stat st;
  stat(filename, &st);
  int size = st.st_size;

  *buffer = malloc(size);
  fread(*buffer, size, 1, file1);
  fclose(file1);

  return size;
}

void write_file(char* filename, char*buffer, int size) {
  FILE* file2 = fopen(filename, "w"); int k;

  for (k = size - 1; k >= 0; k--) {
    fwrite(buffer + k, 1, 1, file2);
  }

  fclose(file2);
}

int main(int argc, char *argv[]) {
  char* buffer;
  char* filename1;
  char* filename2;
  int filesize;

  filename1 = "input.txt";
  filename2 = "reverse.txt";

  filesize = read_file(filename1, &buffer);
  write_file(filename2, buffer, filesize);

  free(buffer);

  return 0;
}   

带有现场演示。请添加对所有返回值的检查,例如malloc()不返回NULL

于 2017-02-03T23:15:31.460 回答