0

我是 C 的新手,我正在尝试迭代地调用流中的 line 并检查它是否包含我的搜索字符串或者它是否为空。我不知道如何进行此检查,我收到一条警告,指出指针和整数之间的 [警告] 比较或 [警告] 赋值使指针从整数中生成,而无需强制转换,每当我尝试执行此操作时。谁能帮忙?谢谢。

#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

... i am trying to iteratively call line in stream...

Why not use fgets()?

Secondly, to match a substring in a string, you can use strstr()

Please use standard C library instead of re-inventing the wheel. It saves the day usually.

于 2013-02-18T02:32:02.993 回答
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 回答