【发布时间】:2020-11-19 04:04:06
【问题描述】:
我想将一些 printf() 从函数重定向到文件。
我不想改变整个功能。相反,我想要这样的东西:
void write_to_temp_File(struct s_28b_meta, struct s_db_entry);
file_name = "temp.txt";
handle_file = CreateFile(file_name, GENERIC_WRITE, 0,NULL, CREATE_ALWAYS, NULL);
//sanity check
if(handle_file == INVALID_HANDLE_VALUE)
printf("Could not open %s file, error %d\n", file_name, GetLastError());
//test check
test = GetHandleInformation(handle_file, lpdwFlags);
printf("The return value is %d, error %d\n", test, GetLastError());
//actually print into file
fprintf(handle_file, reader_print_db(byte **ptr_to_s_20b_parse_entries, 1024));
//clean close and exit
CloseHandle(handle_file);
return 0;
所以实际上,我想打开一个文件并将函数内所有打印的输出重定向到新创建的文件。
在 sysCall 中会是这样的:
text.exe > temp.txt
有什么好的方法吗?如何获得函数输出的长度,而不是像我这样的静态值?
谢谢你
【问题讨论】:
-
这与minimal reproducible example 相差甚远...我不明白
[f]printf是否在函数内部,或者您是否想将函数返回的内容写入文件,而不是如果你想写一个字符串或一堆单独的值。 -
您可以在函数中使用
fprintf并传递stdout或其他文件指针。printf和fprintf都返回写入的字节数。 -
一些
printf()打电话但不是其他人?重写整个函数来做你想做的事。 -
我明白你们的意思,但我在这里尝试做一些解决方法。