C++中的字符串溢出?这种持续不断的嘟嘟声很奇怪

发布于 2024-10-04 11:09:31 字数 1072 浏览 7 评论 0原文

我正在开始开发垃圾文件生成器,但由于某种原因,如果我使用大量数字,它会无限发出蜂鸣声,直到文件完成,我认为 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 技术交流群。

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

发布评论

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

评论(5

怪异←思 2024-10-11 11:09:31

i = rand() % 255 + 32;

您可能希望它是这样的:

i = rand() % (255-32) + 32;

你也真的想要摆脱这个:

#define print cout<<

按照目前的情况,当(不是如果)给你作业评分的人决定杀了你时,他(她?)几乎肯定会被判无谋杀罪理由是这是自卫。

i = rand() % 255 + 32;

You probably want this to be something like:

i = rand() % (255-32) + 32;

You also really want to get rid of this:

#define print cout<<

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.

你与清晨阳光 2024-10-11 11:09:31

你想说

i = rand() % 223 + 32;

You meant to say

i = rand() % 223 + 32;
甜心 2024-10-11 11:09:31

我认为 ascii 表中某处有一个 \a 字符

,我相信它位于位置 7。

另外,这个:

for(numberof>0;numberof!=0;numberof--)

应该是这样的:

 for(; numberof > 0; numberof--)

条件在中间,并且不需要任何初始化,因此在 for 循环的开头有一个空语句。

另外,可打印的 ASCII 字符范围仅为 32 到 126,因此您应该这样写:

i = rand() % 95 + 32;

另外,下面的代码效率极低,因为它每次都会生成一个新的字符串对象:

charlist=charlist+charvalue

改为这样做:

charlist.push_back(charvalue);

I'm thinking there is a \a character somewhere in the ascii table

Yes, it is in position 7, I believe.

Also, this:

for(numberof>0;numberof!=0;numberof--)

should be this:

 for(; numberof > 0; numberof--)

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:

i = rand() % 95 + 32;

Also, the following is extremely inefficient, as it generates a new string object every time:

charlist=charlist+charvalue

Do this instead:

charlist.push_back(charvalue);
再可℃爱ぅ一点好了 2024-10-11 11:09:31

事实上,您每次都打印整个字符列表,这意味着只要您在输出字符串中生成至少几个 \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.

萌辣 2024-10-11 11:09:31

删除 print charlist 这样就不会打印出响铃字符。

Remove the print charlist so you're not printing the bell characters out.

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