C语言 如何使用 FILE 作为 C 中函数的参数?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15738029/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 05:53:53  来源:igfitidea点击:

How do I use a FILE as a parameter for a function in C?

cfilefunctionunix

提问by Giga Tocka

I am learning C and I come from a Java background. I would appreciate it if I could have some guidance. Here is my code:

我正在学习 C,我来自 Java 背景。如果我能得到一些指导,我将不胜感激。这是我的代码:

#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <unistd.h>

int main(void)
{
    char *str = "test text\n";
    FILE *fp;

    fp = fopen("test.txt", "a");
    write(fp, str);
}

void write(FILE *fp, char *str)
{
    fprintf(fp, "%s", str);
}

When I try to compile, I get this error:

当我尝试编译时,出现此错误:

xxxx.c: In function ‘main':
xxxx.c:18: warning: passing argument 1 of ‘write' makes integer from pointer without a cast
/usr/include/unistd.h:363: note: expected ‘int' but argument is of type ‘struct FILE *'
xxxx.c:18: error: too few arguments to function ‘write'
xxxx.c: At top level:
xxxx.c:21: error: conflicting types for ‘write'
/usr/include/unistd.h:363: note: previous declaration of ‘write' was here

Any thoughts? Thanks for your time.

有什么想法吗?谢谢你的时间。

回答by squiguy

You are lacking a function prototype for your function. Also, writeis declared in unistd.hso that is why you get the first error. Try renaming that to my_writeor something. You really only need the stdio.hlibrary too as a side note, unless you plan on using other functions later. I added error checking for fopenas well as return 0;which should conclude every main function in C.

您的函数缺少函数原型。此外,write声明为 inunistd.h所以这就是为什么你会得到第一个错误。尝试将其重命名为my_write或其他名称。您实际上也只需要该stdio.h库作为旁注,除非您打算稍后使用其他功能。我添加了错误检查fopen以及return 0;哪个应该结束 C 中的每个主要函数。

Here is what I would do:

这是我会做的:

#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <unistd.h>

void my_write(FILE *fp, char *str)
{
    fprintf(fp, "%s", str);
}

int main(void)
{
    char *str = "test text\n";
    FILE *fp;

    fp = fopen("test.txt", "a");
    if (fp == NULL)
    {
        printf("Couldn't open file\n");
        return 1;
    }
    my_write(fp, str);

    fclose(fp);

    return 0;
}

回答by Deepankar Bajpeyi

See man 2 writeon linux.

man 2 write在 linux 上查看。

#include <unistd.h>

     ssize_t write(int fd, const void *buf, size_t count);

That is the prototype. You need to pass an integer file descriptor and not a file pointer. If you want your own function Change the name to foo_writeor something

那就是原型。您需要传递整数文件描述符而不是文件指针。如果你想要自己的函数 把名字foo_write改成什么的

回答by Xymostech

There's already a system function called write. Just name your function something else, put a function declaration before you use it, and you'll be fine.

已经有一个名为write. 只需将您的函数命名为其他名称,在使用之前放置一个函数声明,就可以了。