如何在c++中读取标准istream缓冲区?

发布于 2024-12-27 02:03:49 字数 1013 浏览 1 评论 0原文

我有以下问题。我必须实现一个类,该类具有一个属性,该属性是一个 char 指针,旨在指向对象的“代码”,如下所示:

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

依此类推。 getVal() 是一种从标准 istream 中获取代码并填充所有信息(包括代码)的方法。问题是,标识对象的“代码”不能超过一定数量的字符。这必须在不使用 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";
     }
     ...
}

这是禁止的。我也无法使用自定义的 istream,也无法使用 boost 库。

我以为我可以很容易地找到 istream 保存其信息的地方,但我找不到它。我发现的只是提及其他类型的流。

有人可以告诉我这是否可以完成或者流在哪里保存其缓冲信息?

谢谢

I have the following problem. I have to implement a class that has an attribute that is a char pointer meant to point to the object's "code", as follows:

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

So on, so forth. getVal() is a method that takes the code from the standard istream and fills in all the information, including the code. The thing is, the "code" that identifies the object can't be longer than a certain number of characters. This has to be done without using customized buffers for the method getVal(), so I can't do the following:

//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";
     }
     ...
}

This is forbidden. I also can't use a customized istream, nor the boost library.

I thought I could find the place where istream keeps its information rather easily, but I can't find it. All I've found were mentions to other types of stream.

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

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

阿楠 2025-01-03 02:03:49

是的,使用 strlen 肯定会起作用..您可以

   int main()
   {

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

为大于 6 个字符的输入编写一个示例程序,它将显示大小 > 6

yes using strlen would work definitely ..you can write a sample program

   int main()
   {

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

for inputs greater than size 6 characters it will display size >6

猫性小仙女 2025-01-03 02:03:49

嗯.... >> 读取到第一个空白,而 strlen 计数到第一个 null。如果您确定要读取的字符串中间没有空格并且连续字符不超过 100 个,则可以混合使用它们。如果没有,您将在抛出之前溢出缓冲区。

另外,访问缓冲区并不意味着所有字符串都已存在(字符串可以超出缓冲区空间,需要部分读取并重新填充缓冲区...)

如果空格是分隔符,为什么不直接读入 std::string,并对其最终状态做出反应?上面的所有动态都已在 std::string 的 >> 内部处理。

[在下面的评论后进行编辑]

存储未知大小的序列的唯一方法是动态分配空间并使其随着需要的增长而增长。这就是 Sting 和 Vector 的作用。

无论您使用它们还是编写自己的代码来分配和重新分配需要更多空间的位置,都不会改变实质内容。

我开始认为这些要求的唯一原因是看看您编写自己的字符串类的能力。所以...只需编写它:

声明一个持有指针、大小和容量的类,分配一些空间,跟踪存储量,当没有存储可用时,分配另一个更宽的存储,复制旧的,销毁它,然后相应地调整数据成员。

直接访问文件缓冲区不是方法,因为您无法控制文件缓冲区的填充方式。

uhm .... >> reads up to the first blank, while strlen counts up to the first null. They can be mixed if you know for sure no blanks are in the middle of string you're going to read and that there are no more than 100 consecutive characted. If not, you will overrun the buffer before throwing.

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.

[EDIT after the comments below]

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.

Whether you use them or write your own code to allocate and reallocate where more space is required, doesn't change the substance.

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:

declare a class holding a pointer a size and a capacity, allocate some space, track how much you store, and when no store is available, allocate another wider store, copy the old, destroy it, and adjust the data member accordingly.

Accessing directly the file buffer is not the way, since you don't control how the file buffer is filled in.

青春如此纠结 2025-01-03 02:03:49

istream 使用 streambuf

我发现 www.cppreference.com 是快速学习 C++ 的好地方参考。您可以去那里了解如何使用streambuf或其派生filebuf

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文