假设以下几点:
- 你的阵列被适当地分配(无论是在叠层或动态地)是在-至少-10-元件宽。
- 您的结构成员
value
是有效的char *
或定长char[n]
缓冲区。
- 您的结构的
value
成员的字符串数据引用正确以null结尾。
- 由
tempVal
引用的字符串数据是有效的,并且正确地以null结尾。
- 你知道
strcmp()
比较大小写敏感
- 你评论阵列中的约100个元素,并且该代码只检查第一(10)元素是故意
我的水晶球告诉我,最终你需要有你的功能,实际上返回的东西。
int valCheck(char *tempVal)
{
int j;
for(j=0;j<10;j++)
{
if(strcmp(array1[j].value, tempVal) == 0)
{
array1[j].count++; //increment its count
return j; // <== return index of found element
}
}
return -1; // <== return -1 per your description of failure.
}
注:您的编译器,配有体面的警告的检查,会很容易地发现缺的回报此代码。留意这些警告。同样,请仔细检查此答案顶部的项目符号列表中的所有内容,以便确定您正在进行的操作是正确的。
编辑更新,以反映样本字典建立OP。
以下是我认为这可能会被称为。希望它可以帮助你。我换了几件事情:
- 作出的字典有点大(256)
- 处理命令行来获取文件名。使我更容易测试。
- 只处理时报告最后的摘要而不是每个词。
我希望它有帮助。
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
struct values
{
char value[64];
int count;
};
// global array.
struct values array1[256];
int n_words = 0;
//check if value exists in array
int valCheck(const char *tempVal)
{
int j;
for(j=0;j<10;j++)
{
if(strcmp(array1[j].value, tempVal) == 0)
{
array1[j].count++; //increment its count
return j;
}
}
return -1;
}
int main(int argc, char *argv[])
{
FILE * input = NULL;
char tempVal[64];
int i=0;
if (argc < 2)
{
printf("Must specify a filename.\n");
return EXIT_FAILURE;
}
// open file
input = fopen(argv[1],"r");
if (!input)
{
perror("Failed to open file.");
return EXIT_FAILURE;
}
// read all strings from the file one at a time.
while (fscanf(input, "%64s", tempVal) == 1)
{
int i = valCheck(tempVal);
if (i == -1)
{
if (n_words < sizeof(array1)/sizeof(array1[0]))
{
strcpy(array1[n_words].value, tempVal);
array1[n_words].count = 1;
i = n_words++;
}
else
{ // error. no more space in dictionary
printf("No more space to add word: %s\n", tempVal);
}
}
}
fclose(input);
// summary report
for (i=0;i<n_words;++i)
printf("WORD %i: %s, COUNT IS: %i\n", i, array1[i].value, array1[i].count);
return EXIT_SUCCESS;
}
输入
hello my name is dave hello I am dave hi
输出
WORD 0: hello, COUNT IS: 2
WORD 1: my, COUNT IS: 1
WORD 2: name, COUNT IS: 1
WORD 3: is, COUNT IS: 1
WORD 4: dave, COUNT IS: 2
WORD 5: I, COUNT IS: 1
WORD 6: am, COUNT IS: 1
WORD 7: hi, COUNT IS: 1
作业
毕竟,我让你确定为什么下面的代码也可以工作,但是不使用临时缓冲区来完成(反正)。
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
struct values
{
char value[64];
int count;
};
// global array.
#define MAX_WORDS 256
struct values array1[MAX_WORDS] = {{{0},0}};
int n_words = 0;
//check if value exists in array
int valCheck(const char *tempVal)
{
int j;
for(j=0; j< n_words; j++)
{
if(strcmp(array1[j].value, tempVal) == 0)
{
array1[j].count++; //increment its count
return j;
}
}
return -1;
}
int main(int argc, char *argv[])
{
FILE * input = NULL;
int i=0;
if (argc < 2)
{
printf("Must specify a filename.\n");
return EXIT_FAILURE;
}
// open file
input = fopen(argv[1],"r");
if (!input)
{
perror("Failed to open file.");
return EXIT_FAILURE;
}
// read all strings from the file one at a time.
while (n_words < MAX_WORDS &&
fscanf(input, "%64s", array1[n_words].value) == 1)
{
if (valCheck(array1[n_words].value) == -1)
{
array1[n_words].count = 1;
++n_words;
}
}
fclose(input);
// summary report
for (i=0;i<n_words;++i)
printf("WORD %i: %s, COUNT IS: %i\n", i, array1[i].value, array1[i].count);
return 0;
}
输出与以前相同。
A **返回值**可能是有保证的,您不同意吗?如果这应该向调用者指出'valCheck()'找到了值并修改了它的计数,并且更重要的是,如果这是您用于“这不起作用”的说法的基础,那么您可能会*希望通过'return'语句实际传达该事实,而不是EAX寄存器中的某个随机值。 – WhozCraig
请定义*不起作用*。还要将'array1 []'结构的定义添加到问题中。 – wildplasser
从你的代码中,没有人可以说出什么是错的。什么是array1?什么是tempVal –