This is the file.
X 1 0 1
1 X X 1
1 0 1 0
1 1 0 X
0 X 1 1
1 X 1 0
This is my code.. Version 1.
char cha2[1];
int patternStored[180];
for(a=0;a<(numberOfPatterns*12);a++)
{
fscanf(patternMatFile,"%1s",cha2);
if(cha2[0]=='X')
{
patternStored[a]=2;
}
else
{
patternStored[a]=atoi(cha2);
}
}
The purpose of the code is to read a single character at a time and save it into the array
the above code works fine but..
at the end of the loop, i am having this error
Run-Time Check Failure #2 - Stack around the variable 'cha2' was corrupted.
This is the second version.
char cha[4];
int patternStored[180];
for(a=0;a<(numberOfPatterns*12);a++)
{
fscanf(patternMatFile,"%c",cha);
if(cha[0]=='X')
{
patternStored[a]=2;
}
else
{
patternStored[a]=atoi(cha);
}
}
The second version doesn’t have error. But it only works with %d.. only digits..
I actually doesn’t know the proper way to get a single character from file using fscanf, especially the % thing.
Tkz..
To get a char, you’ll want to use
%c.