【发布时间】:2014-05-19 21:08:40
【问题描述】:
我正在编写一个只使用 C 的程序,我是一名学生,只有 5 个月的培训。它将打开一个文件,将行存储为字符串,计算我使用的数组中有多少个字符串,然后关闭文件。这是我写的函数。
char animalsarray[100][100], xstring='x';
int numlines1;
void preload(){
int j;
strcpy(animalfile,"animals.txt.");
animals=fopen(animalfile,"r");
if(animals==NULL){
printf("ERROR: animals.txt not found!");
exit(13);
}
for(j=0;j<100;j++){
strcpy(xstring, animalsarray[j][0]);
}
j=0;
while(sscanf(animalsarray[j][0],100,animals)!= EOF){
j++;
}
for(j=0;j==100;j++){
if(animalsarray[j][0]!='x'){
numlines1++;
}
}
fclose(animals);
} 我遇到的问题是这个
错误:来自
char' tochar*'的无效转换错误:初始化 `char* strcpy(char*, const char*)' 的参数 1
错误:来自
char' toconst char*'的无效转换错误:正在初始化 `char* strcpy(char*, const char*)' 的参数 2
将单个字符放入所有字符串有什么问题吗?
【问题讨论】:
-
您能告诉我们错误发生在哪一行,并在您的代码中标记该行吗?
-
我推测这是他拥有的每个地方
animalsarray[j][0]或xstring -
你见过xstring的类型吗?它不是一个 char* 而是一个简单的 char。另外,我引用@KerrekSB。请提供更多信息,因为我们不是读心者:)
-
strcpy(xstring, animalsarray[j][0]);->animalsarray[j][0]=xstring;, -
while(sscanf(animalsarray[j][0],100,animals)!= EOF){-->while(fgets(animalsarray[j],100,animals)!= NULL){