함수가 파일에서 텍스트를 반환하고 파일 이름이 해당 함수의 매개 변수를 통해 함수로 전달되는 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");
이를 수행하려면 편집기가 필요할 것입니다. 그리고 두 손 (또는 적어도 한 손) – wildplasser
귀하가 직접 질문에 답변하셨습니다. 파일 이름을 인수로 전달하고 텍스트를 반환하십시오. 나머지는 'your'코드에 있습니다. – EarlOfEgo
@EarlOfEgo 이 함수의 반환 유형은 int입니다. char를 사용하면 ok입니다. – rkrara