这段代码“长度指示器实现”有什么问题?

发布于 2024-08-30 04:06:30 字数 1429 浏览 3 评论 0原文

这是长度指示符字段的实现 但它挂起了,我认为陷入了循环并且没有显示任何东西。

// readx22.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include "iostream"
#include "fstream"
#include "stdio.h"
using namespace std;


class Student
{
public:
 string id;
 size_t id_len;
 string first_name;
 size_t first_len;
 string last_name;
 size_t last_len;
 string phone;
 size_t phone_len;
 string grade;
 size_t grade_len;
 void read(fstream &ven);
 void print();
};
void Student::read(fstream &ven)
{
 size_t cnt;
 ven >> cnt;
 id_len=cnt;
    id.reserve( cnt );
    while ( -- cnt ) {
        id.push_back( ven.get() );
    }

 ven >> cnt;
 first_len=cnt;
    first_name.reserve( cnt );
    while ( -- cnt ) {
        first_name.push_back( ven.get() );
    }

 ven >> cnt;
 last_len=cnt;
    last_name.reserve( cnt );
    while ( -- cnt ) {
        last_name.push_back( ven.get() );
    }

 ven >> cnt;
 phone_len=cnt;
    phone.reserve( cnt );
    while ( -- cnt ) {
        phone.push_back( ven.get() );
    }

 ven >> cnt;
 grade_len=cnt;
    grade.reserve( cnt );
    while ( -- cnt ) {
        grade.push_back( ven.get() );
    }

}
void Student::print()
{
// string::iterator it;
 for ( int i=0 ; i<id_len; i++)
  cout << id[i];

}
int main()
{
 fstream in;
 in.open ("fee.txt", fstream::in);
 Student x;
 x.read(in);
 x.print();
 return 0;
}

谢谢

this is an implementation of length indicator field
but it hang and i think stuck at a loop and don't show any thing.

// readx22.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include "iostream"
#include "fstream"
#include "stdio.h"
using namespace std;


class Student
{
public:
 string id;
 size_t id_len;
 string first_name;
 size_t first_len;
 string last_name;
 size_t last_len;
 string phone;
 size_t phone_len;
 string grade;
 size_t grade_len;
 void read(fstream &ven);
 void print();
};
void Student::read(fstream &ven)
{
 size_t cnt;
 ven >> cnt;
 id_len=cnt;
    id.reserve( cnt );
    while ( -- cnt ) {
        id.push_back( ven.get() );
    }

 ven >> cnt;
 first_len=cnt;
    first_name.reserve( cnt );
    while ( -- cnt ) {
        first_name.push_back( ven.get() );
    }

 ven >> cnt;
 last_len=cnt;
    last_name.reserve( cnt );
    while ( -- cnt ) {
        last_name.push_back( ven.get() );
    }

 ven >> cnt;
 phone_len=cnt;
    phone.reserve( cnt );
    while ( -- cnt ) {
        phone.push_back( ven.get() );
    }

 ven >> cnt;
 grade_len=cnt;
    grade.reserve( cnt );
    while ( -- cnt ) {
        grade.push_back( ven.get() );
    }

}
void Student::print()
{
// string::iterator it;
 for ( int i=0 ; i<id_len; i++)
  cout << id[i];

}
int main()
{
 fstream in;
 in.open ("fee.txt", fstream::in);
 Student x;
 x.read(in);
 x.print();
 return 0;
}

thanks

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

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

发布评论

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

评论(2

小嗷兮 2024-09-06 04:06:30

您可能应该在任何地方都使用 cnt-- 而不是 -- cnt。第一个零字节字符串将触发一个极大的循环,最终耗尽所有内存(64 位操作系统上可能除外)。实际上,甚至不用担心这个修复。循环get()效率极低,只需调用read()即可。

You probably should have used cnt-- instead of -- cnt everywhere. The first zero-byte string will trigger an extremely large loop that eventually consumes all memory (except maybe on a 64-bit OS). Actually, don't even bother with this fix. Loop over get() is extremely inefficient, just call read().

那一片橙海, 2024-09-06 04:06:30

如果我正确理解你的问题,那么当你运行这个时,你会看到无限循环吗?

我很想知道每个循环之前的 cnt 是什么。

另外,您的代码实际上是否达到了x.print()


另外,这听起来像是取出调试器并在代码上运行它的好时机。如果是无限循环,调试器会很快告诉您卡在哪里。

If I understand your question correctly, you are seeing an infinite loop when you run this?

I'd be curious to know what cnt is before each loop.

Also, does your code actually make it to x.print()?


Also, this sounds like a great time to take out a debugger and run it on your code. If it is an infinite loop, a debugger will very quickly tell you where you are stuck.

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