Thread: Storing integers to a character array

  1. #1
    Registered User
    Join Date
    Nov 2012
    Posts
    51

    Storing integers to a character array

    Hello friends,

    I am creating a program and am having trouble with one part. The input file looks like:
    Code:
    2
    3 
    Bill Jeff Steve
    Linda Brittany Jessica
    3 1 4
    2 1 9
    8 3 7
    6 4 9
    4 8 9
    6 9 4
    Where I am stuck is putting the numbers into the string array. For example, Bill's scores should be set up as Bill's scores should be set as
    Code:
    bill_score = {3, 1, 4}
    and when this code is ran
    Code:
    for (i=0; i<3; i++)
    {
        printf("%s - ", men[i]);
        printf("%d ", bill_score);
    }
    it should output
    Code:
    Bill - 3 1 4
    Here is what I have so far, thanks you guys.

    Code:
    # include <stdio.h>
    # include <stdlib.h>
    
    
    # define MAX_CHARS 20 // MAX_CHARS is the precondition of the number of letters each name can have.
    
    
    
    
    int main()
    {
        // Opens the file and tests to see if the file is being read properly.
        FILE *ifp = fopen("matching.txt   ", "r");
        if (!ifp)
        {
            printf("The file is unable to open!\n");
            return -1;
        }
    
    
        // Variables: i is the initializer; num_events is the number of events; num_pairs is the number of pairs.
        int i, num_events, num_pairs;
        // Reads in the first line-- the number of dating sessions.
        fscanf(ifp, "%d", &num_events);
        // Reads in the number of pairs
        fscanf(ifp, "%d", &num_pairs);
    
    
        //printf("The number of events is: %d\n", num_events);
        //printf("The number of pairs is: %d\n\n", num_pairs);
    
    
        // Mallocing space for a char-pointer at the size of number of pairs.
        char **men = malloc(sizeof(char*) *num_pairs);
        char **women = malloc(sizeof(char*) *num_pairs);
    
    
        // Loops through the whole group of daters and their scores.
        for (i=0; i< num_events; i++)
        {
            // This loop allocates space for the names of each of the men into an array
            for (i=0; i< num_pairs; i++)
            {
                // For each man, dynamically allocate memory for the size of 20 chars(MAX_CHARS)
                men[i] = malloc(sizeof(char) *MAX_CHARS);
                fscanf(ifp, "%s", men[i]);
                printf("%s ", men[i]);
                // Debugger to test to see if the names are being read in properly... printf("%s\n", men[i]);
            }
    
    
            printf("\n");
    
    
            for (i=0; i< num_pairs; i++)
            {
                // For each woman, dynamically allocate memory for the size of 20 chars(MAX_CHARS)
                women[i] = malloc(sizeof(char) *MAX_CHARS);
    
    
                // Scans in the name of the women and alloocates them to women[i]
                fscanf(ifp, "%s", women[i]);
                printf("%s ", women[i]);
    
    
            }
    
    
            printf("\n");
            printf("\n");
        }
    
    
        // Loops and prints the output for the number of events.
        for (i=0; i < num_events; i++)
        {
            printf("Matching #%d: Maximum Score = \n", i+1);
        }
    
    
        return 0;
    
    
    }
    Last edited by C0__0D; 01-30-2013 at 06:07 PM.

  2. #2
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    I'm not totally clear on what exactly you want this code to do, and what is currently wrong with it.

    A few quick notes for starters:
    1. You should check that malloc succeeded before writing to the memory you allocate. Do this for every call to malloc, calloc, realloc, etc.

    2. You should check that your fscanf calls succeed. If you ask for one conversion ("%s", "%d", etc), then it returns one if successful. Ask for two things in one call ("%s %f"), it returns 2 if both were scanned successfully, etc.

    3. If you always malloc a string of the same size (e.g. MAX_CHARS on line 45 or 58), there isn't much point. Instead, just allocate enough space for that actual name, + 1 more for the null. Note, you must read the name first into a temp buffer, then malloc and strcpy.
    Code:
    char temp_buf[MAX_CHARS + 1];
    fscanf(ifp, "%s", temp_buf);
    men[i] = malloc(strlen(temp_buf) + 1);
    if (men[i] == NULL) {
        // holy crap, malloc failed! run screaming from the building
        // but first, print a sensible error message and exit your program
    }
    strcpy(men[i], temp_buf);
    It seems to me a struct would be very useful here:
    Code:
    struct person {
        char *name;
        int *scores;  // or whatever those numbers represent -- this will act as an array of ints
    }
    ...
    struct person *men;
     
    // I'm omitting error checking for brevity
    men = malloc(num_pairs * sizeof(*men));
    men->scores = malloc(num_events * sizeof(*men->scores));
    ...
    for i from 0 to 
        fscanf(fp, "%s", temp buf);
        men[i].name = malloc(strlen(temp_buf) + 1);
        strcpy(men[i].name, temp_buf
    ...
    // use a loop to fscanf "%d" into men[i].scores[j]
    Hope that gives you some ideas.

  3. #3
    Registered User
    Join Date
    Nov 2012
    Posts
    51
    Can you explain what buffer does or mean?

  4. #4
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    A buffer is often just another way of saying char array. Basically it's a temporary array of chars so you can read the name into that first.

  5. #5
    Registered User hex_dump's Avatar
    Join Date
    Dec 2012
    Posts
    88
    Quote Originally Posted by anduril462 View Post
    A buffer is often just another way of saying char array. Basically it's a temporary array of chars so you can read the name into that first.
    In this case. But in general a buffer is as it's name implies. A [temp] storage area to hold some value(s) But you were probably personalizing it so excuse me.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. storing integers in an array
    By Nyah Check in forum C Programming
    Replies: 6
    Last Post: 03-29-2012, 02:11 PM
  2. storing integers from a text file into a 1d array
    By sniperwire in forum C Programming
    Replies: 3
    Last Post: 09-30-2008, 09:04 PM
  3. Storing array of integers as keys
    By vsla in forum C Programming
    Replies: 2
    Last Post: 05-08-2008, 12:53 PM
  4. Storing integers from file into arrays???? please help
    By adzza69 in forum C++ Programming
    Replies: 5
    Last Post: 09-11-2004, 12:28 AM
  5. integers storing as symbols in arrays
    By rjcarmo in forum C++ Programming
    Replies: 4
    Last Post: 05-19-2003, 01:17 AM