English 中文(简体)
如何在++中读到标准一流缓冲?
原标题:How to read the standard istream buffer in c++?

我有以下问题。 我必须执行一个等级,即指指标的“代码”,具体如下:

class foo{
     private:
         char* cod;
         ...
     public:
         foo();
         void getVal();
         ...
}

因此,从标准一流中采用该守则并填写包括该守则在内的所有信息。 就是,确定标的“代码”可能比一定数量的特性更长。 要做到这一点,就必须使用定制的缓冲器,使这种方法能够做到以下几点:

//suppose the maximum number of characters is 50
void foo::getVal()
{
     char buffer[100];
     cin >> buffer;
     if (strlen(buffer) > 50) //I m not sure this would work considering how the stream
                               of characters would be copied to buffer and how strlen
                               works, but suppose this tells me how long the stream of 
                               characters was.
     {
        throw "Exception";
     }
     ...
}

禁止这样做。 我也可以使用一个定制的单流,也可以使用提升图书馆。

我认为,我可以找到一流很容易地保存其信息的地方,但我可以发现。 所有我发现的都提到了其他类型的流。

Can somebody tell me if this can be done or where the stream keeps its buffered information?

增 编

最佳回答

采用透镜可以肯定地工作。

   int main()
   {

    char buffer[10];
    std::cout << "enter buffer:" ;
    std::cin >>buffer;
    if(strlen(buffer)>6)
    std::cout << "size > 6";
    getch();
   }

投入量大于6级,将显示规模和幅度;6

问题回答

uhm ......>> 读到第一张空白处,而斯特伦则算作第一张。 如果你们知道,你不会看到你重新阅读的圈子,并且没有超过100个连续的果园,那么他们就会有好坏。 否则,你将 over倒缓冲,然后扔下。

Also, accessing the buffer does not grant all the string to be already there (the string can go past the buffer space, requiring to partially read and refill the buffer...)

If blanks are separator, why not just read into an std::string, and react to its final state? All the dynamics above are already handled inside >> for std::string.

[摘自以下评论]

The only way to store a sequence of unknown size, is to dynamically allocate the space and make it grow as it is required to grow. This is, no more - no less, what sting and vector do.

如果需要更多的空间,您是否使用或写自己的法典来分配和重新定位,则不会改变实质内容。

I m start thinking the only reason of those requirements is to see your capability in writing your own string class. So ... just write it:

• 宣布一个拥有大小和容量的班级,分配一些空间,跟踪贵重储存量,在没有仓库的情况下,再分配一个更广阔的仓库,复制旧的仓库,销毁旧的,并相应调整数据成员。

直接接触档案缓冲,不是这样,因为你不控制档案如何填满。

An istream uses a streambuf.

I find that www.cppreference.com is a pretty good place for quick C++ references. You can go there to see how to use a streambuf or its derivative filebuf.





相关问题
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?

热门标签