将文件句柄作为参数传递给函数? [英] Passing a File Handle as a parameter to a function?

查看:441
本文介绍了将文件句柄作为参数传递给函数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这可能吗?我想在一个函数中打开输入文件,在另一个函数中处理数据,然后将其传递回去.
另外,如何设置程序以将输出打印到打印机?
谢谢. jimbob

Is this possible? I would like to open an input file in one function, operate on the data in another and then pass it back.
Also, how do I set up a program to print the output to a printer?
Thanks. jimbob

推荐答案

FILE *fhandle;
int my_function (FILE *my_file_handle){
   ...
}
fhandle = fopen (argv[1],"w");
my_function (fhandle);




另请参见 http://www.linuxquestions.org [




see also http://www.linuxquestions.org[^]


这不仅是可能的,而且在许多情况下,这样做对于软件重用和抽象是一件好事:您可以例如,需要从文件中提取一组写操作.这样,您可以在不同文件上使用相同的写入方法,并在同一文件上组合不同的写入方法.

但是,不必传递回",因为您确实想在同一功能中打开和关闭文件.

—SA
Not only this is possible, but in many cases it is good to do so for software reuse and abstraction: you may need to abstract, for example, a set of write operations from file. In this way, you can use the same writing method on different files and combine different writing methods on the same file.

There is not need to "pass in back" though, because you really want to open and close file in the same function.

—SA


这篇关于将文件句柄作为参数传递给函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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