簡體   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