In article <11**********************@g37g2000prf.googlegroups .com>,
MJK <ja*******@gmail.comwrote:
//suppose I have N line each with M digits (in here M=5) like: 1 2 2 1 3
fscanf(ext,"%s",str); //This line read the whole 5 digits
The %s format does not read an entire line: it skips leading whitespace,
then read the next string of non-whitespace, leaving the terminating
whitespace in the input buffer. Thus, the above statement would read only
1 of the digits, not all five (you show whitespace between the digits
in your example.)
Also, it is generally dangerous to use a %s format by itself,
if you do not have perfect control over the input, as someone might
choose to input a longer input string than there is space to store
in your str variable. Either do not use fscanf() for your reading, or
else put in an explicit length limitation in the format, such as
"%.254s"
//I want each of those five digits become an element of
"AM[i].S[j]" array as below:
for(j=0; j<M; ++j)
{
c=fgetc(ext);
Your comment on the fscanf() indicates that you believe you have
already read in the entire line (into the buffer str), but here
you are trying to read more from the file. Also, you are not skipping
over whitespace when you use fgetc().
AM[i].S[j]=int(c);
There is no 'int' function in C, and that would not be the correct
syntax for a type conversion.
If you know that c is a single character representing a numeric
digit, then the short portable way to extract the numeric value
it represents is to use c - '0' (subtract the quoted 0 character
from c). C promises that this will work no matter what character set
you are using, even if the character set is EBCDIC . In the
more general multi-character case such converting the string "42" to the
number 42, there are several choices on how to proceed, with various
tradeoffs.
--
I was very young in those days, but I was also rather dim.
-- Christopher Priest