Visual C 的奇怪错误++

发布于 2024-11-07 09:53:34 字数 1277 浏览 0 评论 0原文

好吧,这是我的代码,到目前为止

int main()
{
    char buffer[10];
    int arraySize = -1;
    FILE *fp;
    int i;  
    char filename[10];
    int userNo = 1;
    char stockArray[18][15];
    sprintf(filename, "file%d", userNo);
    fp = fopen(filename, "r");
    while(fgets(buffer, 30, fp) != NULL)
    { 
        if(buffer[0] == '<' && buffer[1] == 's')
        {
            arraySize++;
        }
        else if(buffer[0] == '<' && buffer[1] == '/'){printf("< char\n");}
        else
        {
            int t = 0;
            int r = 0;
            while(buffer[t] != '>')
            {
                t++;
            }
            t++;
            char holder[15] = {'\0'};
            while(buffer[t] != '<')
            {
                holder[r] = buffer[t];
                t++;
                r++;
            }
            strncpy(stockArray[arraySize], holder, r);
            printf("%s\n", stockArray[arraySize]);
        }
    }
    fclose(fp);
}

我遇到了两个奇怪的问题。首先,当我执行 printf 语句时,它会很好地打印正确的数据,然后执行以下操作:如果第一个单词是“banana”,下一个是“123”,它会打印“123ana”,然后打印一堆奇怪的字符最后,我没有骗你,一张笑脸。

然后,在程序完成并完成后,我收到“运行时检查失败#2 - 变量“文件名”周围的堆栈已损坏”。错误。

我使用的是 VS2010 和 C++,到目前为止我所有的经验都是使用 GCC 和 C,以前从未遇到过这些问题。任何建议将不胜感激

Alright, this is my code so far

int main()
{
    char buffer[10];
    int arraySize = -1;
    FILE *fp;
    int i;  
    char filename[10];
    int userNo = 1;
    char stockArray[18][15];
    sprintf(filename, "file%d", userNo);
    fp = fopen(filename, "r");
    while(fgets(buffer, 30, fp) != NULL)
    { 
        if(buffer[0] == '<' && buffer[1] == 's')
        {
            arraySize++;
        }
        else if(buffer[0] == '<' && buffer[1] == '/'){printf("< char\n");}
        else
        {
            int t = 0;
            int r = 0;
            while(buffer[t] != '>')
            {
                t++;
            }
            t++;
            char holder[15] = {'\0'};
            while(buffer[t] != '<')
            {
                holder[r] = buffer[t];
                t++;
                r++;
            }
            strncpy(stockArray[arraySize], holder, r);
            printf("%s\n", stockArray[arraySize]);
        }
    }
    fclose(fp);
}

I'm running into two strange issues. First, when I do the printf statement, it prints the proper data just fine, then does the following: if the first word is "banana" and the next is "123" it prints "123ana" and then a bunch of weird character that ends with, I kid you not, a smiley face.

Then, after the program is done and finished, I get a "Run-Time Check Failure #2 - Stack around the variable 'filename' was corrupted." error.

I'm using VS2010 and C++, and all my experience thus far has been with GCC and C, where I've never encountered these problems before. Any advice would be appreciated

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

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

发布评论

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

评论(3

咿呀咿呀哟 2024-11-14 09:53:34

您的输入缓冲区有 10 个字符长

char buffer[10];

,但是您告诉 fgets 最多读取 30 个字符到缓冲区中,

fgets(buffer, 30, fp)

这可能会产生“有趣”的效果!

Your input buffer is 10 chars long

char buffer[10];

But then you tell fgets to read up to 30 characters into the buffer

fgets(buffer, 30, fp)

That is likely to have "funny" effects!

奈何桥上唱咆哮 2024-11-14 09:53:34

我通过目视检查发现的一件事是您的 holder 没有正确地以 null 终止。 添加一行。

holder[r++] = '\0';

您可以在 while 循环后

One thing I recognized on visual inspection is that your holder is not properly null-terminated. You might add a line

holder[r++] = '\0';

after your while-loop.

铁轨上的流浪者 2024-11-14 09:53:34

如果不了解您到底在做什么以及文件包含什么内容,就很难指出问题。

但我仍然建议一件事并尝试:

strncpy(stockArray[arraySize], holder, r);
stockArray[arraySize][r] = '\0'; //do this before printf!

一般来说,确保所有 C 字符串都以 null 结尾。否则您将面临由于缺少空字符而出现的常见问题。

Its very difficult to point out the problem, without understanding what exactly you're doing, and what the file contains.

But I still suggest one thing and try:

strncpy(stockArray[arraySize], holder, r);
stockArray[arraySize][r] = '\0'; //do this before printf!

In general, make sure all your c-strings are null-terminated. Or else you will face the usual problem arises due to lack of null character.

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