2017-02-24 29 views
0

存在的數組的話,我有話數組:檢查是否在一個txt文件用C

const char *words[3]={cat,dog,snake,bee};

和這樣一個txt文件:

apple tree day night story bee oil lemons get fight 234 meow woof safari 
jazz stuff what is dog fight street snake garden glass house bee question     
foot head 29191 43493 == 

(我們不知道這個文件有多少行)

我想檢查整個文件,並且每次找到數組中的一個單詞來打印該單詞並打印找到的單詞。

我在比較時遇到了問題。我的想法是將文件的每個單詞保存到一個數組中,並將每個單詞與單詞數組的單詞進行比較。但我不能那樣做。我有這個:

FILE *f; 
const char *arr; 
f=fopen("test.txt","r"); 
while(fscanf(f,"%s",arr)!EOF) 

我真的不知道在這裏寫什麼,以便我將文件分成單詞。

請對我好,我只是想學習。

+1

使用'的strstr()',那將是很容易 –

+0

我該使用怎樣的strstr完全相同()?我的意思是在打開我的文件和上面使用fscanf之後? – frog

+0

'系統(「grep ...」)'...(鴨子和跑步......嘿,這是狂歡節......) – DevSolar

回答

0

使用函數getline &的strstr

char *line = NULL; 
size_t len = 0; 
ssize_t read; 

int line_no = 0; 
while ((read = getline(&line, &len, f)) != -1) 
{ 
    ++line_no; 
    for (int i = 0; i < 3; i++) { 
     if (strstr(line, words[i]) != null) 
     { 
      // if matched 
     } 
    } 
} 
+0

這給了我一個分割:( – frog

+2

你必須改變:單詞[3] - > words [4] –

+0

請注意,'strstr'將搜索子字符串而不是單詞。例如,如果在「manager」中找到「age」,但它不是相同的單詞... –

1

有幾個問題,你所提供的代碼片段:

const char *words[3]={cat,dog,snake,bee}; 

在這裏,您聲明3個元素的數組,但你有4個初始化。你忘了在單詞之間插入單詞。

在這裏,您使用fscanf讀入arr,但你沒有分配內存,arr沒有初始化,你大概的意思是寫char arr[200],200是最大字長。

FILE *f; 
const char *arr; 
f=fopen("test.txt","r"); 
while(fscanf(f,"%s",arr)!EOF) 

您要使用這個基地,堅韌仍有空間形式的改進:

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

const char *words[] = { "cat", "dog", "snake", "bee" }; 

int main() 
{ 
    char line[200]; // maximum line size is 200 
    size_t len = 0; 

    FILE *f; 
    f = fopen("test.txt", "r"); 

    if (f == NULL) 
    { 
    printf("Can't open file\n"); 
    return 1; 
    } 

    int line_no = 0; 
    while (fgets(line, sizeof line, f)) 
    { 
    ++line_no; 

    // (sizeof words)/sizeof *words is the the number of words in the words array 
    for (int i = 0; i < (sizeof words)/sizeof *words; i++) 
    { 
     if (strstr(line, words[i]) != NULL) 
     { 
     printf("found %s in line %d\n", words[i], line_no); 
     } 
    } 
    } 

    fclose(f); 
} 
+0

'strstr'搜索子字符串,將在「經理」中找到「年齡」... –

+0

@SergeBallesta它只是一個基礎,但是有改進的空間 –

+1

'for(int i = 0; i <(sizeof words)/ sizeof * words; i ++)'比較有符號和無符號整數,應該是'for(size_t i = 0; i <(sizeof words)/ sizeof * words; i ++)'。 – RoadRunner

1

您正在使用fscanf()讀出來的話你的文件,這是不是這樣做的最佳方式。您應該使用getline(3)fgets(3)來讀取文件的每一行。

此外,該行:

const char *words[3]={cat,dog,snake,bee}; 

需要能夠容納4個char*指針,而不是3.您還需要包括與這些字符串文字引號。這是另一種方式來做到這一點:

const char *words[] = {"cat", "dog", "snake", "bee"}; 

然後得到這個數組的大小,只需使用sizeof(x)/sizeof(x[0])

此外,在該代碼段:

FILE *f; 
const char *arr; 
f=fopen("test.txt","r"); 
while(fscanf(f,"%s",arr)!EOF) 

你是一個未初始化的指針,這會導致很多問題使用fscanf()。如果你想使用指針,你可能需要在malloc(3)上動態分配堆上的arr。如果你不想這樣做,只需申報一個VLA,如char arr[200]。另外fscanf()返回掃描的項目數,因此fscanf(f,"%s",arr)!=EOF將不得不用fscanf(f,"%s",arr)==1來替換,以確保一次讀取一個字。

注意:您還應該檢查FILE *f是否正確打開,因爲它可以返回NULL出錯。

我在比較時遇到了麻煩。我的想法是將文件的每個單詞保存到一個數組中,並將每個單詞與單詞數組的單詞進行比較。

正如其他人所提到的使用strstr(3),另一個可能的選擇是使用strtok(3)解析就行了每個字,然後用strcmp(3)與文件解析的字比較words[i]。如果words[]將來變得更大,我會建議使用二分搜索而不是線性搜索來比較單詞。這將提高您從O(n)O(logn)的搜索時間。

下面是一些(修改)的代碼,我寫這之前做類似的事情:

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

#define ARRAYSIZE(x) (sizeof x/sizeof x[0]) 

int main(void) { 
    const char *words[] = {"cat", "dog", "snake", "bee"}; 
    FILE *fptr; 
    char *line = NULL, *word = NULL; 
    const char *delim = " \n"; 
    size_t len = 0, lineno = 0; 
    ssize_t read; 

    fptr = fopen("somewords.txt", "r"); 
    if (fptr == NULL) { 
     fprintf(stderr, "Error reading file\n"); 
     exit(EXIT_FAILURE); 
    } 

    while ((read = getline(&line, &len, fptr)) != -1) { 
     lineno++; 
     word = strtok(line, delim); 
     while (word != NULL) { 
      for (size_t i = 0; i < ARRAYSIZE(words); i++) { 
       if (strcmp(word, words[i]) == 0) { 
        printf("Found matched word: %s, Line number: %zu\n", word, lineno); 
       } 
      } 
      word = strtok(NULL, delim); 
     } 
    } 

    free(line); 

    fclose(fptr); 

    return 0; 
} 
相關問題