- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
我正在尝试读取文件并输出内容。一切正常,我可以看到内容,但最后似乎添加了大约 14 个空字节。有谁知道这段代码有什么问题吗?
int length;
char * html;
ifstream is;
is.open ("index.html");
is.seekg (0, ios::end);
length = is.tellg();
is.seekg (0, ios::beg);
html = new char [length];
is.read(html, length);
is.close();
cout << html;
delete[] html;
最佳答案
您没有在 char 数组上放置空终止符。不是 ifstream 读取太多,cout 只是没有空终止符不知道什么时候停止打印。
如果你想读取整个文件,这会容易得多:
std::ostringstream oss;
ifstream fin("index.html");
oss << fin.rdbuf();
std::string html = oss.str();
std::cout << html;
关于C++ Ifstream 读太多?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7049214/
根据 C++11 标准,是 的行为 ifstream in("."); 指定的,还是系统相关的? 对于上下文,我试图避免使用 boost::filesystem 和类似的库,因为它们会导致不相关的可移
我尝试使用 ifstream 读取一个 3GB 的数据文件,但它给出了错误的文件大小,而当我读取一个 600MB 的文件时,它给出了正确的结果。除了错误的文件大小,我也无法使用 ifstream 读取
这里有没有人知道 C++ ifstream 的 get 指针在调用 read() 后可能会损坏的方法?我看到了一些我无法解释的真正奇怪的行为。例如(说明性代码,而不是我实际运行的代码): int ma
我正在编写一个外部合并排序。它是这样工作的:从大文件中读取 k 个 block ,在内存中对它们进行排序,执行 k 路合并,完成。所以我需要在 k-way 合并阶段从文件的不同部分顺序读取。最好的方法
阅读 Savitch 的 Problem Solving in C++,std::ifstream::fail 以检查文件是否已正确打开(ifstream 或 ofstream)。 正如我第一次看到的
我正在尝试逐行读取多个文件(本例中为 3 个)并使用 ifstream shared_ptrs vector 来执行此操作。但我不知道如何取消引用此指针以使用 getline() 或我的代码中存在其他
我正在编写一个程序,它从一个文本文件中获取多个变量。当程序发现EOF时, 它结束输入数据。 int main() { int val, count = 0; ifstream file
这个问题不太可能帮助任何 future 的访问者;它只与一个小的地理区域、一个特定的时间点或一个非常狭窄的情况相关,这些情况并不普遍适用于互联网的全局受众。为了帮助使这个问题更广泛地适用,visit
我想知道在什么情况下我们可以拥有: bool(std::ifstream) != std::ifstream::good() 区别在于 bool(std::ifstream) 不测试 eof 位,而
也许是个伪问题,但我需要一个明确的答案。这些函数的返回有什么不同吗 int FileExists(const std::string& filename) { ifstream file(file
我只是采用了一些在 Linux 下开发的文件阅读器的旧代码,并尝试在我的 Windows 项目中使用完全相同的代码,该项目是用 MSVC++7.1 编译的。代码编译没有任何问题,但根据 Windows
是var > var相同? 据我所知,它们应该完全相同。但是已经很晚了,我的大脑处于半睡状态,所以我想澄清一下。 最佳答案 它们不一样。 foo > foo是bar.operator>>(foo)或
我一直在测试使用 Visual Studio 在 C++ 中读取文件的性能,我得到了一些我真的不明白的结果。 我的代码如下: std::vector Method1(std::string fileP
我使用 SymbolHound 在网络上查找比较两者的资源,但找不到任何东西。 在 VS13 中寻找 std::ifstream::in 的声明和定义让我找到了 basic_ifstream。寻找 s
我无法将图像文件读入缓冲区。当我读取普通的 ascii 文件时,一切都很好,但当涉及到图像文件时,我怀疑图像文件中有一个\0 字符? 我需要接收图像文件,将其解析为 16KB 的 block 以进行哈
我知道已经有很多关于如何迭代 ifstream 的答案,但没有一个真正帮助我找到解决方案。 我的问题是:我有一个包含多行数据的txt文件。 txt 文件的第一行告诉我其余数据是如何组成的。例如这是我的
首先,我为我的英语表示歉意。 我在尝试编写一个 XML 解析器时遇到了一个奇怪的问题。为了解释我的问题,我应该说,我有一个 xml 解析器类,它有一个 ifstream 成员。这个类有一个函数,它会读
我正在使用 xcode。我编译得很好,然后一旦它运行它就成功打开文件,然后读取两个值,但这些值不会进入变量,因此它会跳过 for 循环并关闭文件并从 main 返回。 #include #inclu
我有一个包含以下格式数据的文件: name1 p1 p2 ... p11 name2 p1 p2 ... p11 ... (参数不一定在一行) 我的目标是读取名称和 11 个参数,对它们做一些事情,然
我正在尝试读取一个输入文件,该文件的格式为一行中有两个数字,并将该行中的第一个数字存储在一个 vector 中,将第二个数字存储在另一个 vector 中。 除了实际读取文件之外,我的代码的每个部分都
我是一名优秀的程序员,十分优秀!