gpt4 book ai didi

c - 读取文本文件的函数

转载 作者:行者123 更新时间:2023-11-30 21:04:20 26 4
gpt4 key购买 nike

我想在 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;
}

示例运行:

[michael@michael-desktop ~]$ echo "hello world" > some_file.txt
[michael@michael-desktop ~]$ ./test some_file.txt
hello world
[michael@michael-desktop ~]$

如果需要,我可以评论程序,但查找函数引用可能就足够了。

关于c - 读取文本文件的函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14424439/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com