Thread: Need help with my C program

  1. #1
    Registered User
    Join Date
    Apr 2010
    Posts
    34

    Need help with my C program

    I'm trying to make a program that reads a file and does stuff with it.
    First thing it does is gets some usernames which are contained in the file.
    I'll post my code.
    Code:
    #include <stdio.h>
    
    void sort(int n, int v[], char **name) {
        int gap, i, j, temp;
        char *temp2;
        for(gap = n/2; gap > 0; gap /= 2) {
            for(i = gap; i < n; i++) {
            	for(j = i - gap; j >= 0 && v[j] > v[j+gap]; j -= gap) {
                    temp = v[j];
                    temp2 = name[j];
                    v[j] = v[j+gap];
                    name[j] = name[j+gap];
                    v[j+gap] = temp;
                    name[j+gap] = temp2;
                }
            }
        }
    }
    
    int get_ID(char *str, int *ID, char **players) {
        char getLine[1024];
        FILE *fp;
        int i;
        char s[20];
        int count = 0;
        fp = fopen(str, "r");
        if(fp == NULL) {
            printf("Missing log file\n");
            exit(20);
        }
        while(fgets(getLine, 1024, fp) != NULL) {
            if(sscanf(getLine, "%s %x",s,&i)==2) {
                if(strcmp(s,"ID:")==0) {
                    ID[count] = i;
                    printf("%x\n",i);
                    fgets(getLine,1024,fp);
                    sscanf(getLine, "%*s %*s %s",s);
                    players[count] = s;
                    printf("%s\n",players[count]);
                    count += 1;
                }
            }
            if(count==10) {
                break;
            }
        }
        return count;
    }
    
    
    int main(int argc, char *argv[]) {
        char heroes[10][20];
        int ID[10];
        char *players[10];
        char clicks[20][20];
        int i,b;
        if(argc<2) {
            printf("Usage: fogclicks maplog\n");
            exit(10);
        }
        else {
            b = get_ID(argv[1], ID, players);
            for(i=0;i<b;i++) {
                printf("%s\n",players[i]);
            }
            sort(b, ID, players);
        }
        return 0;
    }
    In the Get_ID function, it prints the player names correctly but then in the main function when i print the player names it just prints the last player name each time in the for loop.

    This is my first time posting on a programming forum, sorry if I am not very clear.

    Any help appreciated.
    Last edited by Blasz; 04-29-2010 at 08:31 AM. Reason: changed %d to %s in main function print

  2. #2
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Welcome to the forum, Blasz!

    What format specifier are you using to print out players in main()?

  3. #3
    Registered User
    Join Date
    Apr 2010
    Posts
    34
    oh woops its meant to be %s, I originally was printing out the ID's out and they worked fine, and forgot to switch back to %s in main.

    Thats not the problem though.

    I can give you the results of me testing the program to see what the prints output if that would help.

  4. #4
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Yeah, I knew that wasn't the problem, but we need to see the right code!

    Is the rest of the code all the same as your code, now?

    Hmmm... < dons his coat, pipe and deerstalking cap >

  5. #5
    Registered User
    Join Date
    Jan 2009
    Location
    Australia
    Posts
    375
    Your array 'players' is an array of 10 pointers. In the Get_ID function you set them to point to a local variable. Not only will this memory not persist once the function has exited, they will all point to the same string.

    This is usually done by allocating memory for the strings instead by using malloc.

    malloc - C++ Reference

    EDIT: Woops, Adak if you were aware of this but just weren't letting on, sorry. I posted this only after seeing your first post and so I wasn't sure if you didn't know or were just trying to teach him/her something.
    Last edited by DeadPlanet; 04-29-2010 at 08:38 AM.

  6. #6
    Registered User
    Join Date
    Apr 2010
    Posts
    34
    Hmm, I want the players array to be an array of strings. Should an array of strings be an array of 10 pointers?

    I'll try using malloc and tell you what i get.

  7. #7
    Registered User
    Join Date
    Jan 2009
    Location
    Australia
    Posts
    375
    Remember that with malloc, you're allocating space, so you actually have to copy the data to it, instead of just assigning the pointer.

    Also, make sure you free().

    This can be done without dynamically allocating memory. If you know the maximum size for a string, then you can create a two dimensional array to hold the data.

  8. #8
    Registered User
    Join Date
    Apr 2010
    Posts
    34
    Yes i know the maximum size, so i dont need malloc. I don't understand how i would copy data to the array, maybe use &s instead of s?

  9. #9
    Registered User
    Join Date
    Jan 2009
    Location
    Australia
    Posts
    375

  10. #10
    Registered User
    Join Date
    Apr 2010
    Posts
    34
    Thankyou, also when i define the player array as
    Code:
    char players[10][20];
    instead of
    Code:
    char *players[10];
    I get this compiler warning,
    assignment of pointer to array 20 of char to pointer to pointer to char.

  11. #11
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Excellent work, DeadPlanet! (aka, Dr. Watson)

    Blasz, you can't assign the address (which was s), to an actual array of char's. You need to strcpy() the CONTENTS you want, into the new style players[][]

    No worries, DeadPlanet. I was just poking along here, this morning.
    Last edited by Adak; 04-29-2010 at 09:04 AM.

  12. #12
    Registered User
    Join Date
    Apr 2010
    Posts
    34
    Ok i think i've fixed everything, thanks deadplanet and adak for your help.

    One other thing, what C programming writing software do you recommend for windows 7 users. I have been using lcc-win32 and its not very good.
    Last edited by Blasz; 04-29-2010 at 09:26 AM.

  13. #13
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    <Sigh>
    I still have my Windows 7 package sitting here next to the computer.

    I was thinking of using either Visual C or Code::Blocks with Ming, but I want to go 64 bit because of my interest in chess programming (64 bits has obvious advantages for a chessboard). May wind up going with Intel compiler(some are free for non-commercial use).

  14. #14
    Registered User
    Join Date
    Apr 2010
    Posts
    34
    As this is only the tip of the iceberg of my program, should i post any future problems in a new thread or keep it here. I already have 1 problem i need solved atm.

  15. #15
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    Quote Originally Posted by Adak View Post
    <Sigh>
    I still have my Windows 7 package sitting here next to the computer.

    I was thinking of using either Visual C or Code::Blocks with Ming, but I want to go 64 bit because of my interest in chess programming (64 bits has obvious advantages for a chessboard). May wind up going with Intel compiler(some are free for non-commercial use).
    That's dope. Please hire me as a tester. I used to have a Master standing in professional chess.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Issue with program that's calling a function and has a loop
    By tigerfansince84 in forum C++ Programming
    Replies: 9
    Last Post: 11-12-2008, 01:38 PM
  2. Need help with a program, theres something in it for you
    By engstudent363 in forum C Programming
    Replies: 1
    Last Post: 02-29-2008, 01:41 PM
  3. Replies: 4
    Last Post: 02-21-2008, 10:39 AM
  4. My program, anyhelp
    By @licomb in forum C Programming
    Replies: 14
    Last Post: 08-14-2001, 10:04 PM