Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/65.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C编程,调用函数_C - Fatal编程技术网

C编程,调用函数

C编程,调用函数,c,C,我是C语言的新手,我正在尝试迭代地调用流中的line,并检查它是否包含我的搜索字符串,或者它是否为null。我不知道如何进行此检查,每当我尝试执行此操作时,都会收到一条警告,说明指针和整数之间的[warning]比较,或者[warning]赋值从整数生成指针而不进行强制转换。有人能帮忙吗?谢谢 #include <stdio.h> #include <errno.h> #include <string.h> #include <stdlib.h>

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

#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 ;

          }
#包括
#包括
#包括
#包括
#包括
int main(int argc,char*argv[]){
文件*fpntr;
char*文件路径名,*第一行;
如果(argc!=2){
fprintf(stderr,“用法:%s文件\n”,argv[0]);
返回退出失败;
}
文件_pathname=argv[1];
if((fpntr=fopen(文件路径名,“r”))==NULL){
fprintf(stderr,“打开文件%s时出错:%s\n”,文件路径名,strerror(errno));
返回退出失败;
}否则{
grep_stream();
fclose(fpntr);
}
返回退出成功;
}
int grep_流(文件*fpntr,字符*string,字符*FILE_路径名){
//警告在下一行
while((?=get_next_line(fpntr))==NULL){
perror(“错误读取线”);
退出(退出失败);
}
埃尔塞夫()
{
printf(“第一行:%s\n%s”,文件路径名,字符串);
}
}
char*get\u下一行(文件*fpntr){
char*buff=malloc(101);
int pos=0;
int-next;
而((next=fgetc(fpntr))!='\n'&&next!=EOF){
buff[pos++]=next;
}
buff[pos]='\0';
如果(buff!=NULL){
返回buff;
}否则
返回NULL;
}

将*添加到整数指针,将其从指针转换为整数

。。。我正在尝试迭代调用流中的line

为什么不使用
fgets()

其次,要匹配字符串中的子字符串,可以使用
strstr()


请使用标准C库,而不是重新发明轮子。它通常可以节省时间。

请记住,C代码是自上而下编译的。在读取
行时,函数
get\u next\u line
不会在
时声明

get_next_line
的定义移动到
main
之前,或通过以下方式向前声明:

char *get_next_line(FILE *fpntr);
事先。您得到警告而不是错误的原因是假定未声明的函数返回
int
,并且不对其参数进行任何假设。也就是说,它们的类型为
int()

另外,为了你自己和那些将回答你的问题(或与你一起工作)的人,正确地格式化你的代码。

#include//我太笨了,没有断言就无法编程!
#包括
#包括
#包括
#包括
//#包括我更喜欢stdlib.h,看不出任何不可移植头的需要。。。
#define MAX_LINE(101)//在一个地方定义funky常量,以便以后更改。
//在使用函数之前声明它们
void grep_流(文件*fpntr,字符*FILE_路径名);
char*get\u next\u行(文件*fpntr);
int main(int argc,char*argv[]){
文件*fpntr;
char*文件路径名;
如果(argc!=2){
fprintf(stderr,“用法:%s文件\n”,argv[0]);
返回退出失败;
}
文件_pathname=argv[1];
if((fpntr=fopen(文件路径名,“r”))==NULL){
fprintf(stderr,“打开文件%s时出错:%s\n”,文件路径名,strerror(errno));
返回退出失败;
}
否则{
grep_流(fpntr,文件路径名);
fclose(fpntr);
}
返回退出成功;
}
void grep_流(文件*fpntr,字符*FILE_路径名){
字符*行;
int got_first=0;
断言(fpntr);
assert(file_pathname);//我担心编写main()的人可能是像我这样的白痴!
//警告在下一行[不再是!]
while((line=get\u next\u line(fpntr))!=NULL){
如果(!先得到你){
printf(“第一行在:%s\n%s\n”,文件路径名,行);
got_first=1;
}
//如果我们不打算使用它,就不要泄露内存
自由线;
}
}
char*get\u下一行(文件*fpntr){
char*buff=malloc(最大线);
int pos=0;
int-next;
assert(buff!=NULL);//知道malloc()工作不是很好吗?
而((next=fgetc(fpntr))!='\n'&&next!=EOF){
buff[pos++]=(char)next;
assert(pos<(MAX_LINE-1));//不想在出现seg故障的情况下重新启动,是吗?
}
buff[pos]='\0';
如果(下一个==EOF){
免费(buff);
buff=NULL;
}
返回buff;
}

首先,请修复格式请提供:编译并运行(或应该运行)的代码示例,以及收到错误/警告的行号。其次,引用确切的消息,并确定标记的确切行。在提供问题的详细描述后,同时尝试识别相关标记:)请在编译代码后…
elseif()
根本不起作用,无论是函数调用还是拼写错误的
else if
。什么指针?我在OP的代码中没有看到任何指向整数的指针。
#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;
    }