0

私は C が初めてで、ストリーム内の行を繰り返し呼び出して、検索文字列が含まれているかどうか、または null であるかどうかを確認しようとしています。このチェックを行う方法がわかりません。[警告] ポインターと整数の比較、または [警告] 代入により、これを試みるたびにキャストなしで整数からポインターが作成されるという警告が表示されます。誰でも助けることができますか?ありがとう。

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

int main(int argc, char *argv[]) {

    FILE *fpntr;
    char *file_pathname, *first_line;

      if (argc != 2) {
         fprintf(stderr, "usage: %s FILE\n", argv[0]);
         return EXIT_FAILURE;
      }
   file_pathname = argv[1];

   if ((fpntr = fopen(file_pathname, "r")) == NULL ) {
        fprintf(stderr, "Error opening file %s: %s\n", file_pathname, strerror(errno));
    return EXIT_FAILURE;
    } else {
       grep_stream();
       fclose(fpntr);
    }
     return EXIT_SUCCESS;
     }

 int grep_stream(FILE *fpntr, char *string, char *file_pathname) {
          //warning is on next line
     while ((? = get_next_line(fpntr)) == NULL ) {
         perror("Error reading line");
         exit(EXIT_FAILURE);
}
elseif()
{
    printf("First line in : %s \n %s", file_pathname, string);
}

}

    char *get_next_line(FILE *fpntr) {
   char *buff = malloc(101);
   int pos = 0;
   int next;

   while ((next = fgetc(fpntr)) != '\n' && next != EOF) {

    buff[pos++] = next;

    }
    buff[pos] = '\0';
     if (buff != NULL ) {
    return buff;
     } else
      return NULL ;

          }
4

4 に答える 4

1

C コードは最初から最後までコンパイルされることに注意してください。関数は、行が読み取らget_next_lineれるまでに宣言されていません。while

get_next_lineの定義を の前に移動するか、次のようmainに前方宣言します。

char *get_next_line(FILE *fpntr);

予め。エラーではなく警告が表示される理由は、宣言されていない関数が返されるintと想定されており、それらのパラメーターについて何も想定されていないためです。つまり、それらは型を持っていint()ます。

また、あなたとあなたの質問に答える (またはあなたと協力する) 人のために、コードを適切にフォーマットしてください。

于 2013-02-18T02:49:58.840 に答える
0
#include <assert.h> // I'm too dumb to program without assertions!
#include <stdio.h>
#include <errno.h>
#include <string.h>
#include <stdlib.h>
//#include <unistd.h>  I prefer stdlib.h, couldn't see any need for non-portable header...

#define MAX_LINE    (101)       // define funky constants in one place for easy changing later.

// declare functions before using them
void grep_stream(FILE *fpntr, char *file_pathname);
char *get_next_line(FILE *fpntr);


int main(int argc, char *argv[]) {

    FILE *fpntr;
    char *file_pathname;

    if (argc != 2) {
        fprintf(stderr, "usage: %s FILE\n", argv[0]);
        return EXIT_FAILURE;
        }
    file_pathname = argv[1];

    if ((fpntr = fopen(file_pathname, "r")) == NULL ) {
        fprintf(stderr, "Error opening file %s: %s\n", file_pathname, strerror(errno));
        return EXIT_FAILURE;
        }
    else {
        grep_stream(fpntr, file_pathname);
        fclose(fpntr);
        }
    return EXIT_SUCCESS;
    }

void grep_stream(FILE *fpntr, char *file_pathname) {
    char*   line;
    int     got_first = 0;

    assert(fpntr);
    assert(file_pathname);  // I worry the guy who wrote main() might be an idiot like me!

    //warning is on next line [not anymore!]
    while ((line = get_next_line(fpntr)) != NULL ) {
        if(!got_first) {
            printf("First line in : %s \n%s\n", file_pathname, line);
            got_first = 1;
            }
        // if we're not going to use it, let's not leak memory
        free(line);
        }
    }


char *get_next_line(FILE *fpntr) {
    char *buff = malloc(MAX_LINE);
    int pos = 0;
    int next;

    assert(buff != NULL);   // wouldn't it be nice to know malloc() worked?
    while ((next = fgetc(fpntr)) != '\n' && next != EOF) {
        buff[pos++] = (char)next;
        assert(pos < (MAX_LINE-1)); // don't want to be right back on SO with a seg fault, eh?
        }
    buff[pos] = '\0';

    if(next == EOF) {
        free(buff);
        buff = NULL;
        }

    return buff;
    }
于 2013-02-18T06:12:00.993 に答える
0

整数のポインターに * を追加して、ポインターから整数に変換します

于 2013-02-18T02:09:07.240 に答える
0

...ストリーム内で繰り返しラインを呼び出そうとしています...

なぜ使用しないのfgets()ですか?

次に、文字列内の部分文字列を一致させるには、次を使用できますstrstr()

車輪を再発明する代わりに、標準の C ライブラリを使用してください。それは通常その日を節約します。

于 2013-02-18T02:32:02.993 に答える