假设如下:
- 您的数组已正确分配(在堆栈上或动态)为至少 10 个元素宽。
- 您的结构成员
value
是有效的char *
或固定长度的char[n]
缓冲区。
- 结构成员引用的字符串数据
value
正确地以空值结尾。
- 引用的字符串数据
tempVal
有效且正确地以空值结尾。
- 您知道
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;
}
输出与之前相同。