It's a good thing I didn't close it, even if I could. I have another question: will you helpful souls check out my format string and data struct. It's my first time using either of these concepts in C and I'm having trouble understanding the formatting for fscanf. I was attempting to use this sample c - How to read specifically formatted data from a file? - Stack Overflow program to help form my formatting inside fscanf, but I'm reading out garbage. I was hoping ultimately someone could help me understand how to pull the different data types out of the stream, especially when quotes are surrounding the information.
Here is my program so far. I'm just testing out my fscanf function at the time being (note: there are some artifacts floating around from me messing around, don't worry if I have unused variables.):
Code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "record.h"
#define MAXRECORDS 5000
int main()
{
const char * readfile = "stateoutflow.txt";
const char * writefile = "stateoutflow-report.txt";
const char * read_mode = "r";
const char * write_mode = "w";
FILE * datafile , * writeout;
if( ( datafile = fopen( readfile , read_mode ) ) == NULL )
{
printf( "unable to open file \"%s\" with mode \"%s\"...quitting\n" , readfile , read_mode );
return( 1 );
}
writeout = fopen( writefile , write_mode );
OutflowRecord mainstorage;
char charbuf[ 50 ];
unsigned int n = 0;
/* this while loop discards the first line of headers */
while( ( fscanf( datafile , "%s" , charbuf ) == 1 ) && ( n++ < 9 ) )
;
fscanf( datafile ,
"\"%u\" " // store:state code origin number
"\"%*d\" " // discard:county code origin number
"\"%*d\" " // discard:state code dest number
"\"%*d\" " // discard:county code dest number
"\"*2c\" " // discard:state abbreviation
"\"%[^\"]\" " // store:state name
"%*d " // discard:return number
"%u " // store:exemption number
"%*d " , // discard:aggr_agi
&mainstorage.st_origin_code , mainstorage.dest_state_name , &mainstorage.exmpt_num );
printf( "State Code of Origin: %u\nName of Destination State: %s\nNumber of Exemptions: %u\n" , mainstorage.st_origin_code , mainstorage.dest_state_name , mainstorage.exmpt_num );
fclose( datafile );
return 0;
}
And record.h:
Code:
#define MAXLEN 23
typedef struct StateOutwardFlowRecords
{
unsigned int st_origin_code;
char dest_state_name[ MAXLEN ];
unsigned int exmpt_num;
} OutflowRecord;
The file being used for reading is attached to the original post.
Another bit of information; when I run the program, I get different results each time for the "State origin code".