Visual C 的奇怪错误++
好吧,这是我的代码,到目前为止
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您的输入缓冲区有 10 个字符长
,但是您告诉 fgets 最多读取 30 个字符到缓冲区中,
这可能会产生“有趣”的效果!
Your input buffer is 10 chars long
But then you tell fgets to read up to 30 characters into the buffer
That is likely to have "funny" effects!
我通过目视检查发现的一件事是您的
holder
没有正确地以 null 终止。 添加一行。您可以在 while 循环后
One thing I recognized on visual inspection is that your
holder
is not properly null-terminated. You might add a lineafter your while-loop.
如果不了解您到底在做什么以及文件包含什么内容,就很难指出问题。
但我仍然建议一件事并尝试:
一般来说,确保所有 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:
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.