c - 尝试在 strcpy 之前将空字符添加到 char 数组的末尾

标签 c pointers struct

正在解决我必须使用 . 将文件中的数据读入结构的问题。

文件的组织方式包括一个名称、几行以 # 和评级结尾的 ASCII 艺术字。这是一个例子

Sample Name
( S )
( S )
# 5

我的结构是这样设置的:

typedef struct
 {
   char* name;
   char* art;
   int rating;
 }CASE;

当我编译源代码时,我不断收到以下警告:

multiple-character character constant
overflow in implicit constant conversion

在这一行 buffer[artCount] = '/0'; 其中 artCount 是缓冲区本身的 strlen。

我只是在字符数组的末尾添加一个空字符,为 strcpy 做准备。我的逻辑有问题吗?

功能:

/*CASE* all is an empty array of CASE structs*/
void readFile(FILE* FPin, CASE* all)
{
  CASE* walker = all;
  int count = 0;
  int artCount;
  char buffer[160];

  if((FPin = fopen("art.txt", "r")) == NULL)
  {
    printf("Error opening file.");
    exit(100);  
  }

 walker->name = (char*)malloc(sizeof(char)*100);

 /*Reads in the name*/
 while(fgets(walker->name, 100, FPin) != NULL)
  {

 /*Reads in the art*/
   while(fscanf(FPin, "%c", buffer) != '#');

   artCount = strlen(buffer);
   buffer[artCount] = '/0';
   walker->art = (char*)malloc(sizeof(char)*160);
   strcpy(walker->art, buffer);

/*Reads in the rating*/

     fscanf(FPin, "%d", &walker->rating);

   count++;
   walker++;
 } 

  fclose(FPin);
  return;
}

最佳答案

常量应该是'\0'(带反斜杠),而不是'/0'(带正斜杠)。

关于c - 尝试在 strcpy 之前将空字符添加到 char 数组的末尾,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10288751/

相关文章:

c - 将 char 数组传递给另一个函数

c++ - ZeroMQ C++ 上的简单 FlatBuffer 示例 - 通过 zmq 将结构复制到 flatbuffer 并再次返回结构

c - 链表中的段错误

c++ - Linux ext3 和 ext4 文件 - 兼容性问题

c 队列链表 - 打印队列更改指针的值?

C++ 引用指针作为改变指针的参数

C 对应于 C++ find_first_not_of?

C自定义重写规则

c - 如何使指针在新结构中兼容?

c - 返回指向结构的指针后出现段错误