【发布时间】:2012-11-20 01:41:14
【问题描述】:
所以,我的问题很简单,我想创建一个函数来分隔文件中的单词并解析这些单词并为这些单词创建一个特殊的想法,但是,当我尝试分离时,我在 strcat 中出现错误代币:
看代码:
#include <stdio.h>
#include <string>
#include <Windows.h>
using namespace std;
bool compile(FILE *fp)
{
char c;
char token[256];
bool _return = false;
while ((c = getc(fp)) != EOF)
{
if(isalpha(c))
{
// The error is here
strcat(token, (char*)c);
printf("%c\n", c);
}
else
{
printf("----> %c\n", c);
}
}
printf("%s\n", token);
return _return;
}
int main()
{
char *filename = "test.in";
FILE *fp = fopen(filename, "rb");
if(!fp)
{
return 1;
}
if(!compile(fp))
{
printf("ERROR...\n");
}
fclose(fp);
}
代码正常构建,但是,当我调试时,调试器转到 strcat.asm 文件:( 所以,我的错误在哪里,谢谢你的帮助。 (:
-0-0-0-0-0-0-0-0-0-> 编辑 而且,当我将 strcat 更改为:
strcat(token, &c)
我从调试器收到此错误:
APP.exe 中 0x20E801F2 处未处理的异常:0xC0000005:访问 违规(参数:0x00000008)。
【问题讨论】:
-
令牌未初始化。在进入循环之前尝试
token[0] = 0;。 -
但是将字符转换为
char*是没有意义的,所以你还是坏了。 -
而且你不能对字符进行排序。所以要么做 token[1] = 0;在 strcat 之前。或者将 strcat 替换为 token[token_len++] = c;令牌[token_len] = 0;但请检查您没有超过令牌的缓冲区大小。