如何在C文件*结构得到一个文件名 [英] How to get a file's name from FILE* struct in C
本文介绍了如何在C文件*结构得到一个文件名的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
是否有可能在C检索文件的名字?如果一个文件像下面这样创建:
Is it possible to retrieve a file's name in C? If a file is created like the following:
FILE *f = fopen ("foo.txt", "r");
与foo.txt的来自变量超出范围。有没有一种方法,从FILE结构找回?
And the "foo.txt" comes from a variable out of scope. Is there a way to retrieve it from the FILE struct?
推荐答案
您的无法的实际上是从检索文件名称; 文件
对象。
You can't in fact retrieve the file names from your FILE
objects.
不过,如果被传递给名称的fopen
对你很重要,而该变量超出范围是什么原因,你总是可以换一个 FILE *
在结构,即
However, if the name being passed to fopen
is important to you, and that variable is out of scope for whatever reason, you could always wrap a FILE*
in a struct, ie.
struct file_with_name {
FILE *f;
char *name; /* OR char name[MAX] */
}
至少这就是我想要做的,但要看是什么你实际上是试图做的。
At least that's what I'd do but it depends on what you're actually trying to do.
这篇关于如何在C文件*结构得到一个文件名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文