-
problem with output
I have just started to write this program and the problem is the output. I'm not sure if I'm setting the char array, because it looks like I'm not. The output is just numerous "=". Can you take a look at it? Here is the code:
Code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct problem {
char answer[100];
char question[1024];
struct problem *next_prob;
};
// function prototypes
struct problem *getques(FILE *, struct problem *);
int getquestans(char question[], char ans[], FILE *fp);
int main(int argc, char *argv[])
{
FILE *fp;
struct problem *head_prob; // head struct to pass to function
head_prob = NULL;
fp = fopen(argv[1], "r"); // open the file to read
if (fp == NULL)
{
// file not found
printf("File not found!\n");
return 0;
}
else
head_prob = getques(fp, head_prob);
printf("question: %s\n", head_prob->question);
return 0;
}
struct problem *getques(FILE *fp, struct problem *head_prob)
{
int yesno;
struct problem *temp;
temp = malloc(sizeof(struct problem));
head_prob = malloc(sizeof(struct problem));
head_prob->next_prob = NULL;
temp->next_prob = NULL;
printf("**while loop\n");
while (yesno != EOF)
{
temp->next_prob = NULL;
yesno = getquestans(head_prob->question, head_prob->answer, fp);
printf("**after fgets\n");
printf("**before next_prob\n");
head_prob->next_prob = temp;
printf("**head_prob->next_prob = temp\n");
head_prob = head_prob->next_prob;
printf("**successful next prob\n");
}
printf("**return successful\n");
return head_prob;
}
int getquestans(char *question, char *answer, FILE *fp)
{
char ch, q=0, a=0, i;
while (ch != '\n')
{
if (ch == EOF)
return EOF;
ch = fgetc(fp);
if (ch == ':')
{
i = 1;
continue;
}
else if (i == 0)
{
question[q] = ch;
q++;
}
else if (i == 1)
{
answer[a] = ch;
a++;
}
}
return 0;
}
Don't mind some of the printf calls. They are just there to pin-point the problem. But, I can't find it.
Thanks.
--Garfield
-
Have a look at the following code and compare it to your original code.
In future you may want to actually explain the problem. I've made a few changes.........
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct problem {
char answer[100];
char question[1024];
struct problem *next_prob;
};
// function prototypes
struct problem *getques(FILE *);
int getquestans(char question[], char ans[], FILE *fp);
int main(int argc, char *argv[])
{
FILE *fp = NULL;
struct problem *head_prob = NULL,
*tempPtr;
head_prob = NULL;
if ( argc != 2 ) /* check we have been given a parameter !!!! */
{
printf("No File Specified!\n");
return 0;
}
fp = fopen(argv[1], "r"); // open the file to read
if (fp == NULL)
{
// file not found
printf("File not found!\n");
return 0;
}
head_prob = getques(fp);
tempPtr = head_prob;
while ( tempPtr != NULL )
{
printf("question: %s\nanswer: %s\n++++++++++\n", tempPtr->question, tempPtr->answer);
tempPtr = tempPtr->next_prob;
}
return 0;
}
struct problem *getques(FILE *fp)
{
int yesno;
struct problem *head = NULL, /* initialise this one !!!! */
*temp;
do
{
/* allocate a temp structure to store the question (if any) */
temp = malloc(sizeof(struct problem));
yesno = getquestans(temp->question, temp->answer, fp);
if ( yesno != EOF )
{
/* add the structure to the stack */
temp->next_prob = head;
head = temp;
}
else /* yesno == EOF */
{
/* we didn't get a question, so we'll free the structure */
free(temp);
}
}
while ( yesno != EOF );
return head;
}
int getquestans(char *question, char *answer, FILE *fp)
{
char ch,
q=0,
a=0,
i=0;
do
{
ch = fgetc(fp);
if ( (ch != EOF) && (ch != '\n') )
{
if (ch == ':')
{
i = 1;
}
else if (i == 0)
{
question[q] = ch;
q++;
}
else if (i == 1)
{
answer[a] = ch;
a++;
}
}
} while ( (ch != '\n') && (ch != EOF) );
/* null terminate the output strings */
question[q] = '\0';
answer[a] = '\0';
/* we'll only return EOF if we don't have a valid question/answer, as
the last q/a may not have a terminating EOL before the EOF */
if ( (ch == EOF) && (!a) )
return EOF;
return 0;
}
-
Thanks, I'll take a look at it and try to work with it!
--Garfield