[英]What is the correct way to declare and use a FILE * pointer in C/C++?
What is the correct way to declare and use a FILE * pointer in C/C++?在 C/C++ 中声明和使用 FILE * 指针的正确方法是什么? Should it be declared global or local?
应该声明为全局的还是局部的? Can somebody show a good example?
有人可以举一个很好的例子吗?
It doesn't matter at all whether it's local or global.无论是本地的还是全球的,都无关紧要。 The scope of the file pointer has nothing to do with its use.
文件指针的scope与其用途无关。
In general, it's a good idea to avoid global variables as much as possible.一般来说,尽可能避免使用全局变量是个好主意。
Here's a sample showing how to copy from input.txt
to output.txt
:这是一个示例,展示了如何从
input.txt
复制到output.txt
:
#include <stdio.h>
int main(void) {
FILE *fin, *fout; int c;
// Open both files, fail fast if either no good.
if ((fin = fopen("input.txt", "r")) == NULL) {
fprintf(stderr, "Cannot read from input.txt");
return 1;
}
if ((fout = fopen("output.txt", "w")) == NULL) {
fprintf(stderr, "Cannot write to output.txt");
fclose(fin);
return 1;
}
// Transfer character by character.
while ((c = fgetc(fin)) >= 0) {
fputc (c, fout);
}
// Close both files and exit.
fclose(fin);
fclose(fout);
return 0;
}
It's just an ordinary pointer like any other.它只是一个普通的指针,和其他指针一样。
FILE *CreateLogFile()
{
return fopen("logfile.txt","w"); // allocates a FILE object and returns a pointer to it
}
void UsefulFunction()
{
FILE *pLog = CreateLogFile(); // it's safe to return a pointer from a func
int resultsOfWork = DoSomeWork();
fprintf( pLog, "Work did %d\n", resultsOfWork ); // you can pass it to other functions
fclose( pLog ); // just be sure to clean it up when you are done with fclose()
pLog = NULL; // and it's a good idea to overwrite the pointer afterwards
// so it's obvious you deleted what it points to
}
Here is the first hit on google for "file io in c"这是谷歌上第一次点击“文件 io in c”
http://www.cs.bu.edu/teaching/c/file-io/intro/ http://www.cs.bu.edu/teaching/c/file-io/intro/
Here is the third hit from gamedev with more of a C++ slant这是 gamedev 的第三次命中,更多的是 C++ 倾斜
http://www.gamedev.net/reference/articles/article1127.asp http://www.gamedev.net/reference/articles/article1127.asp
You declare the pointer in the scope that you need it.您在 scope 中声明您需要它的指针。
int main(void)
{
char c;
FILE *read;
read = fopen("myfile", "r"); // opens "myfile" for reading
if(read == NULL)
{
perror("Error: could not open \"myfile\" for reading.\n");
exit(1);
}
c = fgetc(read);
fclose(read);
printf("The first character of myfile is %c.\n", c);
return 0;
}
You're perfectly allowed to declare global filehandles if you like, just like any other variable, but it may not be recommended.如果您愿意,您完全可以声明全局文件句柄,就像任何其他变量一样,但可能不推荐这样做。
This is the C way.这是 C 方式。 C++ can use this, but I think there's a more C++ friendly way of doing it.
C++ 可以使用这个,但我认为有一个更 C++ 友好的方式来做它。 As a note, I hate it when questions are marked C/C++, because C and C++ are not the same language and do not work the same.
请注意,当问题标记为 C/C++ 时,我讨厌它,因为 C 和 C++不是同一种语言,并且工作方式不同。 C++ has a lot of different ways to do things that C doesn't have, and they may be easier for you to do in the context of C++ but are not valid C.
C++ has a lot of different ways to do things that C doesn't have, and they may be easier for you to do in the context of C++ but are not valid C. So while this will work for either language, it's not what you want if you predominantly use C++.
因此,虽然这适用于任何一种语言,但如果您主要使用 C++,这不是您想要的。
EDIT: Added some error checking.编辑:添加了一些错误检查。 Always use error checking in your code.
始终在代码中使用错误检查。
First, keep in mind that a file pointer (and the associated allocated structure) is based on the lower level open() read() write() calls.首先,请记住文件指针(和相关的分配结构)基于较低级别的 open() read() write() 调用。 The associated file descriptor (obtained by fileno(file_pointer) is the least interesting thing, but something you might want to watch your scope with.
关联的文件描述符(由 fileno(file_pointer) 获得是最不有趣的东西,但你可能想用它来观察你的 scope。
If your going to declare a file pointer as global in a module, its usually a very good idea to keep it static (contained within that module / object file).如果您要在模块中将文件指针声明为全局文件,那么将其保留为 static(包含在该模块/object 文件中)通常是一个非常好的主意。 Sometimes this is a little easier than storing it in a structure that is passed from function to function if you need to write something in a hurry.
有时这比将它存储在从 function 传递到 function 的结构中要容易一些,如果你需要匆忙写一些东西的话。
For instance, (bad)例如,(坏)
#include <stdio.h>
#include ...
#define MY_LOG_FILE "file.txt"
FILE *logfile
Better done as:更好的做法是:
#include <stdio.h>
#define MY_LOG_FILE "file.txt"
static FILE *logfile;
int main(void)
{
UNLESS, you need several modules to have access to that pointer, in which case you're better off putting it in a structure that can be passed around.除非,您需要几个模块才能访问该指针,在这种情况下,您最好将它放在一个可以传递的结构中。
If its needed only in one module, consider declaring it in main() and letting other functions accept a file pointer as an argument.如果仅在一个模块中需要它,请考虑在 main() 中声明它并让其他函数接受文件指针作为参数。 So, unless your functions within the module have so many arguments that another would be unbearable.. there's (usually) no reason to declare a file pointer globally.
因此,除非您在模块中的函数有这么多 arguments 以至于另一个函数将无法忍受......(通常)没有理由全局声明文件指针。
Some logging libraries do it, which I don't care for... especially when dealing with re-entrant functions.一些日志库会这样做,我不关心......尤其是在处理可重入函数时。 Nevermind C's monolithic namespace:)
没关系 C 的单一命名空间:)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.