簡體   English   中英

如何在C語言中掃描數組的各個部分尋找匹配的用戶輸入

[英]How to scan through parts of an array in C Looking to match user input

我在弄清楚如何使用C讀取數組時遇到麻煩。我試圖將用戶輸入與數組的各個部分進行匹配。 該數組是從一個文本文件填充的,該文本文件如下所示:

1754
1350

等等。 截至目前,陣列中共有8個四位數。 我希望能夠將更多這些數字添加到文本文件中,並且仍然能夠使用相同的代碼通過用戶輸入來掃描整個數組。 這是我正在處理的代碼:

#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字符串函數。 任何想法都會有所幫助。

您想做這兩件事吧?

  • “能夠在文本中添加更多這些數字的功能”
  • “使用相同的代碼通過用戶輸入來掃描整個陣列。”

以下代碼包括這兩個功能(請注意注釋 ):

#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

如我的評論所述,我將要寫的解決方案是將數據序列化為單個字符數組或將所有內容都轉換為數字以便於進行比較或使用字符數組和字符串比較的一種選擇。

在這些選項中,我認為序列化數據將是最簡單的,盡管它需要(至少)以下條件之一才是正確的解決方案:

  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長或短的字符串,但這應該很容易找出。

此特定序列化的一個隱藏功能是, strstr將在整個fullSet變量中搜索A的實例,而不是由於條目之間的NULL終止而在第一個條目之后停止。 請注意, fullSet中的最后一個條目在上一次snprintf(...)調用后仍將具有終止NULL

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM