Thread: Need help scanning input file into queue array

  1. #1
    Registered User
    Join Date
    Sep 2012
    Posts
    21

    Need help scanning input file into queue array

    Hello everyone, I have an input file that has 2 lines of text. The two lines represent two different "hands" for playing cards. The lines of text are a number (1-13, Jack =11, Queen =12, King =13) then a space then a letter for the suit of the card S D C H
    Example would be
    11 D 5 H 3 S
    12 S 8 C 7 C

    How do I go about scanning the input file into a queue array?

  2. #2
    Registered User
    Join Date
    Sep 2012
    Posts
    21
    A little more info, the struct I am using for cards is
    Code:
    struct card {
       int value;
       char suit;
    };
    and my queue is
    Code:
    struct queue {
    int front;
    int numElements;
    int size;
    };

  3. #3
    Registered User
    Join Date
    May 2012
    Posts
    1,066
    For formatted data like yours a good choice would be fscanf().
    Since one card is a decimal number followed by space followed by a character I'd use
    Code:
    fscanf(fp, "%d %c", &rank, &suit)
    to read it.

    A safer approach would be reading one line from the input file using fgets() and then scanning the resulting string with sscanf() using the same format string as above.

    Quote Originally Posted by krazzyjman View Post
    and my queue is
    Code:
    struct queue {
    int front;
    int numElements;
    int size;
    };
    And where do you store the cards in your queue? Aren't you missing an array element?

    Bye, Andreas

  4. #4
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,694
    The functions i usually use to handle simple file operations
    • fopen
    • fread
    • fwrite
    • fclose

    Just type their name in google and read about them, write your code and if you have problems post back

  5. #5
    Registered User
    Join Date
    Sep 2012
    Posts
    21
    Thank you both for the fast replies.
    I do have one more question.
    I want to store the card in a queue array.
    How do I go about making that queue array?
    Also, I do not want to dynamically allocate memory, I just want the max set to 52

  6. #6
    Ticked and off
    Join Date
    Oct 2011
    Location
    La-la land
    Posts
    1,728
    Quote Originally Posted by krazzyjman View Post
    I want to store the card in a queue array.
    How do I go about making that queue array? I do not want to dynamically allocate memory, I just want the max set to 52
    Like AndiPersti said: you are missing that in your structure.

    I do believe you could use a stack instead of a queue. Consider this:
    Code:
    #define  CARDS  52
    
    struct card {
        char  value;
        char  suit;
    };
    
    struct deck {
        int          cards;  /* Cards in the stack, max. CARDS */
        struct card  card[CARDS];
    };
    Here, the struct deck is your card stack.

    You should first fill the deck with the leftover cards (not in hands), then shuffle them. If you have
    Code:
    struct deck  Deck;
    then Deck.cards is the number of cards in the deck (0 to 52, inclusive).

    When you remove (pop) a card off the deck, you first verify Deck.cards > 0, as otherwise the deck is obviously empty. Then, you decrease Deck.cards, and copy the card information from Deck.card[Deck.cards].

    You only really need a queue -- deck with head and tail instead of top -- if you intend to slide cards back into the bottom of the deck.

  7. #7
    Registered User
    Join Date
    Sep 2012
    Posts
    21
    I am making the card game war. So I do need a queue because when you win a round the cards go to the bottom of the pile.
    I am using a stack for the cards played on the table
    Does that make sense? I'm having trouble wording it

  8. #8
    Registered User
    Join Date
    Sep 2012
    Posts
    21
    I think I have the array all set up. For the scanf the input has an integer n that represent the number of games being played followed by 2n line showing the hands of the players.
    Each player will be dealt half of the deck (only 2 players).
    How do I go about scanning in that information into the array?

  9. #9
    Registered User
    Join Date
    Sep 2012
    Posts
    21
    This is my code I have so far. I'm not sure how correct is just need some guidance
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    
    struct card {
            int value;
            char suit;
            };
    
    struct stack {
        struct card tablecards[52];
        int top;
        };
    
        struct queue {
            struct card playercards[52];
            int font;
            int numElements;
            int size;
        };
    
    void init(struct queue* qPtr);
    int enqueue(struct queue* qPtr, int val);
    int dequeue(struct queue* qPtr);
    int empty(struct queue* qPtr);
    int front(struct queue* qPtr);
    int queue [52];
    
    int main () {
       int value, numgames, i;
       char suit;
    
    FILE *inputFile;
    
    inputFile = fopen("hi.txt", "r");
    
    if (inputFile == NULL) {
    printf("Does not exist\n");
    }
    
    fscanf(inputFile, "%d", &numgames);
    printf("%d", numgames);
    
    for (i = 0; i < numgames; i++) {
    fscanf("%d %c", &value, &suit);
    }
    
    fclose(inputFile);
    
    return 0;
    }

  10. #10
    Ticked and off
    Join Date
    Oct 2011
    Location
    La-la land
    Posts
    1,728
    Quote Originally Posted by krazzyjman View Post
    Code:
    struct card {
            int value;
            char suit;
    };
    Since char type can hold at least values 0 to 127, it is more than enough for the value, too. So, char value; is more appropriate above.

    Quote Originally Posted by krazzyjman View Post
    Code:
    struct stack {
        struct card tablecards[52];
        int top;
    };
    Okay, the above defines a card stack. I just wish you intended your braces more carefully; the haphazard indentation makes it annoying to read.

    Quote Originally Posted by krazzyjman View Post
    Code:
    struct queue {
            struct card playercards[52];
            int font;
            int numElements;
            int size;
    };
    font? numElements? size? What are they?

    Your queue size is fixed to 52 cards. You only need two fields: one to tell where the topmost card is, head or top, and one to tell where the bottommost card is, tail or bottom. Usually the latter is the index after the bottommost card, since that makes a lot of the math simpler.

    Quote Originally Posted by krazzyjman View Post
    Code:
    int queue [52];
    What purpose does the above have? It's just an array of 52 integers, definitely not a queue of any kind.

    If you want a 52-card queue in variable queue, declare something like struct queue queue;

    Note that queue is a confusing name. Your variable names should not describe what the variables are; they should describe what you use the variable for.

    As a hint, I'd use struct queue deck[2]; so I'd get an up to 52-card queue for each player.

    Quote Originally Posted by krazzyjman View Post
    Code:
    inputFile = fopen("hi.txt", "r");
    if (inputFile == NULL) {
    printf("Does not exist\n");
    }
    That just prints a line but forges ahead! No, you need to abort your program. For example, if you include <errno.h> and <string.h> too at the start of your program, you can use
    Code:
        static const char inputFileName[] = "hi.txt";
    
        inputFile = fopen(inputFileName, "r");
        if (inputFile == NULL) {
            fprintf(stderr, "%s: %s.\n", inputFileName, strerror(errno));
            return 1;
        }
    which prints the file name and the error message, describing exactly why the file could not be opened, then exits the program (with nonzero exit status, by returning from main()).

    Quote Originally Posted by krazzyjman View Post
    Code:
    fscanf(inputFile, "%d", &numgames);
    fscanf() returns the number of successful conversions. Above, you really should check that it did get an integer, one that makes sense, using for example
    Code:
        if (fscanf(inputFile, "%d", &numgames) < 1) {
            fprintf(stderr, "%s: File did not start with numgames.\n", inputFileName);
            fclose(inputFile);
            return 1;
        }
        if (numgames < 1) {
            fprintf(stderr, "%s: No games to play.\n", inputFileName);
            fclose(inputFile);
            return 1;
        }
        if (numgames > the appropriate limit) {
            fprintf(stderr, "%s: Too many games to play!\n", inputFileName);
            fclose(inputFile);
            return 1;
        }
    Quote Originally Posted by krazzyjman View Post
    Code:
    for (i = 0; i < numgames; i++) {
    fscanf("%d %c", &value, &suit);
    }
    Here, again, you should check that fscanf() does not return less than 2. If it does, then you didn't get both value and suit.

    You read the value into variable value, and the suit into variable suit, but you overwrite them on the very next iteration.

    Also, you should check the suit and value too. Perhaps
    Code:
             if ((value >= 1 && value <= 13) &&
                (suit == 'H' || suit == 'D' ||
                 suit == 'C' || suit == 'S') {
                /* This one is okay, save somewhere */
            } else {
                fprintf(stderr, "%s: Invalid card.\n", inputFileName);
                fclose(inputFile);
                return 1;
            }
    Finally, shouldn't you save them somewhere? In fact, weren't these supposed to be two different hands? Didn't I mention a hint in this message above somewhere?

  11. #11
    Registered User
    Join Date
    Sep 2012
    Posts
    21
    I did what I understood, which I don't think is much. I apologize. I'm an IT major not CS, programming is not my strong suit.
    Here is what I have now
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    
    struct card {
            int value;
            char suit;
            };
    
    struct stack {
    struct card tablecards[52];
        int top;
        };
    
        struct queue {
            struct card playercards[52];
            int font;
            int numElements;
            int size;
        };
    
        struct queue hand[2];
    
    void init(struct queue* qPtr);
    int enqueue(struct queue* qPtr, int val);
    int dequeue(struct queue* qPtr);
    int empty(struct queue* qPtr);
    int peek(struct queue* qPtr);
    
    
    
    
    
    
    int main () {
       int value, numgames, i;
       char suit;
    
    FILE *inputFile;
    
    inputFile = fopen("hi.txt", "r");
    
    
    fscanf(inputFile, "%d", &numgames);
    printf("%d", numgames);
    
    for (i = 0; i < numgames; i++) {
    fscanf("%d %c", &value, &suit);
    
    if ((value >= 1 && value <= 13) &&
        (suit == 'H' || suit == 'D' || suit == 'C' || suit == 'S')) {
            enqueue(value, suit);
        }
    
    
    }
    
    
    fclose(inputFile);
    
    return 0;
    }
    I didn't need the check for the open file part that was more just for me to make sure I did it right

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Scanning(copying) a .txt file to struct array
    By cagurtay in forum C Programming
    Replies: 10
    Last Post: 08-08-2012, 02:32 PM
  2. Scanning txt file values into an array
    By skmightymouse in forum C Programming
    Replies: 16
    Last Post: 04-28-2012, 01:38 PM
  3. Scanning input of different types
    By astalls769 in forum C Programming
    Replies: 1
    Last Post: 04-25-2012, 05:09 PM
  4. Scanning input data
    By papermate in forum C Programming
    Replies: 38
    Last Post: 04-25-2010, 01:56 AM
  5. Replies: 1
    Last Post: 04-25-2006, 12:14 AM