简体   繁体   English

fwrite() 不写任何东西

[英]fwrite() doesn't write anything

I recently learned how to use fwrite on the Web and tried it on my own, but it does nothing on the file The file is created, but it is nothing in it.最近学习了如何在Web上使用fwrite,自己尝试了一下,但是对文件没有任何作用。 I tried in line 9 writing as char buffer[]={'r','e','a','d'};我尝试在第 9 行写为char buffer[]={'r','e','a','d'}; , char buffer[4]={'r','e','a','d'}; , char buffer[4]={'r','e','a','d'}; , char* buffer="read" . , char* buffer="read" What am I doing wrong?我究竟做错了什么? (I also tried fprintf , but result was same.) (我也试过fprintf ,但结果是一样的。)

#include <stdio.h>

int main()
{
    FILE* fp = NULL;

    fp = fopen_s(&fp, "C:\\Users\\Public\\Downloads\\test.txt", "w");

    int buffer[] = { 'r','e','a','d' };

    fwrite(buffer, sizeof(buffer), 1, &fp);

    fclose(&fp);

    return 0;
}

You have to pass the file pointer fp , not the pointer to the file pointer &fp to fwrite() and fclose() .您必须将文件指针fp而不是指向文件指针&fp的指针传递给fwrite()fclose()

Also assigning the return value of fopen_s() to fp is wrong because it is not a file pointer but a value the represents if it was successful.同样将fopen_s()的返回值分配给fp也是错误的,因为它不是文件指针,而是表示成功的值。

Try this:尝试这个:

#include <stdio.h>

int main()
{
    FILE* fp = NULL;

    if (fopen_s(&fp, "C:\\Users\\Public\\Downloads\\test.txt", "w") != 0)
    {
        fputs("file open error\n", stderr);
        return 1;
    }

    int buffer[] = { 'r','e','a','d' };

    fwrite(buffer, sizeof(buffer), 1, fp);

    fclose(fp);

    return 0;
}

The primary problem of seeing output in your files is address in the accepted answer.在您的文件中看到 output 的主要问题是接受的答案中的地址。 This will address the content that is output to your file.这会将 output 的内容寻址到您的文件中。

"I tried in line 9 writing as char buffer[]={'r','e','a','d'};, char buffer[4]={'r','e','a','d'};, char* buffer="read" “我在第 9 行尝试写为 char buffer[]={'r','e','a','d'};, char buffer[4]={'r','e','a' ,'d'};, char* buffer="read"

But then in your post you have the following:但是在您的帖子中,您有以下内容:

int buffer[] = { 'r','e','a','d' };
...
fwrite(buffer, sizeof(buffer), 1, fp); //results in file are: 7200 0000 6500 0000 6100 0000 6400 0000  

Important to note, int buffer[] = { 'r','e','a','d' };需要注意的重要一点, int buffer[] = { 'r','e','a','d' }; does not produce a C string.不会产生C字符串。

All of the following are legal arrays in C , but only the bottom 2 are treatable as a C string, as it is the only one that has the null terminator. All of the following are legal arrays in C , but only the bottom 2 are treatable as a C string, as it is the only one that has the null terminator.

int buffer[] = { 'r','e','a','d' };//int array, ASCII values will be output.
char buffer[]={'r','e','a','d'};//char array, not C string
char buffer[4]={'r','e','a','d'};//char array, not C string
char buffer[]={'r','e','a','d','\0'};//C string (editable)
char* buffer="read";//C string (not editable)

So, if you just happened to be looking for something like "read" to be written to the file rather than 7200 0000 6500 0000 6100 0000 6400 0000 use the following in your code:因此,如果您恰好正在寻找将"read"之类的内容写入文件而不是7200 0000 6500 0000 6100 0000 6400 0000 ,请在代码中使用以下内容:

char buffer[]={'r','e','a','d','\0'};//note the null terminator to avoid UB
...
fwrite(buffer, sizeof(buffer), 1, fp);  // results are: "read"

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

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