英文:
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 <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
答案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("testfile", std::ios::binary);
const char content[] = "11223344";
ofs.write(content, sizeof content - 1);
ofs.close();
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论