So, my problem is simple, I whant to create a function to separate the words in the file and parse those words and create an special think for those words, but, I have an error in strcat, when I try to separate the tokens:
look the code:
#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);
}
THe code builds normally, but, when I debug, the debugger goes to strcat.asm file 🙁
So, where is my error, thanks for the help. (:
-0-0-0-0-0-0-0-0-0-> edit
And, when I change the strcat to:
strcat(token, &c)
I receive this error from the debugger:
Unhandled exception at 0x20E801F2 in APP.exe: 0xC0000005: Access
violation (parameters: 0x00000008).
strcatexpects a null-terminated string; you cannot pass a pointer to an individual character and expect it to work correctly.Replace
with
to make it work.
You also need to set
token[0]to zero to avoid undefined behavior due to it not being initialized.