将一个二维字符数组写入文本文件。

huangapple go评论64阅读模式
英文:

Writing a 2D character array into a text file

问题

我正在尝试将我的2D字符数组"A"写入一个名为"some_file.txt"的.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;
}
英文:

I am trying to write my 2D character array "A" into a .txt file "some_file.txt". I tried to use the Sergey Kalinichenko's answer from this question for my code (see below). However, it does not work. Could you please tell me what I am doing wrong.

#include &lt;stdio.h&gt;
#include &lt;stdlib.h&gt;


char A[10][10] =
{
  {&#39;~&#39;, &#39;S&#39;, &#39;S&#39;, &#39;S&#39;, &#39;S&#39;, &#39;S&#39;, &#39;~&#39;, &#39;~&#39;, &#39;~&#39;, &#39;S&#39;},
  {&#39;S&#39;, &#39;~&#39;, &#39;~&#39;, &#39;~&#39;, &#39;~&#39;, &#39;~&#39;, &#39;~&#39;, &#39;~&#39;, &#39;~&#39;, &#39;S&#39;},
  {&#39;S&#39;, &#39;~&#39;, &#39;~&#39;, &#39;S&#39;, &#39;S&#39;, &#39;S&#39;, &#39;~&#39;, &#39;S&#39;, &#39;~&#39;, &#39;~&#39;},
  {&#39;S&#39;, &#39;~&#39;, &#39;~&#39;, &#39;~&#39;, &#39;~&#39;, &#39;~&#39;, &#39;~&#39;, &#39;S&#39;, &#39;~&#39;, &#39;~&#39;},
  {&#39;S&#39;, &#39;~&#39;, &#39;S&#39;, &#39;S&#39;, &#39;S&#39;, &#39;~&#39;, &#39;~&#39;, &#39;~&#39;, &#39;~&#39;, &#39;~&#39;},
  {&#39;~&#39;, &#39;S&#39;, &#39;~&#39;, &#39;~&#39;, &#39;~&#39;, &#39;~&#39;, &#39;~&#39;, &#39;~&#39;, &#39;~&#39;, &#39;~&#39;},
  {&#39;~&#39;, &#39;S&#39;, &#39;~&#39;, &#39;~&#39;, &#39;~&#39;, &#39;~&#39;, &#39;S&#39;, &#39;~&#39;, &#39;~&#39;, &#39;~&#39;},
  {&#39;~&#39;, &#39;S&#39;, &#39;~&#39;, &#39;~&#39;, &#39;~&#39;, &#39;~&#39;, &#39;S&#39;, &#39;~&#39;, &#39;~&#39;, &#39;S&#39;},
  {&#39;~&#39;, &#39;S&#39;, &#39;~&#39;, &#39;S&#39;, &#39;S&#39;, &#39;~&#39;, &#39;~&#39;, &#39;~&#39;, &#39;~&#39;, &#39;S&#39;},
  {&#39;~&#39;, &#39;~&#39;, &#39;~&#39;, &#39;~&#39;, &#39;~&#39;, &#39;~&#39;, &#39;~&#39;, &#39;~&#39;, &#39;~&#39;, &#39;S&#39;}
};

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

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

  return 0;
}

答案1

得分: 4

当将数组作为参数传递给函数时,该数组会转化为指针。这篇帖子可能是关于这种转化的良好起点阅读。

由于这种转化,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)
         |                                       ~~~~~^~~~~~~~~~~~~~~~~~~~

这条警告是基本编译的一部分,没有指定任何特殊的警告标志(强烈建议使用比默认标志更严格和更全面的警告标志)。在这条消息中,您可以清楚地看到问题及其源头 - 关键是应该启用严格的警告并密切关注它们。

英文:

When sending an array to a function as a parameter, the array decays into a pointer. This post may be a good reading start on the decay.

Because of this decay, your sizeof(image) is actually the size of the pointer and not the number of all array elements. As you already saw, you need to replace it with the actual element number:

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

The important part is that when compiling, you should get a warning indicating the above. For instance, on GCC 9.4.0 (Ubuntu) I get:

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)
         |                                       ~~~~~^~~~~~~~~~~~~~~~~~~~

As part of a basic compilation that does not have any special warning flags specified (it is highly recommended to use stricter and more comprehensive warning flags than the default ones).

In this message, you can clearly see the issue and its source - point being that one should enable strict warnings and pay close attention to them.

答案2

得分: 0

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);
  }
}

// 使用示例: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);
  }
}

// 使用示例:saveImage(10, 10, A, "some_file.txt");
英文:

sizeof in your question is giving the size of the pointer. Use a pointer to the array and sizeof will work fine :). Also, use the correct types.

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

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

or

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

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

huangapple
  • 本文由 发表于 2023年5月18日 07:12:14
  • 转载请务必保留本文链接:https://go.coder-hub.com/76276749.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定