C 读取文本文件的函数

C 读取文本文件的函数,c,file-handling,C,File Handling,我希望在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 d

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

通过一些研究,这应该很容易做到,但我已经修改了您的程序,使其具有一个函数,可以返回给定文件名的字符串。我想这不是最简单、最好或最正确的方法,但你要求修改你的程序

#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);
        fclose(file);
        return NULL;
    }

    if(fread(result, 1, size, file) != size) {
        fputs("Read error.\n", stderr);
        fclose(file);
        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;
}               

如果需要,我可以对程序进行注释,但查找函数引用可能就足够了。

您可能需要一个编辑器来完成这项工作。用两只手(或至少一只手)回答你的问题。将文件名作为参数传递并返回文本。剩下的在“你的”代码中…@EarlOfEgo这个函数的返回类型是int,如果我使用chart可以吗这里有很多错误。我建议你先学习基础知识。阅读一些c语言书籍,或者尝试fname必须是一个
char*
,然后c必须知道您使用的每个库函数所期望的参数类型;ie添加一些带有正确头文件的
#包括
——为此,请在linux终端(或cygwin)或google“C exit manpage”中执行
man 3 exit
,非常感谢,我会尝试一下并让大家知道。谢谢在对我的代码进行了一些修改之后,它可以在很少出现警告的情况下工作,比如example1.c:In函数“read_from_file”:example1.c:88:5:warning:函数“malloc”的隐式声明[-Wimplicit函数声明]示例1.c:88:29:警告:内置函数“malloc”[默认启用]@user1733911的隐式声明不兼容,是否包含头?它的定义就在那里。
[michael@michael-desktop ~]$ echo "hello world" > some_file.txt
[michael@michael-desktop ~]$ ./test some_file.txt
hello world
[michael@michael-desktop ~]$