我希望在 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");