So, I have posted here before and you have all been a great help. My problem is that I am still a really bad programmer.
I am attempting to read in data from a text file and have a couple of questions.
1) How do I read in data, but exclude the quotation marks and commas?
2) How do I add an eof statement to stop my endless while loop.
THANKS!!
Sample of my text file:
Code:
"#2",,0,130.00,170.00
"#2ACSR",,0,160.00,230.00
"#2AL",,0,168.00,230.00
"#2CU",,0,216.00,294.00
Here is my code:
Code:
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <time.h>
#define MAX_NUMBER_CONDUCTORS 26
#define COND_DATA_LENGTH 5
#define COND_FILENAME "tblConductor.txt"
#define LOG_FILENAME "DCA.log"
static FILE *log_file;
static FILE *cond_file;
int main(void)
{
char cond_name[ MAX_NUMBER_CONDUCTORS ][ COND_DATA_LENGTH ];
int ud_flag[ MAX_NUMBER_CONDUCTORS ];
float summer_amp[ MAX_NUMBER_CONDUCTORS ];
float winter_amp[ MAX_NUMBER_CONDUCTORS ];
struct tm *newtime;
time_t aclock;
// open logging file
if ((log_file = fopen( LOG_FILENAME, "a+" )) == NULL)
fprintf(stderr, "\nERROR - Cannot open <%s>\n", LOG_FILENAME);
else {
// write start-up message
time( &aclock );
newtime = localtime( &aclock );
fprintf(log_file, "\nDCA v1.0 BEGINNING %s\n",asctime(newtime));
fprintf(stderr, "\nDCA v1.0 BEGINNING %s\n", asctime(newtime));
// close logging file
fclose (log_file);
}
// open conductor file
cond_file = fopen( COND_FILENAME, "r" );
int i = 0;
while ( cond_file != NULL ) {
fscanf( cond_file, "%s,,%d,%f,%f", cond_name[i], &ud_flag[i], &summer_amp[i],
&winter_amp[i] );
if ((log_file = fopen( LOG_FILENAME, "a+" )) == NULL)
fprintf(stderr, "\nERROR - Cannot open <%s>\n", LOG_FILENAME);
else {
// open log file again
fprintf(log_file, "Conductor Name: %s UD Flag: %d Summer Amp: %f Winter Amp: %f\n",
cond_name[i], ud_flag[i], summer_amp[i], winter_amp[i]);
// close logging file
fclose (log_file);
}
printf( "Conductor Name: %s UD Flag: %d Summer Amp: %f Winter Amp: %f\n",
cond_name[i], ud_flag[i], summer_amp[i], winter_amp[i]);
i++;
}
fclose( cond_file );
return(0);
}