I would like to use fscanf
to process each row of a file, in which the matrix I do not know one line for each matrix line, one priority , how many columns or rows are matrix in any file.
It is okay to deal with an unknown number of rows, because it is easy to stop the loop when it reaches the end of the file. For example:
while (.fiFile) {if (fscanf (file, "% f", & amp; f) == 1) {printf ("% f \ N ", f); }}
But I should know which row belongs to the variable f
for example:
N_row = 0; While (.fif (file) {if (fscanf (file, "% f", & amp; F) == 1) {printf ("one value is% f line% d \ n", f, n_row) ; [If you find "\ n"] {// increase the line counter n_row ++; }}}
How is it possible to do this?
A.
There are two ways to do this:
- Your input line- Read the By-Line from
fscanf
, and then split it from the line buffer into either individual numbers using eithersscanf
orstrtok_r
Or - Try reading one of the characters after
float
, and check that the\ n
is for that character.
How the other way can work:
while (! Feof (file)) {Next; Int count = fscanf (file, "% f% c", and amp; f, and next); If (calculation> gt; 0) {// there can not be '\ n' on the last line, // in which case the count is equal to 1. If (count == 1 || next == '\ n') {// then increase the line counter n_row ++; } Printf ("value is% f line% d \ n", f, n_row); }}
The second approach corresponds to the solution that you wanted to see, the first approach is stronger.
Comments
Post a Comment