1

C で配列を読み取る方法がわかりません。ユーザー入力を配列の一部と一致させようとしています。配列は、次のようなテキスト ファイルから入力されます。

1754
1350

等々。現在、配列には合計 8 つの 4 桁の数字があります。これらの数値をさらにテキスト ファイルに追加し、同じコードを使用してユーザー入力で配列をスキャンできるようにしたいと考えています。ここに私が取り組んでいるコードがあります:

#include <stdio.h>
#include <string.h>
#include <stdlib.h>

/*this is to test strings*/
int main ()
{

    FILE* spEmployees;
    printf("Enter the code:");
    char A[100];
    scanf("%c", A);
    char empNum[100];//stores empolyee numbers
    spEmployees = fopen("Employees.txt", "r");
    if (spEmployees == NULL)
    {
        printf("fail\n");
    }
    else
    {
        int num_lines = 0;
        while ( !feof (spEmployees) && fgets(empNum, 99, spEmployees ))
        {
            printf("%s", empNum);
            num_lines ++;
        }
    }
    fclose(spEmployees);
    getchar();
    return 0;
}

そのため、現在、配列をスキャンまたは比較するための場所はありません。これは、配列のテキスト ファイルから情報を取得し、ユーザー入力を読み取るために機能します。私はほぼすべての標準 C 文字列関数を試しました。どんなアイデアでも役に立ちます。

4

2 に答える 2

0

私のコメントのように、私が書く解決策は、データを単一の文字配列にシリアル化するか、簡単に比較できるようにすべてを数値に変換するか、文字配列の配列を使用して文字列を 1 つずつ比較するかのいずれかです。

これらのオプションの中で、データをシリアル化するのが最も簡単だと思いますが、正しい解決策であるためには (少なくとも) 次の条件のいずれかが必要です。

  1. すべてがまったく同じ長さ/型を持つ通常のデータ
  2. あるデータ項目と別のデータ項目の違いを示す空白文字
  3. ある種の構造 (単一の空白文字よりも複雑ですが、同じ効果があります)

(1) の場合で、データが 4 文字の文字列として到着し、ユーザーの入力が 4 文字に制限されていると仮定すると、次のように書くことができます。

#define MAX_ITEMS 100
#define ITEM_SIZE 4
...
char fullSet[MAX_ITEMS*ITEM_SIZE+1];
int num_lines = 0;
while (!feof (spEmployees) && fgets(empNum, 99, spEmployees) && num_lines < MAX_ITEMS) {
    printf("%s", empNum);
    snprintf(&(fullSet[num_lines*ITEM_SIZE]), ITEM_SIZE+1, "%s", empNum);
    num_lines++;
}
char *idx = strstr(fullSet, A);
int isMatch = 0;
if (idx != NULL) {
    if ((int)(fullSet-idx) % ITEM_SIZE == 0)
        isMatch = 1;
}
// do something with having found a match...

(1) で仮定を行いNULL、文字列の区切り文字を上書きして数学を簡単にすることを選択した後、ある文字列の一部を別の文字列の一部で取得しないようにするために必要な唯一のこと一致として、の結果がstrstr私のシリアライゼーションのいずれかの項目の「先頭」から始まるかどうかを判断することです。もちろん、A含まれている文字列が より長いか短いかはチェックしていませんITEM_SIZEが、それは簡単に理解できるはずです。

この特定のシリアライゼーションの隠れた機能の 1 つは、エントリ間の終了のために最初のエントリの後で停止するのではなく、変数strstr全体で のインスタンスを検索することです。内の最後のエントリには、最後の呼び出しからの終了がまだあることに注意してください。fullSetANULLfullSetNULLsnprintf(...)

于 2013-11-12T13:35:39.827 に答える
0

この2つを作りたいですよね?

  • 「これらの数字をテキストに追加する機能」
  • 「同じコードを使用して、ユーザー入力で配列をスキャンします。」

次のコードには、これら 2 つの機能が含まれています (コメントに注意してください)。

#include <stdio.h>
#include <string.h>
#include <stdlib.h>

/*this is to test strings*/
int main ()
{

    FILE* spEmployees;
    char A[100];
    char *A_ptr = &A[0];
    char empNum[100];//stores empolyee numbers

    printf("Enter the code:");
    scanf("%s", A_ptr);

    spEmployees = fopen("Employees.txt", "r+"); // read/update

    if (spEmployees == NULL)
    {
        printf("fail\n");
    }
    else
    {
        int num_lines = 0;
        while ( !feof (spEmployees) && fgets(empNum, 99, spEmployees ))
        {
            printf("%s", empNum);
            num_lines ++;
        }

        // "the ability to add more of these numbers into the text file"
        fprintf(spEmployees, A);    

        // adding a new-line char to the file so the next number
        // will be written in the next line
        fprintf(spEmployees, "\n");
    }

    fclose(spEmployees);
    getchar();
    return 0;
}

ファイルは次のようにする必要があることに注意してください。

1754
1350

^
After writing manually `1350` to the file you need to go to the next line
so the program will write the new (entered) value to the file at the beginning
of the new line instead of continuing the 2nd:

1754
13502013   // like this
于 2013-11-12T05:24:26.110 に答える