繁体   English   中英

在 C/C++ 中声明和使用 FILE * 指针的正确方法是什么?

[英]What is the correct way to declare and use a FILE * pointer in C/C++?

在 C/C++ 中声明和使用 FILE * 指针的正确方法是什么? 应该声明为全局的还是局部的? 有人可以举一个很好的例子吗?

无论是本地的还是全球的,都无关紧要。 文件指针的scope与其用途无关。

一般来说,尽可能避免使用全局变量是个好主意。

这是一个示例,展示了如何从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;
}

它只是一个普通的指针,和其他指针一样。

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
}

这是谷歌上第一次点击“文件 io in c”

http://www.cs.bu.edu/teaching/c/file-io/intro/

这是 gamedev 的第三次命中,更多的是 C++ 倾斜

http://www.gamedev.net/reference/articles/article1127.asp

您在 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;
}

如果您愿意,您完全可以声明全局文件句柄,就像任何其他变量一样,但可能不推荐这样做。

这是 C 方式。 C++ 可以使用这个,但我认为有一个更 C++ 友好的方式来做它。 请注意,当问题标记为 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++,这不是您想要的。

编辑:添加了一些错误检查。 始终在代码中使用错误检查。

首先,请记住文件指针(和相关的分配结构)基于较低级别的 open() read() write() 调用。 关联的文件描述符(由 fileno(file_pointer) 获得是最不有趣的东西,但你可能想用它来观察你的 scope。

如果您要在模块中将文件指针声明为全局文件,那么将其保留为 static(包含在该模块/object 文件中)通常是一个非常好的主意。 有时这比将它存储在从 function 传递到 function 的结构中要容易一些,如果你需要匆忙写一些东西的话。

例如,(坏)

#include <stdio.h>
#include ...

#define MY_LOG_FILE "file.txt"

FILE *logfile

更好的做法是:

#include <stdio.h>

#define MY_LOG_FILE "file.txt"

static FILE *logfile;

int main(void)
{

除非,您需要几个模块才能访问该指针,在这种情况下,您最好将它放在一个可以传递的结构中。

如果仅在一个模块中需要它,请考虑在 main() 中声明它并让其他函数接受文件指针作为参数。 因此,除非您在模块中的函数有这么多 arguments 以至于另一个函数将无法忍受......(通常)没有理由全局声明文件指针。

一些日志库会这样做,我不关心......尤其是在处理可重入函数时。 没关系 C 的单一命名空间:)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM