English 中文(简体)
C++ 溪流没有植被吗?
原标题:C++ stream get unget not a nop?
  • 时间:2011-10-25 18:21:01
  •  标签:
  • c++
  • io

我有以下C++方案,利用Windows 2008视力演播室进行。 7. 基本生活 我的特性是,然后是没有目标。 在这样做之后,档案位置不同。 为什么? 我如何处理这个问题?


测试.txt(如果你愿意,下载链连接以下)

/* Comment 1 */

/* Comment 2 */

#include <fstream>

int main (int argc, char ** argv) {
    char const * file = "test.txt";
    std::fstream fs(file, std::ios::in);
    std::streampos const before = fs.tellg();

    // replacing the following two lines with
    // char c = fs.peek(); results in the same problem
    char const c = fs.get();
    fs.unget();

    std::streampos const after = fs.tellg();
    fs.seekg(after);
    char const c2 = fs.get();
    fs.close();
    return 0;
}

  • c: 47 / char
  • c2: -1 ÿ char
  • before: {_Myoff=0 _Fpos=0 _Mystate=0 } std::fpos<int>
  • after: {_Myoff=0 _Fpos=-3 _Mystate=0 } std::fpos<int>

添加<条码>:fstream:binary到构造者看来可以解决这个问题。 也许它必须处理档案中的新路线? 如果是,它为什么会影响甚至接近阅读新线的法典?

更新后,寻求后一种立场并具有另一种特性。

似乎通过“灯塔”来节约。 举足轻重。 通过“灯塔”进行救助,使下游工作变得oka。

我将文件上载到洞gle角,如果你想把文件 d起来的话:

最佳回答

我看到你同样的行为。 在进行了一些试验之后,它像档案一样以“不完美”的形式处理,然后编辑“^M”特性(至少是我如何复制)。

为了确定该档案,我编辑了Vim的档案,执行“:编造......”后添加并删除了该档案的多余性质,然后予以保存。

问题回答

档案位置按预期:

// unget.cpp
#include <fstream>
#include <iostream>
int main ()
{
    char const * file = "test.txt";
    std::fstream fs(file, std::fstream::in);

    std::cout << fs.tellg() << std::endl; // 0
    char c = fs.get();
    std::cout << fs.tellg() << std::endl; // 1
    fs.unget();
    std::cout << fs.tellg() << std::endl; // 0

    fs.close();
    return 0;
}

建设和管理:

$ clang++ unget.cpp 
$ ./a.out 
0
1
0

或者,我不理解问题在哪里。





相关问题
Undefined reference

I m getting this linker error. I know a way around it, but it s bugging me because another part of the project s linking fine and it s designed almost identically. First, I have namespace LCD. Then I ...

C++ Equivalent of Tidy

Is there an equivalent to tidy for HTML code for C++? I have searched on the internet, but I find nothing but C++ wrappers for tidy, etc... I think the keyword tidy is what has me hung up. I am ...

Template Classes in C++ ... a required skill set?

I m new to C++ and am wondering how much time I should invest in learning how to implement template classes. Are they widely used in industry, or is this something I should move through quickly?

Print possible strings created from a Number

Given a 10 digit Telephone Number, we have to print all possible strings created from that. The mapping of the numbers is the one as exactly on a phone s keypad. i.e. for 1,0-> No Letter for 2->...

typedef ing STL wstring

Why is it when i do the following i get errors when relating to with wchar_t? namespace Foo { typedef std::wstring String; } Now i declare all my strings as Foo::String through out the program, ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

Window iconification status via Xlib

Is it possible to check with the means of pure X11/Xlib only whether the given window is iconified/minimized, and, if it is, how?

热门标签