[英]Read characters from a file. C language
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. 这是我的代码。版本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.. 但这仅适用于%d
..仅数字..
I actually doesn't know the proper way to get a single character from file using fscanf
, especially the %
thing. 我实际上不知道使用fscanf
从文件中获取单个字符的正确方法,尤其是%
东西。
Tkz.. Tkz ..
要获取字符,您将需要使用%c
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.