- Java 双重比较
- java - 比较器与 Apache BeanComparator
- Objective-C 完成 block 导致额外的方法调用?
- database - RESTful URI 是否应该公开数据库主键?
我正在读取这样的文件:
char string[256];
std::ifstream file( "file.txt" ); // open the level file.
if ( ! file ) // check if the file loaded fine.
{
// error
}
while ( file.getline( string, 256, ' ' ) )
{
// handle input
}
只是为了测试目的,我的文件只有一行,最后有一个空格:
12345
我的代码首先成功读取了 12345。但随后它并没有结束循环,而是读取了另一个字符串,这似乎是一个返回/换行符。
我已经在 gedit
和 nano
中保存了我的文件。而且我用linux的cat
命令也输出过,最后没有return。所以文件应该没问题。
为什么我的代码读取返回/换行符?
谢谢。
最佳答案
首先确保你的输入文件是好的:
运行以下命令并让我们知道输出结果:
#include <iostream>
#include <sstream>
#include <string>
#include <iterator>
#include <fstream>>
#include <iomanip>
#include <algorithm>
int main()
{
std::ifstream file("file.txt");
std::cout << std::hex;
std::copy(std::istreambuf_iterator<char>(file),
std::istreambuf_iterator<char>(),
std::ostream_iterator<int>(std::cout, " "));
}
输出为 31 32 33 34 35 20 0A
尝试运行这段代码,看看输出是什么:
#include <iostream>
#include <sstream>
#include <string>
#include <iterator>
#include <fstream>>
#include <iomanip>
#include <algorithm>
int main()
{
std::ofstream file("file.txt");
file << "12345 \n";
}
转储此文件的输出并将其与原始文件进行比较。
问题是不同的平台有不同的线路终止顺序。我只想验证“0x0A”是您平台的线路终止序列。请注意,当以文本模式读取文件时,行终止序列将转换为 '\n',而当您以文本模式将 '\n' 输出到文件时,它会转换为行终止序列。
所以我有文件:file.txt
> od -ta -tx1 file.txt
0000000 1 2 3 4 5 sp nl
31 32 33 34 35 20 0a
0000007
因此文件包含 1 行以 0x0A
使用这个程序:
#include <iostream>
#include <sstream>
#include <string>
#include <iterator>
#include <fstream>>
#include <iomanip>
#include <algorithm>
int main()
{
std::ifstream file("file.txt");
std::string line;
while(std::getline(file,line))
{
std::cout << "Line(" << line << ")\n";
}
}
我得到:
> g++ t.cpp
> ./a.out
Line(12345 )
关于C++: ifstream::getline 问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4704097/
根据 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 中。 除了实际读取文件之外,我的代码的每个部分都
我是一名优秀的程序员,十分优秀!