这段代码“长度指示器实现”有什么问题?
这是长度指示符字段的实现 但它挂起了,我认为陷入了循环并且没有显示任何东西。
// 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可能应该在任何地方都使用
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 overget()
is extremely inefficient, just callread()
.如果我正确理解你的问题,那么当你运行这个时,你会看到无限循环吗?
我很想知道每个循环之前的 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.