Thread: Inserting unique IDs in my structure

  1. #1
    Registered User
    Join Date
    Jul 2014

    Inserting unique IDs in my structure


    I've used qsort to extract the unique student IDs by jumping every four because there's 80 rows of data and twenty students.

    I want to make sure it's all working and there's something very strange going on. If you look at my code, when it comes to initialising the structure with the unique student IDs it will happily print them after each one has been initialised inside the for loop (which suggests to me it's obviously done it) but, however, when I want to print them again outside the for loop in another for loop as a "double-check" it goes horribly wrong, presumably they're not in there at all? If you traverse the code to the bottom you'll see my comments.

    Now in my for loop, j is going to be incremented 20 times which will correspond to the #define STUDENTS 20. So imagine the array of structure db[j] , the first loop will set i and j both to zero, retrieve the first unique ID and store it in the structure. Then i is incremented by four to acces the next unique ID and then j is incremented by 1 to insert this new ID in the structure.

    Hope that makes sense.
    #include <stdio.h>
    #include <string.h>
    #include <stdlib.h>
    #define ROWS 80
    #define SIZE 100
    #define STUDENTS 20
    int string_compare(void const *x, void const *y)
        return strcmp(*(char**)x, *(char**)y);
    struct student
        char student_ID[SIZE];
    int main(void)
        FILE* input;
        int i,j,data_items;
        int records=0;
        char buffer_IDs[ROWS][SIZE];
        char buffer_subjects[ROWS][SIZE];
        int marks[ROWS];
        char *string_ptrs[ROWS];
        struct student db[STUDENTS];
        if((input=fopen("C:\\marks\\marks.txt", "r"))==NULL)
            perror("File open failed!");
            while ( ( data_items=fscanf(input, "%s %s %d", buffer_IDs[records], buffer_subjects[records], &marks[records])) == 3) 
            printf("%s %s %d\n", buffer_IDs[records], buffer_subjects[records], marks[records]);
            if ( records > ROWS)
        qsort(string_ptrs, records, sizeof(char*), string_compare);
            printf("%s\n",db[j].student_ID); /*Happily prints the unique IDs contained in the structure*/
    "%s\n",db[i].student_ID); /*Does NOT print them outside the for loop which initialises the structure. */
        return 0;
    Last edited by Tripswitch; 08-11-2014 at 08:31 AM.

  2. #2
    Registered User
    Join Date
    Jul 2014
    It doesn't matter. I've done it now! I keep setting j to zero INSIDE the for loop lol! False alarm folks. I'll be back sometime soon.

  3. #3
    Registered User
    Join Date
    Oct 2006
    You probably want to look at UuidCreate() and UuidToString(), since you're obviously running on windows. Note that this is only one of many options.
    What can this strange device be?
    When I touch it, it gives forth a sound
    It's got wires that vibrate and give music
    What can this thing be that I found?

  4. #4
    Registered User zub's Avatar
    Join Date
    May 2014
    OMG, You tread not all the same rake. When you finally get tired of beating your forehead?
    Our goals are clear, tasks are defined! Let's work, comrades! -- Nikita Khrushchev

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 5
    Last Post: 12-15-2012, 08:16 PM
  2. Unique function in C
    By Hiba in forum C Programming
    Replies: 10
    Last Post: 12-28-2009, 12:31 PM
  3. A Unique C++ Application??
    By toonlover in forum C++ Programming
    Replies: 9
    Last Post: 07-14-2006, 01:40 PM
  4. Unique Files
    By neandrake in forum C++ Programming
    Replies: 13
    Last Post: 12-02-2003, 01:59 PM
  5. unique windows
    By jhawsey in forum Windows Programming
    Replies: 10
    Last Post: 09-30-2002, 08:30 PM