C 仅使用 stdio 库将文件内容读入字符串
C Read file content into string just with stdio libary
我努力尝试将文件内容读入字符串 (char*)。
我只需要使用 stdio.h 库,所以我不能用 malloc 分配内存。
如何读取文件的所有内容并将其 return 成字符串?
我会尝试回答你的问题。不过请记住 - 我对 C 还是很陌生,所以可能会有更好的解决方案,在这种情况下也请告诉我! :)
编辑:还有……
这是我的看法...
您需要知道要存储在字符串中的文件的大小。
更准确地说 - 在我提供的示例解决方案中,您需要知道输入文件中有多少个字符。
您对 malloc
所做的一切都是在 'heap' 上动态分配内存。我想你已经知道了...
因此,无论您的 string
在内存中的哪个位置(堆栈或堆),您都需要知道 string
必须有多大,以便输入文件的所有内容都适合它。
这是一个快速的伪代码和一些代码,可以解决您的问题...希望这对您有所帮助,如果有更好的方法,我愿意学习更好的方法,和平!
伪代码:
打开输入文件进行读取
找出文件的大小
forward seek file position indicator to the end of the file
get the total number of bytes (chars) in infile
rewind file position indicator back to the start
(because we will be reading from it again)
声明一个 char 数组,大小为 - infile 中所有计数的字符 + 1 for '\0'
在字符串的末尾。
将infile的所有字符读入数组
用'\0'终止你的字符串
关闭输入文件
这是一个简单的程序,可以执行此操作并打印您的字符串:
#include <stdio.h>
int main(void)
{
// Open input file
FILE *infile = fopen("hello.py", "r");
if (!infile)
{
printf("Failed to open input file\n");
return 1;
}
////////////////////////////////
// Getting file size
// seek file position indicator to the end of the file
fseek(infile, 0L, SEEK_END);
// get the total number of bytes (chars) in infile
int size = ftell(infile); // ask for the position
// Rewind file position indicator back to the start of the file
rewind(infile);
//////////////////////////////////
// Declaring a char array, size of - all the chars from input file + 1 for the '[=10=]'
char str[size + 1];
// Read all chars of infile in to the array
fread(&str, sizeof(char), size, infile);
// Terminate the string
str[size] = '[=10=]'; // since we are zero indexed 'size' happens to be the last element of our array[size + 1]
printf("%s\n", str);
// Close the input file
fclose(infile);
// The end
return 0;
}
我努力尝试将文件内容读入字符串 (char*)。 我只需要使用 stdio.h 库,所以我不能用 malloc 分配内存。
如何读取文件的所有内容并将其 return 成字符串?
我会尝试回答你的问题。不过请记住 - 我对 C 还是很陌生,所以可能会有更好的解决方案,在这种情况下也请告诉我! :)
编辑:还有……
这是我的看法... 您需要知道要存储在字符串中的文件的大小。 更准确地说 - 在我提供的示例解决方案中,您需要知道输入文件中有多少个字符。
您对 malloc
所做的一切都是在 'heap' 上动态分配内存。我想你已经知道了...
因此,无论您的 string
在内存中的哪个位置(堆栈或堆),您都需要知道 string
必须有多大,以便输入文件的所有内容都适合它。
这是一个快速的伪代码和一些代码,可以解决您的问题...希望这对您有所帮助,如果有更好的方法,我愿意学习更好的方法,和平!
伪代码:
打开输入文件进行读取
找出文件的大小
forward seek file position indicator to the end of the file
get the total number of bytes (chars) in infile
rewind file position indicator back to the start (because we will be reading from it again)
声明一个 char 数组,大小为 - infile 中所有计数的字符 + 1 for '\0' 在字符串的末尾。
将infile的所有字符读入数组
用'\0'终止你的字符串
关闭输入文件
这是一个简单的程序,可以执行此操作并打印您的字符串:
#include <stdio.h>
int main(void)
{
// Open input file
FILE *infile = fopen("hello.py", "r");
if (!infile)
{
printf("Failed to open input file\n");
return 1;
}
////////////////////////////////
// Getting file size
// seek file position indicator to the end of the file
fseek(infile, 0L, SEEK_END);
// get the total number of bytes (chars) in infile
int size = ftell(infile); // ask for the position
// Rewind file position indicator back to the start of the file
rewind(infile);
//////////////////////////////////
// Declaring a char array, size of - all the chars from input file + 1 for the '[=10=]'
char str[size + 1];
// Read all chars of infile in to the array
fread(&str, sizeof(char), size, infile);
// Terminate the string
str[size] = '[=10=]'; // since we are zero indexed 'size' happens to be the last element of our array[size + 1]
printf("%s\n", str);
// Close the input file
fclose(infile);
// The end
return 0;
}