【问题标题】:updating file pointer via other function通过其他函数更新文件指针
【发布时间】:2016-11-23 08:42:07
【问题描述】:
#include<stdio.h>

void hello(FILE * fp)
{ 
    if( ( fp = fopen("log","r") ) == NULL)
        printf("%s", "Error opening file");
}

void main()
{
    char p;
    FILE *sf=fopen("prac.txt","r");
    hello(sf);

    p=fgetc(sf);
    printf("%c",p);
}

我想通过hello函数将文件指针sf更改为指向文件log,但printf仍在打印prac.txt文件的内容。

【问题讨论】:

标签: c


【解决方案1】:

如果要更改FILE* 指向的内容,则需要传递FILE**。在更改它之前,您需要确保它恰好指向的任何文件都已关闭。这也依赖于您总是在 fclose 之后将 FILE* 变量设置为 NULL(唉,这不会自动发生),因此很有可能不小心使用此函数会在已经关闭的 @987654326 上调用 fclose @。但这可能仍然比故意泄漏文件描述符而不刷新文件要好。

void hello(FILE **fp)
{
  // This is actually a horrible test. And in general, this is not
  // something you should do, but it is better than leaking open
  // file descriptors, so, yeah, 
  if (*fp != NULL) {
    fclose(*fp);
    *fp = NULL;
    printf("Closed file.");
  }
  if( (*fp = fopen("log","r") == NULL) {
    printf("%s", "Error opening file");
  }
}

【讨论】:

  • 为什么需要关闭?
  • @enamel 有几个原因。已“写入”(但仍由程序缓冲)的数据可能会丢失。 “打开的文件”是一种非常有限的资源,所以你不应该浪费它们。而且,如果您像这样覆盖 FILE*,则之后您绝对无法关闭或刷新它。
【解决方案2】:

void hello(FILE *fp)中,fp只存在于函数hello的范围内(调用函数时会复制指针的值,并在函数结束时销毁)。

这行得通:

#include<stdio.h>

FILE* hello(FILE * fp)
{ 
    fclose(fp);
    if( ( fp = fopen("log","r") ) == NULL) {
        printf("%s", "Error opening file");
        return NULL;
    }
    return fp;
}

void main()
{
    char p;
    FILE *sf=fopen("prac.txt","r");
    sf = hello(sf);

    if (sf != NULL) 
    {
        p=fgetc(sf);
        printf("%c",p);
    }
}

【讨论】:

  • 谢谢.....为什么我们需要这个 fclose(fp)? ............我试图通过引用调用来更新'sf'指针地址
  • 阅读 Adam Rosenfield 的 fclose 答案:stackoverflow.com/questions/8175827/…。还有一点,这里不更新sf指针地址,直接更新sf指针的一个副本。当您通过引用调用时,您必须发送变量的地址。如果你的变量是一个指针,你必须发送一个指针指针。这里 sf 是 FILE* 并且它的地址类型是 FILE** (参见 Vatine 的解决方案)
【解决方案3】:

这是因为你复制了参数。您不覆盖文件指针的内容,只覆盖地址的本地副本。

文件指针本身没有更新。要归档它,要么传递一个指向函数的指针并相应地取消引用,要么返回新的文件指针并覆盖旧的。无论如何都要关闭文件。

【讨论】:

    【解决方案4】:

    你应该在重新打开文件之前关闭它

    #include<stdio.h>
    void hello(FILE ** fp)
    { 
     fclose(*fp);
    if( ( *fp = fopen("log","r") ) == NULL)
      printf("%s", "Error opening file");    
    }
    
    void main()
    {
     char p;
     FILE *sf=fopen("prac.txt","r");
     hello(&sf);
    
     p=fgetc(sf);
     printf("%c",p);
    }
    

    【讨论】:

    【解决方案5】:
    #include<stdio.h>
    // Use double pointer since fp is getting address of a pointer variable
    void hello(FILE ** fp)
    { 
        // If the file is already pointing to some file then close that file
        if(*fp != NULL)
        {
            fclose(*fp); 
            *fp = NULL;
        }
        if( ( *fp = fopen("log","r") ) == NULL)
            printf("%s", "Error opening file");
    }
    
    int main()
    {
        char p;
        FILE *sf=fopen("prac.txt","r");
        // While sending the file pointer you need to send the address of the file pointer
        hello(&sf);
    
        p=fgetc(sf);
        printf("%c",p);
    
        return 0;
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-07-01
      • 2020-09-08
      • 2015-05-22
      • 2018-09-08
      • 2021-11-02
      • 1970-01-01
      • 2021-08-17
      • 2015-06-21
      相关资源
      最近更新 更多