[英]Redirect output from function to File in C
我想将一些 printf() 从 function 重定向到文件。
我不想改变整个 function。 相反,我想要这样的东西:
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;
所以实际上,我想打开一个文件并将 function 内所有打印的 output 重定向到新创建的文件。
在 sysCall 中会是这样的:
text.exe > temp.txt
有没有一种好的方法,我怎样才能得到 function 的 output 的长度,而不是像我这样的 static 值?
谢谢你
使用 freopen 应该可以帮助你。
freopen("temp.txt", "a+", stdout);
使用 freopen() 所有 output 语句printf, putchar
被重定向到文件temp.txt
。 因此,此后任何printf()
语句都会将其 output 重定向到temp.txt
文件。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.