以下を仮定します。
- 配列は、少なくとも 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;
}
出力は以前と同じです。