0

I am new to C and i am trying to iteratively call line in stream and check to see if it contains my search string or if it is null. I cant figure out how to make this check, i get a warning saying [Warning] comparison between pointer and integer or [Warning] assignment makes pointer from integer without a cast whenever i try and do this. can anyone help? thanks.

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

          }
5
  • 2
    first up, pls fix formatting Commented Feb 18, 2013 at 2:04
  • 3
    Please provide: a code sample that compiles and runs (or is supposed to), and the line numbers where you're getting errors/warnings. Commented Feb 18, 2013 at 2:05
  • Second, quote the EXACT message, and identify the EXACT line that's being flagged. Commented Feb 18, 2013 at 2:05
  • And after providing detailed description of your problem, try to identify relevant tags as well :) Commented Feb 18, 2013 at 2:06
  • Please post compiling code...elseif() simply doesn't work, whether it is a function call or a misspelled else if. Commented Feb 18, 2013 at 3:36

4 Answers 4

1

Remember that C code is compiled top-to-bottom. The function get_next_line isn't declared by the time the while line is read.

Either move get_next_line's definition to before main's, or forward-declare it by saying:

char *get_next_line(FILE *fpntr);

beforehand. The reason that you're getting a warning instead of an error is that undeclared functions are assumed to return int and no assumptions are made about their parameters. That is, they have the type int().

Also, properly format your code for both your sake and of those who will be answering your questions (or working with you.)

Sign up to request clarification or add additional context in comments.

Comments

0

add a * to the pointer of integer to convert it from pointer to integer

1 Comment

What pointer? I don't see any pointer to integer in OP's code.
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.

Comments

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.