从ifstream读取将不会读取空格 [英] Reading from ifstream won't read whitespace

查看:1244
本文介绍了从ifstream读取将不会读取空格的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在C ++中实现一个自定义词法分析器,当试图在空白处读取时,ifstream将不会读取它。我正在使用>> 逐个字符读取,所有的空格都消失了。有什么办法让ifstream保留所有的空白并读出来给我吗?我知道当读取整个字符串时,读取将停止在空格,但我希望通过字符读字符,我会避免这种行为。

I'm implementing a custom lexer in C++ and when attempting to read in whitespace, the ifstream won't read it out. I'm reading character by character using >>, and all the whitespace is gone. Is there any way to make the ifstream keep all the whitespace and read it out to me? I know that when reading whole strings, the read will stop at whitespace, but I was hoping that by reading character by character, I would avoid this behaviour.

尝试: code> .get(),由许多答案推荐,但它具有与 std :: noskipws 相同的效果,我现在得到所有的空间,但不是我需要lex一些结构的新行字符。

Attempted: .get(), recommended by many answers, but it has the same effect as std::noskipws, that is, I get all the spaces now, but not the new-line character that I need to lex some constructs.

这里是冒犯的代码评论已截断)

Here's the offending code (extended comments truncated)

while(input >> current) {
    always_next_struct val = always_next_struct(next);
    if (current == L' ' || current == L'\n' || current == L'\t' || current == L'\r') {
        continue;
    }
    if (current == L'/') {
        input >> current;
        if (current == L'/') {
            // explicitly empty while loop
            while(input.get(current) && current != L'\n');
            continue;
        }

我打破 c $ c>行,并查看当前中的每个值,以及 \r \\\
绝对不在其中 - 输入只是跳到输入文件中的下一行。

I'm breaking on the while line and looking at every value of current as it comes in, and \r or \n are definitely not among them- the input just skips to the next line in the input file.

推荐答案

我最终只是打开Windows API,并使用它先读取整个文件到缓冲区,然后读取该缓冲区的字符。谢谢你们。

I ended up just cracking open the Windows API and using it to read the whole file into a buffer first, and then reading that buffer character by character. Thanks guys.

这篇关于从ifstream读取将不会读取空格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆