将 2D 字符数组写入文本文件

Writing a 2D character array into a text file

提问人:3nondatur 提问时间:5/18/2023 最后编辑:John Kugelman3nondatur 更新时间:5/18/2023 访问量:66

问:

我正在尝试将我的 2D 字符数组“A”写入.txt文件“some_file.txt”。我试图将谢尔盖·卡利尼琴科(Sergey Kalinichenko)从这个问题中的答案用于我的代码(见下文)。但是,它不起作用。你能告诉我我做错了什么吗?

#include <stdio.h>
#include <stdlib.h>


char A[10][10] =
{
  {'~', 'S', 'S', 'S', 'S', 'S', '~', '~', '~', 'S'},
  {'S', '~', '~', '~', '~', '~', '~', '~', '~', 'S'},
  {'S', '~', '~', 'S', 'S', 'S', '~', 'S', '~', '~'},
  {'S', '~', '~', '~', '~', '~', '~', 'S', '~', '~'},
  {'S', '~', 'S', 'S', 'S', '~', '~', '~', '~', '~'},
  {'~', 'S', '~', '~', '~', '~', '~', '~', '~', '~'},
  {'~', 'S', '~', '~', '~', '~', 'S', '~', '~', '~'},
  {'~', 'S', '~', '~', '~', '~', 'S', '~', '~', 'S'},
  {'~', 'S', '~', 'S', 'S', '~', '~', '~', '~', 'S'},
  {'~', '~', '~', '~', '~', '~', '~', '~', '~', 'S'}
};

void saveImage(int height, int width, char image[height][width], const char* file_name)
{
  FILE *file = fopen(file_name, "wb");
  fwrite(image, sizeof(char), sizeof(image), file);
  fclose(file);
}

int main()
{
  saveImage(10, 10, A, "some_file.txt");

  return 0;
}
Arrays C 文件

评论

4赞 yano 5/18/2023
sizeof(image)不是你想的那样
2赞 Ted Lyngmo 5/18/2023
诀窍是不要让数组衰减为指针 - 或者这样做,但要考虑到这一点
2赞 atru 5/18/2023
...并注意编译器警告(打开它们后)。
2赞 Ted Lyngmo 5/18/2023
@atru <-钉住了
2赞 Ted Lyngmo 5/18/2023
@atru 我认为用你的话回答会起作用。这个问题无疑有一个重复的答案,但你没有。

答:

0赞 0___________ 5/18/2023 #1

sizeof在您的问题中给出指针的大小。使用指向数组的指针,:)就可以正常工作了。此外,请使用正确的类型。sizeof

void saveImage(size_t height, size_t width, char (*image)[height][width], const char* file_name)
{
  FILE *file = fopen(file_name, "wb");
  if(file) 
  {
     fwrite(image, sizeof(char), sizeof(*image), file);
     fclose(file);
  }
}

//usage saveImage(10, 10, &A, "some_file.txt");

void saveImage(size_t height, size_t width, char (*image)[width], const char* file_name)
{
  FILE *file = fopen(file_name, "wb");
  if(file) 
  {
     fwrite(image, sizeof(char), height * sizeof(*image), file);
     fclose(file);
  }
}

//usage   saveImage(10, 10, A, "some_file.txt");

4赞 atru 5/18/2023 #2

将数组作为参数发送到函数时,数组会衰减为指针。这篇文章可能是关于衰变的一个很好的阅读开始。

由于这种衰减,ur 实际上是指针的大小,而不是所有数组元素的数量。正如您已经看到的,您需要将其替换为实际的元素编号:sizeof(image)

fwrite(image, sizeof(char), height*width, file);

重要的是,在编译时,您应该收到指示上述内容的警告。例如,在 GCC 9.4.0 (Ubuntu) 上,我得到:

cscratch.c: In function ‘saveImage’:
cscratch.c:23:35: warning: ‘sizeof’ on array function parameter ‘image’ will return size of ‘char (*)[(sizetype)(width)]’ [-Wsizeof-array-argument]
   23 | fwrite(image, sizeof(char), sizeof(image), file);
      |                                   ^
cscratch.c:19:44: note: declared here
   19 | void saveImage(int height, int width, char image[height][width], const char* file_name)


  |                                       ~~~~~^~~~~~~~~~~~~~~~~~~~

作为基本编译的一部分,没有指定任何特殊的警告标志(强烈建议使用比默认警告标志更严格、更全面的警告标志)。

在此消息中,您可以清楚地看到问题及其来源 - 重点是应该启用严格的警告并密切关注它们。

评论

1赞 Ted Lyngmo 5/18/2023
对不起,我回滚了您的编辑。我的评论让你误入歧途。我的错。
1赞 atru 5/18/2023
对不起,经过漫长的一天,已经很晚了!谢谢你:)
1赞 Ted Lyngmo 5/18/2023
你没有做错什么。我做了。干杯!