problem with output

This is a discussion on problem with output within the C Programming forums, part of the General Programming Boards category; I have just started to write this program and the problem is the output. I'm not sure if I'm setting ...

  1. #1
    the Corvetter
    Join Date
    Sep 2001
    Posts
    1,584

    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
    Attached Files Attached Files
    1978 Silver Anniversary Corvette

  2. #2
    Registered User
    Join Date
    Aug 2001
    Posts
    6
    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;
    }

  3. #3
    the Corvetter
    Join Date
    Sep 2001
    Posts
    1,584
    Thanks, I'll take a look at it and try to work with it!

    --Garfield
    1978 Silver Anniversary Corvette

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem with my output
    By Dogmasur in forum C Programming
    Replies: 17
    Last Post: 08-07-2008, 08:07 PM
  2. output from bank type problem
    By IzaakF in forum C Programming
    Replies: 2
    Last Post: 09-04-2002, 06:42 PM
  3. File input output problem
    By VanJay011379 in forum C++ Programming
    Replies: 2
    Last Post: 08-30-2002, 02:09 PM
  4. String Output Problem
    By Yin in forum C++ Programming
    Replies: 3
    Last Post: 03-14-2002, 06:36 AM
  5. Problem with output
    By Unregistered in forum C Programming
    Replies: 6
    Last Post: 12-11-2001, 07:32 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21