2016-06-26 24 views
0

私の質問は、n個のファイルが与えられた一定量のファイルのみを読み込もうとしていることです。Cのファイルから特定の量の行を読み込む方法

例えば、私はその後、内側に以下の内容を持つ2つのファイル

TEST1を持っている:

猫は

test2はアップル

をオフに走っ:

少年が家に帰った

をオフに走った:猫ではない

test1のは

をオフに走っ:個の

りんごは

私は出力が

test1のようにしたい赤

test2:リンゴは赤色です

これは私がこれまでに書いたコードです:

#include <stdio.h> 
#include <string.h> 
int main (int argc, char ** argv) 
{ 
    extern int searcher(char * name, char*search,int amount); 
    while(argc != 0){ 
    if(argv[argc] !=NULL) 
    if(searcher(argv[argc],"a",1)) break; 
    argc-- ; 
    } 
} 

int searcher(char * name, char*search,int amount){ 

FILE *file = fopen (name, "r"); 
int count = 0; 

if (file != NULL) { 
    char line [1000]; 
while(fgets(line,sizeof line,file)!= NULL && count != amount) 
{ 
    if(strstr(line,search) !=NULL){ 
    count++; 
    if(count == amount){ 
     return(count); 
    } 
    printf("%s:%s\n", line,name); 
    } 
} 

fclose(file); 
}else { 
    perror(name); //print the error message on stderr. 
    } 
return(0); 
} 
+0

はfgets'がで行を読み込み、 '覚えておいてください(カウンター

+0

あなたは何を意味するのか教えてもらえますか?上記のコードに編集を追加してください。 –

+0

もちろん、例を挙げる前に、各ファイルから何本の行を読みたいのですか?または各ファイル内で用語を検索していますか?あなたが検索をしようとしているようですが(それは問題ありません)、ファイルから 'X'個の行を厳密に読み取ることとは別に、離れています。あなたは両方をする必要がありますか? –

答えて

1

コメントに引き続き、あなたがfgetsに含ま末尾newlineを削除する必要があります注意して、次のような何かを行うことができます:

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

enum { MAXC = 1000 }; 

int searcher (char *name, char *search, int amount); 
void rmlf (char *s); 

int main (int argc, char **argv) 
{ 
    int i; 

    for (i = 1; i < argc; i++) 
     if (searcher (argv[i], "a", 1)) 
      break; 

    return 0; 
} 

int searcher (char *name, char *search, int amount) 
{ 
    FILE *file = fopen (name, "r"); 
    int count = 0; 

    if (!file) { 
     fprintf (stderr, "error: file open failed '%s'.\n", name); 
     return 0; 
    } 

    char line[MAXC] = ""; 
    while (count < amount && fgets (line, MAXC, file)) { 
     rmlf (line);     /* strip trailing \n from line */ 
     if (strstr (line, search)) { 
      count++; 
      printf ("%s: %s\n", name, line); 
     } 
    } 

    fclose (file); 
    return count == amount ? count : 0; 
} 

/** stip trailing newlines and carraige returns by overwriting with 
* null-terminating char. str is modified in place. 
*/ 
void rmlf (char *s) 
{ 
    if (!s || !*s) return; 
    for (; *s && *s != '\n'; s++) {} 
    *s = 0; 
} 

例入力ファイル

$ cat test1 
A cat ran off 
Apple 

$ cat test2 
The boy went home 
Apples are red 

使用例/出力

あなたはあなたのような出力になってしまいますので、あなたのファイルは、逆に処理されargc--で反復理解:処理する:

$ ./bin/searcher test2 test1 
test1: A cat ran off 

$ ./bin/searcher test1 test2 
test2: Apples are red 

ノートファイルは順番にwhile (argc--)の代わりにfor (i = 1; i < argc; i++)のようにしてください。さらに質問がある場合はお知らせください。

すべてのファイルが処理されmainで代わりwhileforループに変更して探すために発生回数、として10を入力し、例えば:

$ ./bin/searcher test1 test2 
test1: A cat ran off 
test2: Apples are red 
+0

新しい行を削除することがなぜ重要ですか? –

+0

私の 'rmlf'をコメントアウトして': '' '' fgets' *があなたの ''行 'に改行*を読み込んでいることを覚えておいてください。 'name'と' line'を印刷するとどうなりますか? –

+0

enumは本当に必要ですか? char size [1000]、sizeof sizeのようなものを使うことはできません。 –

関連する問題