D
dmoran21
Hi All, I am working on a program to take input from a txt file, do
some calculations, and then output the results to another txt file.
The program that I've written compiles fine for me, however, when I
run it, it stalls and does nothing. I'm wondering if there's something
obvious that I'm missing. My code is below and any help would be
appreciated.
Thanks,
Dave
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
int main(int argc, char *argv[])
{
/* Variable Declarations */
const int arraylength = 5000;
const int errorcode = -1;
float sum = 0;
int count = 0;
int condition = 1;
float* precipvals = (float*)NULL;
float* precip3hrs = (float*)NULL;
long* date = (long*)NULL;
FILE *fpin;
FILE *fpout;
fpin = fopen(argv[1],"r+");
fpout = fopen(argv[2],"w");
/* Array Allocation */
precipvals = (float*)malloc(sizeof(int) * arraylength);
precip3hrs = (float*)malloc(sizeof(int) * arraylength);
date = (long*)malloc(sizeof(int) * arraylength);
/* Calculation Section */
while(condition != EOF) {
condition = fscanf(fpin,"%l,%f", &date[count], &precipvals[count]);
if(feof(fpin)) {
fclose(fpin);
return -3;
}
if(count == 1) {
precip3hrs[count] = precipvals[count];
}
if(count == 2) {
precip3hrs[count] = precipvals[count] + precip3hrs[count-1];
}
if(count > 2) {
precip3hrs[count] = precipvals[count] + precipvals[count-1] +
precipvals[count-2];
}
}
/* Output section */
for(count = 1; count <= arraylength; count++) {
if(count == 1) {
printf("One hour precipitation: %2.2f Three hour precipitation %2.2f
\n",precipvals[count],precip3hrs[count]);
}
if(count == 2) {
printf("One hour precipitation: %2.2f Three hour precipitation %2.2f
\n",precipvals[count],precip3hrs[count]);
}
if(count > 2) {
printf("One hour precipitation: %2.2f Three hour precipitation %2.2f
\n",precipvals[count],precip3hrs[count]);
}
}
/* Write to file */
for(count = 1; count <= arraylength; count++) {
if(count == 1) {
fprintf(fpout, "One hour precipitation: %2.2f Three hour
precipitation %2.2f\n",precipvals[count],precip3hrs[count]);
}
if(count == 2) {
fprintf(fpout,"One hour precipitation: %2.2f Three hour
precipitation %2.2f\n",precipvals[count],precip3hrs[count]);
}
if(count > 2) {
fprintf(fpout,"One hour precipitation: %2.2f Three hour
precipitation %2.2f\n",precipvals[count],precip3hrs[count]);
}
}
fclose(fpin);
fclose(fpout);
/* Deallocation */
free(date);
date = (long*)NULL;
free(precipvals);
precipvals = (float*)NULL;
free(precip3hrs);
precip3hrs = (float*)NULL;
return 0;
}
some calculations, and then output the results to another txt file.
The program that I've written compiles fine for me, however, when I
run it, it stalls and does nothing. I'm wondering if there's something
obvious that I'm missing. My code is below and any help would be
appreciated.
Thanks,
Dave
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
int main(int argc, char *argv[])
{
/* Variable Declarations */
const int arraylength = 5000;
const int errorcode = -1;
float sum = 0;
int count = 0;
int condition = 1;
float* precipvals = (float*)NULL;
float* precip3hrs = (float*)NULL;
long* date = (long*)NULL;
FILE *fpin;
FILE *fpout;
fpin = fopen(argv[1],"r+");
fpout = fopen(argv[2],"w");
/* Array Allocation */
precipvals = (float*)malloc(sizeof(int) * arraylength);
precip3hrs = (float*)malloc(sizeof(int) * arraylength);
date = (long*)malloc(sizeof(int) * arraylength);
/* Calculation Section */
while(condition != EOF) {
condition = fscanf(fpin,"%l,%f", &date[count], &precipvals[count]);
if(feof(fpin)) {
fclose(fpin);
return -3;
}
if(count == 1) {
precip3hrs[count] = precipvals[count];
}
if(count == 2) {
precip3hrs[count] = precipvals[count] + precip3hrs[count-1];
}
if(count > 2) {
precip3hrs[count] = precipvals[count] + precipvals[count-1] +
precipvals[count-2];
}
}
/* Output section */
for(count = 1; count <= arraylength; count++) {
if(count == 1) {
printf("One hour precipitation: %2.2f Three hour precipitation %2.2f
\n",precipvals[count],precip3hrs[count]);
}
if(count == 2) {
printf("One hour precipitation: %2.2f Three hour precipitation %2.2f
\n",precipvals[count],precip3hrs[count]);
}
if(count > 2) {
printf("One hour precipitation: %2.2f Three hour precipitation %2.2f
\n",precipvals[count],precip3hrs[count]);
}
}
/* Write to file */
for(count = 1; count <= arraylength; count++) {
if(count == 1) {
fprintf(fpout, "One hour precipitation: %2.2f Three hour
precipitation %2.2f\n",precipvals[count],precip3hrs[count]);
}
if(count == 2) {
fprintf(fpout,"One hour precipitation: %2.2f Three hour
precipitation %2.2f\n",precipvals[count],precip3hrs[count]);
}
if(count > 2) {
fprintf(fpout,"One hour precipitation: %2.2f Three hour
precipitation %2.2f\n",precipvals[count],precip3hrs[count]);
}
}
fclose(fpin);
fclose(fpout);
/* Deallocation */
free(date);
date = (long*)NULL;
free(precipvals);
precipvals = (float*)NULL;
free(precip3hrs);
precip3hrs = (float*)NULL;
return 0;
}