ifstream.read()没有读取任何内容?

huangapple go评论66阅读模式
英文:

ifstream.read() does not read anything?

问题

I got some file-reading problems in my project, so I wrote following test codes.

I'm tring to read a file by ifstream.read(), but anything has been read.

I've searched numerous related questions, but still can't figure out why my code doesn't work. please help.

#include <iostream>
#include <fstream>

int main() {
    /*--------------- write file ----------------*/
    std::ofstream ofs("testfile", std::ios::binary);
    const char *content = "11223344";
    ofs.write(content, sizeof content);

    system("chmod 777 testfile");

    /*--------------- read file ----------------*/

    char arr[8] = {0};
    const int length = sizeof arr / sizeof arr[0];
    std::ifstream ifs("testfile", std::ios::binary);

    std::cout << "good: " << ifs.good() << std::endl;  // 1

    ifs.seekg(0, ifs.end);
    int file_size = ifs.tellg();
    ifs.seekg(0, ifs.beg);
    std::cout << "file_size: " << file_size << std::endl << std::endl;  // 0

    if (ifs) {
        ifs.read(&arr[0], 2);
        std::cout << "after read," << std::endl;
        std::cout << "gcount: " << ifs.gcount() << std::endl;  // 0
        std::cout << "good: " << ifs.good() << std::endl;  // 0
        ifs.read(&arr[2], 2);
        ifs.read(&arr[4], 2);
        ifs.read(&arr[6], 2);
    } else {
        std::cout << "file not found" << std::endl;
    }

    for (int i = 0; i < length; ++i) {
        printf("%d ", arr[i]);
    }
    printf("\n");
}

The output is

good: 1
file_size: 0

after read,
gcount: 0
good: 0
0 0 0 0 0 0 0 0
英文:

I got some file-reading problems in my project, so I wrote following test codes.

I'm tring to read a file by ifstream.read(),but anything has been read.

I've searched numerous related questions, but still can't figure out why my code doesn't work. please help.

#include &lt;iostream&gt;
#include &lt;fstream&gt;

int main() {
    /*--------------- write file ----------------*/
    std::ofstream ofs(&quot;testfile&quot;, std::ios::binary);
    const char *content = &quot;11223344&quot;;
    ofs.write(content, sizeof content);

    system(&quot;chmod 777 testfile&quot;);

    /*--------------- read file ----------------*/

    char arr[8] = {0};
    const int length = sizeof arr / sizeof arr[0];
    std::ifstream ifs(&quot;testfile&quot;, std::ios::binary);

    std::cout &lt;&lt; &quot;good: &quot; &lt;&lt; ifs.good() &lt;&lt; std::endl;  // 1

    ifs.seekg(0, ifs.end);
    int file_size = ifs.tellg();
    ifs.seekg(0, ifs.beg);
    std::cout &lt;&lt; &quot;file_size: &quot; &lt;&lt; file_size &lt;&lt; std::endl &lt;&lt; std::endl;  // 0

    if (ifs) {
        ifs.read(&amp;arr[0], 2);
        std::cout &lt;&lt; &quot;after read,&quot; &lt;&lt; std::endl;
        std::cout &lt;&lt; &quot;gcount: &quot; &lt;&lt; ifs.gcount() &lt;&lt; std::endl;  // 0
        std::cout &lt;&lt; &quot;good: &quot; &lt;&lt; ifs.good() &lt;&lt; std::endl;  // 0
        ifs.read(&amp;arr[2], 2);
        ifs.read(&amp;arr[4], 2);
        ifs.read(&amp;arr[6], 2);
    } else {
        std::cout &lt;&lt; &quot;file not found&quot; &lt;&lt; std::endl;
    }

    for (int i = 0; i &lt; length; ++i) {
        printf(&quot;%d &quot;, arr[i]);
    }
    printf(&quot;\n&quot;);
}

The output is

good: 1
file_size: 0
after read,
gcount: 0
good: 0
0 0 0 0 0 0 0 0 

答案1

得分: 1

这是因为数据没有写入磁盘。在调用ofs.write之后,应该调用ofs.flush()ofs.close()来确保数据在读取之前写入磁盘。

此外,使用sizeof content来获取以content指针指定的地址开头的字符串的长度,在64位平台上,对于这个特定情况,字符串有8个字符是有效的,但在其他情况下将不起作用。正确的习惯是声明一个字符数组,并用文本字符串进行初始化,然后使用sizeof获取包含终止空字符的数组的大小,所以要获取字符串的长度,需要减去1。

因此,开始部分应该如下所示:

/*--------------- 写文件 ----------------*/  
std::ofstream ofs("testfile", std::ios::binary); 
const char content[] = "11223344";               
ofs.write(content, sizeof content - 1);          
ofs.close();
英文:

This is because the data is not written to the disk. After having called ofs.write, either ofs.flush() or ofs.close() should be called to ensure data is written on disk before reading it.

Also, sizeof content to obtain the length of the string starting at address given by content pointer works, on 64 bits platforms, for this particular case where the string has 8 characters, but it will not work on other cases. The right idiom is to declare an array of characters initializing it with the literal string, then sizeof gives the size of the array which contains the terminating nul character, so to obtain the length of the string then 1 should be subtracted.

So the beginning would rather be:

/*--------------- write file ----------------*/  
std::ofstream ofs(&quot;testfile&quot;, std::ios::binary); 
const char content[] = &quot;11223344&quot;;               
ofs.write(content, sizeof content - 1);          
ofs.close();                                     

huangapple
  • 本文由 发表于 2023年5月22日 17:22:15
  • 转载请务必保留本文链接:https://go.coder-hub.com/76304706.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定