
Need help with input from text file
Hi, I have a program that reads its commands and arguments from a text
file. The commands call functions in my program that accept the arguments. The
text file is formatted like this:
c_n_e: 123456789 300
r_s: 34 999999999
r_s: 888888888
.
.
.
where c_n_e accepts a long and an int and r_s accepts two longs. The commands
do not exceed 6 characters. My program to test my input routine looks like this:
#include <stdio.h>
int main()
{
char option[7]; /*to read in the command*/
int i,j,seats,sum,x=0; /*seats stores the 2nd arg of c_n_e*/
long EVNT,ssn; /*EVNT stores 1st arg of c_n_e or r_
ssn stores 2nd arg of r_s*/
FILE *fp;
fp=fopen("data.txt","r");
do{
sum=0;
for(j=0;j<7;j++)
option[j]=NULL;
fscanf(fp,"%s", option);
for(i=1;i<7;i++) /*Here I read in the command as a
switch(option[i]){ string. I process the string by
case 'c': using assigned values for each
sum+=1; letter. The sum will decide
break; how I will scan in the arguments
case 'n': and later on which function to
sum+=2; call*/
break;
case 'e':
sum+=3;
break;
case 'r':
sum+=4;
break;
case 's':
sum+=5;
break;
}
if(sum==5){
fscanf(fp,"%ld%d",&EVNT,&seats); /*This is for c_n_e*/
printf("%ld\n", EVNT);
printf("%d\n", seats);
}
if(sum==9){
fscanf(fp,"%ld%ld", &EVNT,&ssn); /*This is for r_s*/
printf("%ld\n",EVNT);
printf("%ld\n",ssn);
}
x++;
}while(x<5);
return 0;
Quote:
}
My problem is dealing with cases like:
r_s: 888888888
where my input routine expects 2 arguments but where the text
file sends only 1 argument. I know my routine will read the 888888888 as the
first argument although it is suppose to be the second and read in something
strange for the second argument. Can anyone suggest a better way of getting the
input from the text file or how I should change my routine.
Thanks for any help.
kristopher m. huynh
http://www.*-*-*.com/ ~axs03425/