Article 57D15 fscanf(...) not completing the read

fscanf(...) not completing the read

by
bkelly13
from LinuxQuestions.org on (#57D15)
The goal is to read a CSV file and put the values into variables. I have extracted the misbehaving code to a test program and it manifests the problem. The printf at line 79 looks good, but the next printf at line 79 shows only one character read. I am expecting a lot more. The same concepts applies within the for loop.
Conclusion: There is something about the fscanf(...) that I do not understand. Here is the code.

Code:#include <stdio.h>
#include <string.h>
int main( )
{
FILE * multi_column_file;
char multi_column_buffer[ 256 ];
char multi_column_name[] = "td_short.csv";

struct DATA_ROW
{
int row;
float time;
float sine_1;
float sine_2;
float sine_3;
float sine_4;
float sine_5;
float composite;
};

DATA_ROW * m_test_data;

const int COLUMN_COUNT = 8; // total number of columns in the file
char c_sample_count[ 32 ] = "no";
char c_column_count[ 32 ] = "no";
char c_sample_rate[ 32 ] = "no";
char c_hz[ 32 ] = "no";
char c_index[ 32 ] = "no";
char column_headers[ COLUMN_COUNT ][ 32 ];

int field_count = 0;
int sample_count = 0;
int column_count = 0;
float sample_rate = 0.0;

m_test_data = new DATA_ROW[ 10 ];

DATA_ROW one_row;

multi_column_file = fopen( multi_column_name, "r");

field_count = fscanf( multi_column_file, "%s %d %s %d %s %f %s",
&c_sample_count, &sample_count,
&c_column_count, &column_count,
&c_sample_rate, &sample_rate,
&c_hz );

printf( "\n%4d %s %d %s %d %s %f %s",
__LINE__,
c_sample_count, sample_count,
c_column_count, column_count,
c_sample_rate, sample_rate,
c_hz );
if( column_count != COLUMN_COUNT )
{
printf( "\n%4d Expected %d columns, found %d, cannot run",
__LINE__, COLUMN_COUNT, column_count );
}

size_t len = 0;
ssize_t read = 0;;
char * line = NULL;

for( int i = 0; i < 2; i ++ )
{
read = getline( &line, &len, multi_column_file );
if( read == -1 )
{
printf( "\n%4d read d", __LINE__, read );
}
else
{
int t = strlen( line );
printf( "\n%4d read %d t %d", __LINE__, read, t );
printf( "\n%4d text is <%s> text done", __LINE__, line);
}
}
delete line;

int row_number;
float time = 0.0;
float sine_1 = 1.0;
float sine_2 = 2.0;
float sine_3 = 3.0;
float sine_4 = 4.0;
float sine_5 = 5.0;
float composite = 6.0;

char a[ 16 ] = "no";
char b[ 16 ] = "no";
char c[ 16 ] = "no";
char d[ 16 ] = "no";
char e[ 16 ] = "no";
char f[ 16 ] = "no";
char g[ 16 ] = "no";
char h[ 16 ] = "no";

for( int i = 0; i < 5; i ++ )
{
// This should read a line
field_count = fscanf( multi_column_file, "%s %s %s %s %s %s %s %s",
&a, &b, &c, &d, &e, &f, &g, &h );
printf( "\n%4d field_count %4d text %s %s %s %s %s %s %s %s",
__LINE__, field_count, a, b, c, d, e, f, g, h );

#if 0
row_number = 2 * i;
// This should read the next line, it does not
field_count = fscanf( multi_column_file, "%d %f %f %f %f %f %f %f",
&row_number, &time, &sine_1, &sine_2, &sine_3, &sine_4, &sine_5, &composite );

printf( "\n%4d field_count %4d values %d %f %f %f %f %f %f %f ",
__LINE__, field_count,
row_number, time, sine_1, sine_2, sine_3, sine_4, sine_5, composite );
#endif

}

if( multi_column_file ) fclose( multi_column_file );

printf( "\n%4d Exit\n", __LINE__ );


return 0;
}And here is a short example of the data to read.

Code:sample_count 16 column_count 8 SAMPLE_RATE 1200.00 Hz
i, time , frequency_0020_Hz, frequency_0040_Hz, frequency_0060_Hz, frequency_0080_Hz, frequency_0100_Hz, composite
0, 0.0001, 0.0002, 0.0003, 0.0004, 0.0005, 0.0006, 0.0007
1, 0.0008, 5.2264, 8.3165, 9.2705, 8.1347, 20.0000, 50.9481
2, 0.0017, 10.3956, 16.2695, 17.6336, 14.8629, 34.6410, 93.8025
3, 0.0025, 15.4508, 23.5114, 24.2705, 19.0211, 40.0000, 122.2539
4, 0.0033, 20.3368, 29.7258, 28.5317, 19.8904, 34.6410, 133.1258
5, 0.0042, 25.0000, 34.6410, 30.0000, 17.3205, 20.0000, 126.9615
6, 0.0050, 29.3893, 38.0423, 28.5317, 11.7557, 0.0000, 107.7189
7, 0.0058, 33.4565, 39.7809, 24.2705, 4.1582, -20.0000, 81.6661
8, 0.0067, 37.1572, 39.7809, 17.6336, -4.1582, -34.6410, 55.7724
9, 0.0075, 40.4508, 38.0423, 9.2705, -11.7557, -40.0000, 36.0079
10, 0.0083, 43.3013, 34.6410, 0.0000, -17.3205, -34.6410, 25.9808
11, 0.0092, 45.6773, 29.7258, -9.2705, -19.8904, -20.0000, 26.2421
12, 0.0100, 47.5528, 23.5114, -17.6336, -19.0211, -0.0000, 34.4095
13, 0.0108, 48.9074, 16.2695, -24.2705, -14.8629, 20.0000, 46.0434
14, 0.0117, 49.7261, 8.3165, -28.5317, -8.1347, 34.6410, 56.0172
15, 0.0125, 50.0000, 0.0000, -30.0000, -0.0000, 40.0000, 60.0000
16, 0.0133, 49.7261, -8.3165, -28.5317, 8.1347, 34.6410, 55.6537latest?d=yIl2AUoC8zA latest?i=fMa4Qny72dg:K52t3wO7sEE:F7zBnMy latest?i=fMa4Qny72dg:K52t3wO7sEE:V_sGLiP latest?d=qj6IDK7rITs latest?i=fMa4Qny72dg:K52t3wO7sEE:gIN9vFwfMa4Qny72dg
External Content
Source RSS or Atom Feed
Feed Location https://feeds.feedburner.com/linuxquestions/latest
Feed Title LinuxQuestions.org
Feed Link https://www.linuxquestions.org/questions/
Reply 0 comments