2013-01-20 9 views
-3

함수가 파일에서 텍스트를 반환하고 파일 이름이 해당 함수의 매개 변수를 통해 함수로 전달되는 C 프로그래밍에서 사용자 정의 함수를 갖고 싶습니다. 감사합니다. .텍스트 파일을 읽는 함수

이것은 텍스트를 변수에 추가해야하기 때문입니다. 이를 위해 다음 코드를 어떻게 수정합니까? 이것은 내가 어떻게 이런 일을 시도했지만 지금은 많은 오류가 발생합니다.

example1.c: In function ‘readfile’: 
example1.c:47:5: warning: passing argument 1 of ‘fopen’ makes pointer from integer without a cast [enabled by default] 
/usr/include/stdio.h:273:14: note: expected ‘const char * __restrict__’ but argument is of type ‘int’ 
example1.c:48:5: warning: implicit declaration of function ‘exit’ [-Wimplicit-function-declaration] 
example1.c:48:52: warning: incompatible implicit declaration of built-in function ‘exit’ [enabled by default] 
example1.c:56:5: warning: implicit declaration of function ‘malloc’ [-Wimplicit-function-declaration] 
example1.c:56:22: warning: incompatible implicit declaration of built-in function ‘malloc’ [enabled by default] 
example1.c:57:57: warning: incompatible implicit declaration of built-in function ‘exit’ [enabled by default] 
example1.c:61:59: warning: incompatible implicit declaration of built-in function ‘exit’ [enabled by default] 
example1.c:65:2: warning: return makes integer from pointer without a cast [enabled by default] 
example1.c:68:5: warning: implicit declaration of function ‘free’ [-Wimplicit-function-declaration] 
example1.c:68:5: warning: incompatible implicit declaration of built-in function ‘free’ [enabled by default] 
example1.c: At top level: 
example1.c:71:30: warning: initialization makes pointer from integer without a cast [enabled by default] 
example1.c:71:1: error: initializer element is not constant 

도와주세요.

char readfile (fname) { 
    FILE * pFile; 
    long lSize; 
    char * buffer; 
    size_t result; 

    pFile = fopen (fname , "rb"); 
    if (pFile==NULL) {fputs ("File error",stderr); exit (1);} 

    fseek (pFile , 0 , SEEK_END); 
    lSize = ftell (pFile); 
    rewind (pFile); 

    buffer = (char*) malloc (sizeof(char)*lSize); 
    if (buffer == NULL) {fputs ("Memory error",stderr); exit (2);} 

    result = fread (buffer,1,lSize,pFile); 
    if (result != lSize) {fputs ("Reading error",stderr); exit (3);} 

    return buffer; 
    fclose (pFile); 
    free (buffer); 
} 
AC_ALPHABET_t * input_text = readfile("infile"); 
+1

이를 수행하려면 편집기가 필요할 것입니다. 그리고 두 손 (또는 적어도 한 손) – wildplasser

+0

귀하가 직접 질문에 답변하셨습니다. 파일 이름을 인수로 전달하고 텍스트를 반환하십시오. 나머지는 'your'코드에 있습니다. – EarlOfEgo

+0

@EarlOfEgo 이 함수의 반환 유형은 int입니다. char를 사용하면 ok입니다. – rkrara

답변

1

일부 연구에서는이 작업을 쉽게 수행 할 수 있지만 파일 이름이 지정된 문자열을 반환하는 함수가 포함되도록 프로그램을 수정했습니다. 이 방법이 가장 쉽고, 좋으며, 가장 올바른 방법이 아닌 것 같지만 프로그램 수정을 요청했습니다.

#include <stdio.h> 
#include <stdlib.h> 

char *read_from_file(const char *filename) 
{ 
    long int size = 0; 
    FILE *file = fopen(filename, "r"); 

    if(!file) { 
     fputs("File error.\n", stderr); 
     return NULL; 
    } 

    fseek(file, 0, SEEK_END); 
    size = ftell(file); 
    rewind(file); 

    char *result = (char *) malloc(size); 
    if(!result) { 
     fputs("Memory error.\n", stderr); 
     return NULL; 
    } 

    if(fread(result, 1, size, file) != size) { 
     fputs("Read error.\n", stderr); 
     return NULL; 
    } 

    fclose(file); 
    return result; 
} 

int main(int argc, char **argv) 
{ 
    if(argc < 2) { 
     fputs("Need an argument.\n", stderr); 
     return -1; 
    } 

    char *result = read_from_file(argv[1]); 

    if(!result) return -1; 

    fputs(result, stdout); 
    free(result); 

    return 0; 
}    

샘플 실행 :

[[email protected] ~]$ echo "hello world" > some_file.txt 
[[email protected] ~]$ ./test some_file.txt 
hello world 
[[email protected] ~]$ 

나는 필요한 경우 프로그램을 언급하지만, 함수 참조를 찾는 것은 아마도 충분하다 할 수 있습니다.

+0

고마워, 내가 이것을 시도하고 알게됩니다. – rkrara

+0

스위트 룸에 약간의 수정을 한 후 감사합니다.이 경고와 같은 몇 가지 경고와 함께 작동합니다. example1.c : 'read_from_file'함수에서 : example1.c : 88 : 5 : 경고 : 암시 적으로 'malloc'함수의 암시 적 선언 [-Wimplicit-function -declaration] example1.c : 88 : 29 : 경고 : 내장 함수 'malloc'의 암시 적 선언이 호환되지 않음 [기본적으로 활성화 됨] – rkrara

+0

@user1733911 헤더를 포함 했습니까? 거기에 정의되어 있습니다. –

관련 문제