在C++中创建文件解析器
Creating a File Parser in C++
我正在尝试创建一个解析器类,该类将解析基于" "的文件并将单词放入链表中。
class FileReader
{
public:
FileReader(char* file)
{
ifstream fout (file, ifstream::in);
string hold;
while (fout.good())
{
getline (fout, hold, " ");
cout << hold;
}
fout.close();
}
};
函数getline(fout, hold, " ")
不识别 " " 作为分隔符。
我还没有对链表部分进行编码,所以这只是程序的解析部分。
还有没有更好的方法来创建解析器?
它应该像这样工作:
#include <fstream>
#include <iterator>
#include <list>
#include <string>
std::ifstream infile(file);
std::list<std::string> words(std::istream_iterator<std::string>(infile),
std::istream_iterator<std::string>());
现在words
是空格分隔标记的链接列表。
教训:最好的代码是你不必编写的代码。
> getline
中的最后一个参数是char
而不是string
。查看您当前的代码,您想要getline(fout,hold,' ')
或只是getline(fout,hold)
- *如果您想要整行。
*: 编辑
相关文章:
- 终端不会为C++文件创建.exe文件吗
- 如何从具有两列的.txt文件创建并行数组?
- 从 .lib 文件创建一个.dll文件
- 如何从输入文件 c++ 创建随机名称生成器
- 无法从 cso 文件创建顶点着色器(从 fx 文件创建)
- 如何为多个目录中的多个文件创建生成文件?
- 在编译时从 c++ 文件创建枚举
- 如何在运行时从 c++ 中的 DLL 文件创建对象?
- 文件创建并不总是成功
- 如何使用 QFile、c++ 从文件创建整数数组
- 您可以使用 makefile 从 .o 对象文件创建 C++ 文件吗?
- 从函数中的dat文件创建一个2维数组
- 如何为 cpp 和 h 文件创建别名(并忽略编译器__attributes__)
- 如何从现有的生成文件创建 CMake 文件?
- 为什么我无法使用文本文件创建文件映射?
- 通过读取文件创建映射<字符串,矢量>时如何管理内存<string>
- 从文本文件创建 2D 字符串矢量
- ofStream: - 尽管文件创建了,但文本文件上没有输出
- 从语言文件创建JSON对象
- 将文本文件创建到节点.js的插件C++