C++中的字符串溢出?这种持续不断的嘟嘟声很奇怪
我正在开始开发垃圾文件生成器,但由于某种原因,如果我使用大量数字,它会无限发出蜂鸣声,直到文件完成,我认为 ascii 表中的某处有一个 \a 字符,或者它溢出了并导致错误蜂鸣声。有人想解释一下为什么这东西对我尖叫吗?
#include <string>
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <fstream>
#include <time.h>
#include <windows.h>
#define print cout<<
using namespace std;
int numberof,i;
char charvalue;
string charlist,filename;
int main()
{
srand (time(NULL));
print "What do you want the name of your file to be?(with a .txt extension)\n";
getline(cin,filename);
print "\nHow many characters do you want?\n";
cin>>numberof;
for(numberof>0;numberof!=0;numberof--)
{
i = rand() % 255 + 32;
charvalue=i;
charlist=charlist+charvalue;
print charlist;
}
ofstream writefile(filename.c_str());
writefile<<charlist;
writefile.close();
ShellExecute(NULL, "open", filename.c_str(), NULL, NULL, SW_SHOWNORMAL);
return 0;
}
看起来此时字符最终已经正常显示,但它只将其中的 1/4 写入文本文件。有人知道为什么吗?
I'm startin development on a junk file generator, but for some reason if I use a large number it will beep infinitely until the file is finished, I'm thinking there is a \a character somewhere in the ascii table, or it's overflowing and causes an error beep. Anyone wanna explain why this thing is screaming at me?
#include <string>
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <fstream>
#include <time.h>
#include <windows.h>
#define print cout<<
using namespace std;
int numberof,i;
char charvalue;
string charlist,filename;
int main()
{
srand (time(NULL));
print "What do you want the name of your file to be?(with a .txt extension)\n";
getline(cin,filename);
print "\nHow many characters do you want?\n";
cin>>numberof;
for(numberof>0;numberof!=0;numberof--)
{
i = rand() % 255 + 32;
charvalue=i;
charlist=charlist+charvalue;
print charlist;
}
ofstream writefile(filename.c_str());
writefile<<charlist;
writefile.close();
ShellExecute(NULL, "open", filename.c_str(), NULL, NULL, SW_SHOWNORMAL);
return 0;
}
Seems like at this point the characters are coming out alright in the end, but it only writes 1/4 of them to the text file. Anyone know why?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
您可能希望它是这样的:
i = rand() % (255-32) + 32;
你也真的想要摆脱这个:
按照目前的情况,当(不是如果)给你作业评分的人决定杀了你时,他(她?)几乎肯定会被判无谋杀罪理由是这是自卫。
You probably want this to be something like:
i = rand() % (255-32) + 32;
You also really want to get rid of this:
As it stands, when (not if) whoever is grading your homework decides to kill you, he (she?) will almost certainly be found not guilty of murder on the grounds that it was self defense.
你想说
You meant to say
,我相信它位于位置 7。
另外,这个:
应该是这样的:
条件在中间,并且不需要任何初始化,因此在 for 循环的开头有一个空语句。
另外,可打印的 ASCII 字符范围仅为 32 到 126,因此您应该这样写:
另外,下面的代码效率极低,因为它每次都会生成一个新的字符串对象:
改为这样做:
Yes, it is in position 7, I believe.
Also, this:
should be this:
The condition goes in the middle, and you do not need any initialization, hence the empty statement at the beginning of the for-loop.
Also, the printable ASCII characters only range from 32 to 126, so you should write:
Also, the following is extremely inefficient, as it generates a new string object every time:
Do this instead:
事实上,您每次都打印整个字符列表,这意味着只要您在输出字符串中生成至少几个
\a
字符(其几率为'太糟糕了),除了不断的蜂鸣声之外,你什么也得不到。The fact that you're printing the entire charlist every time means as fast as you can means that as soon as you generate at least a couple
\a
characters in the output string (the odds of which aren't too bad), you will get nothing but incessant beeping.删除
print charlist
这样就不会打印出响铃字符。Remove the
print charlist
so you're not printing the bell characters out.