我们调用了一个库来读取文本,该库API仅接受FILE*
指针。它实际上是通过内部调用 fread()
来读取文件文本的。
但是我们还需要使用这个库从 char*
字符串而不是 FILE*
读取文本。
当然,我们可以将 char*
字符串写入临时文件,但由于某些原因我们不允许这样做...
怎么办?谢谢!!
最佳答案
查看fmemopen
fmemopen() 函数应将 buf 参数给出的缓冲区与流关联起来。
#include <stdio.h>
static char buffer[] = "foobar";
int main (void)
{
FILE *stream;
stream = fmemopen (buffer, strlen (buffer), "r");
/* You got a FILE* pointer, you can call your function here :-) */
}
关于c - 如何通过fread函数读取char*字符串?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12420845/