在C中使用strcpy、strcat的冲突?
在下面的代码中,我试图逐个字符加载一个单词文本文件 然后我尝试将每个单词保存在哈希表(字符串数组)中 但是似乎strcpy保存了一个完整的单词,而不是一个字符,我不知道为什么。我是否误用了strcpy和strcat在C中使用strcpy、strcat的冲突?,c,strcpy,strcat,C,Strcpy,Strcat,在下面的代码中,我试图逐个字符加载一个单词文本文件 然后我尝试将每个单词保存在哈希表(字符串数组)中 但是似乎strcpy保存了一个完整的单词,而不是一个字符,我不知道为什么。我是否误用了strcpy和strcat # include <stdio.h> # include <stdlib.h> # include <string.h> # include <ctype.h> # include <stdbool.h> bool loa
# include <stdio.h>
# include <stdlib.h>
# include <string.h>
# include <ctype.h>
# include <stdbool.h>
bool load(const char* dictionary);
#define LENGTH 45
int main (int argc, char* argv[])
{
char* dictionary = argv[1];
load(dictionary);
return 0;
}
bool load(const char* dictionary)
{
int index = 0, words = 0, kk = 0;
int lastl = 0, midl = 0;
char word[LENGTH + 1];
char *wholeword[1001];
FILE* dic = fopen(dictionary, "r");
if (dic == NULL)
{
printf("Could not open %s.\n", dictionary);
return false;
}
for (int c = fgetc(dic); c != EOF; c = fgetc(dic))
{
// allow only alphabetical characters and apostrophes
if (isalpha(c) || (c == '\'' && index > 0))
{
// append character to word
word[index] = c;
index++;
// ignore alphabetical strings too long to be words
if (index > LENGTH)
{
// consume remainder of alphabetical string
while ((c = fgetc(dic)) != EOF && isalpha(c));
// prepare for new word
index = 0;
}
}
// ignore words with numbers (like MS Word can)
else if (isdigit(c))
{
// consume remainder of alphanumeric string
while ((c = fgetc(dic)) != EOF && isalnum(c));
// prepare for new word
index = 0;
}
// we must have found a whole word
else if (index > 0)
{
// terminate current word
word[index] = '\0';
lastl = index - 1;
midl = (index - 1) % 3;
words++;
index = 0;
int hashi = (word[0] + word[lastl]) * (word[midl] + 17) % 1000;
wholeword[hashi] = (char*) malloc(sizeof(char) * (lastl + 2));
strcpy(wholeword[hashi], &word[0]); // ***
for (kk = 1; kk <= lastl + 1; kk++)
{
strcat(wholeword[words], &word[kk]);
}
}
}
fclose(dic);
return true;
}
#包括
#包括
#包括
#包括
#包括
布尔加载(常量字符*字典);
#定义长度45
int main(int argc,char*argv[])
{
char*dictionary=argv[1];
加载(字典);
返回0;
}
布尔加载(常量字符*字典)
{
int-index=0,words=0,kk=0;
int lastl=0,midl=0;
字符字[长度+1];
char*wholeword[1001];
文件*dic=fopen(字典,“r”);
如果(dic==NULL)
{
printf(“无法打开%s.\n”,字典);
返回false;
}
对于(INTC=fgetc(dic);c!=EOF;c=fgetc(dic))
{
//仅允许字母字符和撇号
if(isalpha(c)| |(c='\''&&index>0))
{
//将字符追加到单词中
单词[索引]=c;
索引++;
//忽略太长而不能作为单词的字母字符串
如果(索引>长度)
{
//使用字母字符串的剩余部分
而((c=fgetc(dic))!=EOF&isalpha(c));
//准备新单词
指数=0;
}
}
//忽略带数字的单词(如MS Word can)
否则,如果(i数字(c))
{
//使用字母数字字符串的剩余部分
而((c=fgetc(dic))!=EOF和isalnum(c));
//准备新单词
指数=0;
}
//我们一定找到了一个完整的词
否则,如果(索引>0)
{
//终止当前字
字[索引]='\0';
lastl=指数-1;
midl=(指数-1)%3;
words++;
指数=0;
int hashi=(单词[0]+单词[lastl])*(单词[midl]+17)%1000;
wholeword[hashi]=(char*)malloc(sizeof(char)*(lastl+2));
strcpy(wholeword[hashi],&word[0]);//***
对于(kk=1;kkStrcpy不会复制单个字符,它会复制所有字符,直到下一个空('\0'
)字节。要在代码中复制单个字符,请尝试:
wholeword[hashi] = &word[0];
而不是:
strcpy(wholeword[hashi], &word[0]);
Strcpy不会复制单个字符,它会复制所有字符,直到下一个空('\0'
)字节。要在代码中复制单个字符,请尝试:
wholeword[hashi] = &word[0];
而不是:
strcpy(wholeword[hashi], &word[0]);
是的,您误用了strcpy
和strcat
:这些函数将整个源字符串复制到目标数组(位于strcat
的现有字符串末尾)
以下几行:
wholeword[hashi] = (char*) malloc(sizeof(char) * (lastl + 2));
strcpy(wholeword[hashi], &word[0]); // ***
for (kk = 1; kk <= lastl + 1; kk++)
{
strcat(wholeword[words], &word[kk]);
}
}
strdup()
分配内存,将参数字符串复制到内存中并返回指针。所有Posix系统都可以使用它,如果没有,请使用以下两行:
wholeword[hashi] = malloc(lastl + 2);
strcpy(wholeword[hashi], word);
注:
- 您假设您的哈希是完美的,没有冲突。按照当前编码,冲突会导致从字典中删除前一个单词,并丢失相应的内存
- 字典
char*wholeword[1001];
是load
函数中的一个局部变量。它未初始化,因此无法知道条目是否是指向单词的有效指针。应将其分配、初始化为NULL
并返回给调用方
是的,您误用了strcpy
和strcat
:这些函数将整个源字符串复制到目标数组(位于现有字符串的末尾,用于strcat
)
以下几行:
wholeword[hashi] = (char*) malloc(sizeof(char) * (lastl + 2));
strcpy(wholeword[hashi], &word[0]); // ***
for (kk = 1; kk <= lastl + 1; kk++)
{
strcat(wholeword[words], &word[kk]);
}
}
strdup()
分配内存,将参数字符串复制到内存中并返回指针。所有Posix系统都可以使用它,如果没有,请使用以下两行:
wholeword[hashi] = malloc(lastl + 2);
strcpy(wholeword[hashi], word);
注:
- 您假设您的哈希是完美的,没有冲突。按照当前编码,冲突会导致从字典中删除前一个单词,并丢失相应的内存
- 字典
char*wholeword[1001];
是load
函数中的一个局部变量。它未初始化,因此无法知道条目是否是指向单词的有效指针。应将其分配、初始化为NULL
并返回给调用方
很难理解你的问题是什么。strcpy
函数,顾名思义,复制了一个字符串。什么是wword
?你有没有尝试用调试器单步检查代码?@DavidSchwartz wword在这里输入错误(我编辑了它)为wholeword(字符串数组),谢谢。很难理解你的问题是什么。strcpy函数,顾名思义,复制了一个字符串。什么是wword
?你有没有试过用调试器逐行检查代码?@DavidSchwartz wword在这里输入错误(我编辑过)为wholeword(字符串数组),谢谢不要在数组下标前加空格-它们绑定得很紧,不应该这样隔开。例如,在&word[0]
中,优先级是&(word[0]
)而不是(&word)[0]
。请不要将空格放在数组下标之前-它们绑定得很紧,不应该这样隔开。例如,在&word[0]
中,优先级是&(word[0]
),而不是(&word)[0]
。