我想在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
你自己回答了你的問題。將文件名作爲參數傳遞並返回文本。其餘的是「你的」代碼... – EarlOfEgo
@EarlOfEgo 這個函數的返回類型爲int是確定的,如果我使用CHAR – rkrara