2013-02-18 4 views
0

저는 C 언어에 익숙하며 반복적으로 스트림을 호출하고 검색 문자열이 포함되어 있는지 또는 null인지 확인하려고합니다. 나는이 검사를하는 방법을 알지 못한다. 포인터와 정수 사이의 [경고] 경고를 얻는다. 또는 [경고] 할당은 시도 할 때마다 캐스트가없는 정수로부터 포인터를 만든다. 누구든지 도와 줄 수 있습니까? 감사.C 프로그래밍, 함수 호출

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

      } 
+2

를 먼저 제공하십시오 PLS –

+3

서식을 수정 (또는 당신과 함께 작동합니다.) : 코드 샘플을 그 컴파일 및 실행 (또는 예정), 오류/경고를받는 행 번호. – congusbongus

+0

둘째, EXACT 메시지를 인용하고 플래그가 지정된 EXACT 행을 식별하십시오. –

답변

0

+0

어떤 포인터입니까? OP 코드에서 정수에 대한 포인터를 볼 수 없습니다. – LihO

0

... 나는 반복적으로 스트림 라인을 호출하려고 정수로 포인터로 변환하는 정수의 포인터에 *를 추가 ...

fgets()을 사용하지 않으시겠습니까? 둘째

, 당신은 strstr()

가 다시 발명 바퀴를 대신 표준 C 라이브러리를 사용하십시오 사용할 수있는 문자열에서 문자열을 일치합니다. 보통 하루를 절약합니다.

1

C 코드는 맨 위에서 아래로 컴파일됨을 기억하십시오. 함수 get_next_linewhile 행을 읽을 때 선언되지 않습니다.

의 'main 이전에의 정의'의 get_next_line를 이동하거나, 말을하여 미래 선언

char *get_next_line(FILE *fpntr); 

을 사전에. 오류가 아니라 경고를받는 이유는 선언되지 않은 함수가 int을 반환한다고 가정하고 매개 변수에 대한 가정이 없기 때문입니다. 즉, 유형은 int()입니다. 또한

, 제대로 모두 당신을 위해 당신의 질문에 대답 할 것이다 사람들의 코드를 포맷

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