是否可以在C中伪造诸如stdin之类的文件流? [英] Is it possible to fake a file stream, such as stdin, in C?

查看:87
本文介绍了是否可以在C中伪造诸如stdin之类的文件流?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在一个没有文件系统的嵌入式系统上工作,我需要执行一些程序,这些程序从通过命令(例如参数)指定的文件中或直接从stdin中获取文件的输入数据。

I am working on an embedded system with no filesystem and I need to execute programs that take input data from files specified via command like arguments or directly from stdin.

我知道可以使用来自此答案的方法使用二进制文件来烘烤文件数据: C / C ++:将资源文件静态添加到可执行文件/库中,但是目前,我需要重写所有程序,以新的方式访问数据。

I know it is possible to bake-in the file data with the binary using the method from this answer: C/C++ with GCC: Statically add resource files to executable/library but currently I would need to rewrite all the programs to access the data in a new way.

例如,是否可以烘焙文本文件,并在运行程序时使用指向stdin的伪造文件指针来访问它?

Is it possible to bake-in a text file, for example, and access it using a fake file pointer to stdin when running the program?

推荐答案

如前所述,您可以使用POSIX fmemopen 函数。您需要提供一个libc,例如 musl-libc 或可能 glibc 。出于基准测试的目的,您可以在硬件上安装一些类似于Linux的小型操作系统,例如 uclinux

As commented, you could use the POSIX fmemopen function. You'll need a libc providing it, e.g. musl-libc or possibly glibc. BTW for benchmarking purposes you might install some tiny Linux-like OS on your hardware, e.g. uclinux

这篇关于是否可以在C中伪造诸如stdin之类的文件流?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆