Here's part of my code.
It's the file I/O that I can't seem to master.
I added all those print statements under openFile so I could see that it was working.
Once I read from the file how do I load the stacks I'm using?
Shouldn't what I wrote work?
It compiles, but I get an error saying lhs->nums not defined.
I have all my other functions witten. What am I doing wrong?
Code:
typedef struct{
int *nums;
int top;
int max;
}stack;
typedef enum{TRUE, FALSE}Bool;
int push(stack *final, int num);
int pop(stack *final);
/*function to add stacks*/
void add(stack *fst, stack *snd, stack *ans);
/*function to multiply stacks, includes negative numbers*/
void multiply(stack *fst, stack *snd, stack *ans);
/*Prompt user for the file name,
it is guaranteed they will select the correct one*/
void GetFile(char *fileName);
/*Open and read the file requested*/
void openFile(char *fileName, int *numSets);
int main(void)
{
char fileName[100];
int final[100], stack1[100], stack2[100], numSets;
stack rhs, lhs, ans;
rhs.max = lhs.max = ans.max = 100;
rhs.top = lhs.top = ans.top = -1;
rhs.nums = stack1;
lhs.nums = stack2;
ans.nums = final;
GetFile(fileName);
openFile(fileName,&numSets);
}//end main
void GetFile(char *fileName)
{
printf("Please enter the file name!\n");
printf("Choices are: data.txt or bonus.txt\n");
scanf("%s", fileName);
}
void openFile(char *fileName, int *numSets)
{
int i, ch, k=0, j=0, digit=0, test=0;
stack *rhs, *lhs, *ans;
char Temp[100];
FILE *fp = fopen( fileName, "r");
//error check
if(fp == NULL)
{
fprintf(stderr, "Not quite what we expected!!\nHave a Nice Day!\n", fileName);
exit(-1);
}
//Count number of sets
fscanf( fp, "%d\n" , numSets);
*numSets = *numSets * 2;
for(i=0; i< *numSets; i++)
{
j = i;
fscanf(fp, "%s", &Temp[i]);
while(Temp[j] != '\0')
{
printf("\nthis character is ' %c ' ",Temp[j]);
ch = Temp[j];
digit = ch - '0';
printf("\ndigit = %d", digit);
if(test==0)
push(rhs->nums, digit);
else
push(lhs->nums, digit);
j++;
}
test +=1;
printf("\n\n");
if(test == 1)
{
test = 0;
add(rhs->nums, lhs->nums, ans);
}
}
fclose(fp);