[英]Trying to add null character to end of a char array before strcpy
Working on a problem where I have to read data from a file into a struct using . 解决一个问题,我必须使用将文件中的数据读取到结构中。
The file is organized so that there is a name, a few lines of ASCII art terminated by a # and a rating. 该文件经过组织,以便有一个名称,几行ASCII图片(以#结尾)和一个等级。 Here is an example
这是一个例子
Sample Name
( S )
( S )
# 5
I have my struct set up like this: 我有这样的结构设置:
typedef struct
{
char* name;
char* art;
int rating;
}CASE;
When I compile my source, I keep getting the following warnings: 编译源代码时,我不断收到以下警告:
multiple-character character constant
overflow in implicit constant conversion
on this line buffer[artCount] = '/0';
在此行上
buffer[artCount] = '/0';
where artCount is strlen of the buffer itself. 其中artCount是缓冲区本身的内容。
I'm simply adding a null character to the end of a character array to prepare for a strcpy. 我只是在字符数组的末尾添加一个空字符以准备strcpy。 Is there something wrong with my logic here?
我的逻辑有什么问题吗?
function: 功能:
/*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'
(带正斜杠)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.