在Linux 3.0/C++下:
我想要一个执行以下操作的函数:
string f(string s)
{
string r = system("foo < s");
return r;
}
Run Code Online (Sandbox Code Playgroud)
显然上面的方法不起作用,但你明白了.我有一个字符串s,我想传递作为应用程序"foo"的子进程执行的标准输入,然后我想将其标准输出记录到字符串r然后返回它.
我应该使用linux系统调用或posix函数的组合?
可以stdout文件描述符不同1(STDOUT_FILENO)假设stdout不必修改的左值?
例如,可以freopen("/dev/null", "w", stdout)改变fileno(stdout)结果吗?
如何在CUnit中为打印到stdout的函数编写测试,以验证其输出?
要测试的示例函数:
void print()
{
printf("Hello world");
}
Run Code Online (Sandbox Code Playgroud)
它的单元测试应该以某种方式验证"Hello world"是否已打印到控制台:
void test_print()
{
// how to assert?
}
Run Code Online (Sandbox Code Playgroud)
我该怎么办呢?
假设我有这个C代码:
#include <stdio.h>
// Of course, these functions are simplified for the purposes of this question.
// The actual functions are more complex and may receive additional arguments.
void printout() {
puts("Hello");
}
void printhere(FILE* f) {
fputs("Hello\n", f);
}
Run Code Online (Sandbox Code Playgroud)
我正在编译为共享对象(DLL): gcc -Wall -std=c99 -fPIC -shared example.c -o example.so
然后我将它导入到在Jupyter或IPython笔记本中运行的Python 3.x中:
import ctypes
example = ctypes.cdll.LoadLibrary('./example.so')
printout = example.printout
printout.argtypes = ()
printout.restype = None
printhere = example.printhere
printhere.argtypes = (ctypes.c_void_p) …Run Code Online (Sandbox Code Playgroud) 我有一个简单的 C 扩展(参见下面的示例),有时使用 printf 函数进行打印。我正在寻找一种方法来包装对 C 扩展中的函数的调用,以便所有这些 printfs 将被重定向到我的 python 记录器。
你好ç:
#include <Python.h>
static PyObject* hello(PyObject* self)
{
printf("example print from a C code\n");
return Py_BuildValue("");
}
static char helloworld_docs[] =
"helloworld(): Any message you want to put here!!\n";
static PyMethodDef helloworld_funcs[] = {
{"hello", (PyCFunction)hello,
METH_NOARGS, helloworld_docs},
{NULL}
};
static struct PyModuleDef cModPyDem =
{
PyModuleDef_HEAD_INIT,
"helloworld",
"Extension module example!",
-1,
helloworld_funcs
};
PyMODINIT_FUNC PyInit_helloworld(void)
{
return PyModule_Create(&cModPyDem);
};
Run Code Online (Sandbox Code Playgroud)
设置.py:
from distutils.core import setup, Extension
setup(name = 'helloworld', …Run Code Online (Sandbox Code Playgroud) 我正在尝试使用C中的重定向将输入重定向到一个文件,然后将标准输出设置回打印到屏幕.有人能告诉我这段代码有什么问题吗?
#include <stdio.h>
#include <fcntl.h>
#include <unistd.h>
int main(int argc, char** argv) {
//create file "test" if it doesn't exist and open for writing setting permissions to 777
int file = open("test", O_CREAT | O_WRONLY, 0777);
//create another file handle for output
int current_out = dup(1);
printf("this will be printed to the screen\n");
if(dup2(file, 1) < 0) {
fprintf(stderr, "couldn't redirect output\n");
return 1;
}
printf("this will be printed to the file\n");
if(dup2(current_out, file) < 0) {
fprintf(stderr, "couldn't reset …Run Code Online (Sandbox Code Playgroud) 我想检索一条错误消息,解释 jvm 加载失败的原因。从此处提供的示例中:
http://java.sun.com/docs/books/jni/html/invoke.html
我提取了这个例子:
/* Create the Java VM */
res = JNI_CreateJavaVM(&jvm, (void**)&env, &vm_args);
if (res < 0) {
// retrieve verbose error here?
fprintf(stderr, "Can't create Java VM\n");
exit(1);
}
Run Code Online (Sandbox Code Playgroud)
在我的特定情况下,我在 vm_args 中提供了无效参数,并希望看到我在命令行上得到的信息:“无法识别的选项:-foo=bar”
在进一步测试中,jvm 似乎将我想要的消息放入 stdout 或 stderr。我相信我需要捕获 stdout 和 stderr 才能得到我正在寻找的错误(当然除非有更简单的方法)。我正在用 C++ 编码,所以如果有人可以展示一种将错误捕获到字符串流中的方法,那将是理想的。
谢谢,兰迪
我在C中编写了一个程序,它有一个处理可以通过文件指针传递给它的文件的函数.
void process_my_file(FILE *fptr, ...) {
/* do work */
}
Run Code Online (Sandbox Code Playgroud)
我想从标准输入读取一些输入并将其传递给我的函数,而不必将输入写入临时文件.是否可以通过文件指针参数传递它,就像它是一个文件一样,而不必将其写入磁盘?可以通过我可以添加到签名中的其他一些参数来完成吗?
我是新手,需要一些建议.提前致谢!
我知道正确使用freopen是为了省略分配,给出这篇文章:
freopen("/dev/tty","r", stdin);
我的问题是,我还应该检查返回值吗?我正在重新打开stdin并关闭它.例如:
if(freopen("/dev/tty","r", stdin)==NULL) {
fprintf(stderr, "Unable to redirect stdin from file\n");
exit(1);
}
Run Code Online (Sandbox Code Playgroud) 我编写了printf的实现 - myPrintf,它打印到stdout.我想验证它是否正常工作.为了检查打印输出的相关性,我想将它与我希望得到的char进行比较.如何编写代码以将stdout重定向到缓冲区,而不是使用>.
我只能使用printf!
我想将程序的输出重定向到文件。我怎样才能做到这一点?目前我的文件没有被创建,我只能将输出打印到我的控制台。
int fd[2];
int processId;
int output;
char filename[] = "output.txt";
if((output = open(filename, O_CREAT | O_WRONLY, S_IRUSR | S_IWUSR)) == -1){
fprintf(stderr, "Unable to create/open file '%s'\n", filename);
return 1;
}
if(pipe(fd) == -1){
fprintf(stderr, "Error creating pipe\n");
return 2;
}
if((processId = fork()) == -1){
fprintf(stderr, "Error forking\n");
return 3;
}
if(processId == 0){
int newFD = dup(STDOUT_FILENO);
char newFileDescriptor[2];
sprintf(newFileDescriptor, "%d", newFD);
dup2(fd[1], output);
close(fd[0]);
execl("./pipetest", "pipetest", newFileDescriptor, NULL);
}else{
close(fd[1]);
char c[10];
int r = read(fd[0], …Run Code Online (Sandbox Code Playgroud)