Thread: Command line arguements (codeblocks)

  1. #1
    Registered User
    Join Date
    Feb 2012
    Posts
    27

    Command line arguements (codeblocks)

    im trying to read in a txt file to populate a list in CodeBlocks by entering an arguement before hand. Can anyone describe how to do this ?
    Last edited by Chucker; 04-09-2012 at 03:44 PM.

  2. #2
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909

  3. #3
    Registered User
    Join Date
    Feb 2012
    Posts
    27
    yea, maybe i just like talking to somebody about instead! haha. eh but srsly im lookin at yahoo answers and the like and i keep getting ambiguous answers. ive never done it before. if you know like a step by step for a full retard hollaaah. Here is the code im trying to do it for.

    Code:
    int main(int argc, char *argv[]) {
        char name[20];
        struct artist *ptr;
        char lineBuffer[LINEBUFFERSIZE];
        artistNodePtr startPtr = NULL; /* initially the artist list is empty */
        FILE *musicFile;
        char *artistTemp, *discTemp, *yearTemp, *trackTemp, *songTemp;
        int year, track, menu = 1;
        artistNodePtr theArtist;
        discNodePtr theDisc;
        if (argc==1)
        {
            printf(" Must supply a file name as command line argument/n");
            return 0;
        }
        if ((musicFile = fopen(argv[1], "r")) == NULL)
        {
            printf ("Error opening music file.  Program terminated/n");
            return 0;
        }
        getNextLine(lineBuffer, LINEBUFFERSIZE, musicFile);
        while (!feof(musicFile))
        {
            artistTemp = strtok(lineBuffer,";");
            if (artistTemp == NULL)
            {
                printf("Error parsing input file; Program is terminated\n");
                return 0;
            }
            discTemp = strtok(NULL ,";");
            if (discTemp == NULL)
            {
                printf("Error parsing input file; Program is terminated\n");
                return 0;
            }
            yearTemp  = strtok(NULL ,";");
            if (yearTemp == NULL)
            {
                printf("Error parsing input file; Program is terminated\n");
                return 0;
            }
            trackTemp = strtok(NULL ,";");
            if (trackTemp == NULL)
            {
                printf("Error parsing input file; Program is terminated\n");
                return 0;
            }
            songTemp = strtok(NULL ,"\n");
            if (songTemp == NULL)
            {
                printf("Error parsing input file; Program is terminated\n");
                return 0;
            }
            year = atoi(yearTemp);
            track = atoi(trackTemp);
            theArtist = findOrInsertArtist(artistTemp);
            theDisc = findOrInsertDisc(&(theArtist->disc_p), discTemp, year);
            findOrInsertSong(&(theDisc->song_p), songTemp, track);
            getNextLine(lineBuffer, LINEBUFFERSIZE, musicFile);
        }  /* end of while loop */
    Last edited by Chucker; 04-09-2012 at 04:18 PM.

  4. #4
    Registered User
    Join Date
    Dec 2007
    Posts
    2,675
    So are you just flat out refusing to help yourself, or did you actually check the Google search link provided and run into trouble?

  5. #5
    Registered User
    Join Date
    Feb 2012
    Posts
    27
    nothing was applicable. i go to project->set arguements. it doesnt even give me a box to type in, nothing happens. go to cmdpromt they say. type jibberish that doesnt even apply to txt files they say. i need customized help, im sure codeblocks has some simple method, but ill go back to sifting thru yahoo answers haha. lemme know if u know

  6. #6
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    Quote Originally Posted by Chucker View Post
    nothing was applicable. i go to project->set arguements. it doesnt even give me a box to type in, nothing happens.
    Provide a screen shot of what that looks like and I can probably tell you where to type.

    Quote Originally Posted by Chucker
    go to cmdpromt they say. type jibberish that doesnt even apply to txt files they say. i need customized help, im sure codeblocks has some simple method, but ill go back to sifting thru yahoo answers haha. lemme know if u know
    Running outside of the IDE is certainly possible as well. The IDE controls a separate windows program called command prompt. You can always just use command prompt yourself. But you will miss the features of the IDE if you are still making the program.

  7. #7
    Registered User
    Join Date
    Feb 2012
    Posts
    27
    turns out because i did not have my C file within a project I could not do the arguements, so i fixed that and now I can enter arguements, but Im not sure if i successfully did it or not. All I typed in arguemnnts was HW4Data.txt, compiled, ran and then my computer froze for a sec and then stopped responding, but hey at least it didnt say error accessing file. Would yall say we have an infinite loop on our hand? Or am I missing some proper syntax in the arguement line and that is causing the lock up?
    Last edited by Chucker; 04-09-2012 at 05:30 PM.

  8. #8
    Registered User
    Join Date
    Jun 2010
    Location
    In a house
    Posts
    15
    Hard to say without the whole thing.

  9. #9
    Registered User
    Join Date
    Feb 2012
    Posts
    27
    do i need to free(malloc or what have you, because i did not do that

    and i just threw comment tags on the incomplete functions


    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #define LINEBUFFERSIZE 256
    /* Definitions for data structure nodes (artist, disc, track)  */
    struct song
    {
        char *songName_p;
        int trackNumber;
        struct song *nextSong_p;
    };
    
    struct disc
    {
        char *discName_p;
        int year;
        struct song *song_p;
        struct disc *nextDisc_p;
    };
    struct artist
    {
        char name[20];
        char *artistName_p;
        struct disc *disc_p;
        struct artist *nextArtist_p;
    };
    struct artist *end = (struct artist *) NULL;   //NEW
    struct artist *startPtr = (struct artist *) NULL; //NEW
    struct artist *find(struct artist *, char * );//NEW
    struct artist *initializenode(char *);//NEW
    typedef struct artist artist_t;
    typedef struct disc disc_t;
    typedef struct song song_t;
    typedef struct artist *artistNodePtr;
    typedef struct disc *discNodePtr;
    typedef struct song *songNodePtr;
    /* function  prototypes */
    void InsertArtist(struct artist *New);
    artistNodePtr findOrInsertArtist(char *);
    discNodePtr  findOrInsertDisc(discNodePtr *sPtr, char *discID, int releaseYear);
    void  findOrInsertSong(songNodePtr *sPtr, char *songID, int trackID);
    void getNextLine(char buffer[], int bufferSize, FILE *fptr);
     
    int main(int argc, char *argv[]) {
        char name[20];
        struct artist *ptr;
        char lineBuffer[LINEBUFFERSIZE];
        artistNodePtr startPtr = NULL; /* initially the artist list is empty */
        FILE *musicFile;
        char *artistTemp, *discTemp, *yearTemp, *trackTemp, *songTemp;
        int year, track, menu = 1;
        artistNodePtr theArtist;
        discNodePtr theDisc;
        if (argc==1)
        {
            printf(" Must supply a file name as command line argument/n");
            return 0;
        }
        if ((musicFile = fopen(argv[1], "r")) == NULL)
        {
            printf ("Error opening music file.  Program terminated/n");
            return 0;
        }
        getNextLine(lineBuffer, LINEBUFFERSIZE, musicFile);
        while (!feof(musicFile))
        {
            artistTemp = strtok(lineBuffer,";");
            if (artistTemp == NULL)
            {
                printf("Error parsing input file; Program is terminated\n");
                return 0;
            }
            discTemp = strtok(NULL ,";");
            if (discTemp == NULL)
            {
                printf("Error parsing input file; Program is terminated\n");
                return 0;
            }
            yearTemp  = strtok(NULL ,";");
            if (yearTemp == NULL)
            {
                printf("Error parsing input file; Program is terminated\n");
                return 0;
            }
            trackTemp = strtok(NULL ,";");
            if (trackTemp == NULL)
            {
                printf("Error parsing input file; Program is terminated\n");
                return 0;
            }
            songTemp = strtok(NULL ,"\n");
            if (songTemp == NULL)
            {
                printf("Error parsing input file; Program is terminated\n");
                return 0;
            }
            year = atoi(yearTemp);
            track = atoi(trackTemp);
            theArtist = findOrInsertArtist(artistTemp);
           // theDisc = findOrInsertDisc(&(theArtist->disc_p), discTemp, year);
           // findOrInsertSong(&(theDisc->song_p), songTemp, track);
            getNextLine(lineBuffer, LINEBUFFERSIZE, musicFile);
        }  /* end of while loop */
        while (menu != 0)
        {
            printf("1 to display entire catalog \n");
            printf("2 to display alls songs by a given artist\n");
            printf("3 to display all songs on a given disc\n");
            printf("0 to exit the library\n ");
            scanf("%d", &menu);
            switch (menu)
            {
            case 2: printf("enter an artist name");
                scanf("%s", name);
                ptr = find(startPtr, name);
                if (ptr==NULL)
                {
                    ptr = initializenode(name);
                    InsertArtist(ptr);
                }
            }
        } /* end main */
    }
    artistNodePtr findOrInsertArtist(char *name ) {
      artistNodePtr sPtr = NULL;
      sPtr = find(startPtr, name);
       if(!sPtr)
          sPtr = initializenode(name);
      return sPtr;
    }
    
    struct artist *initializenode(char *name)
    {
      artistNodePtr newNodePtr = (artistNodePtr)malloc(sizeof(artist_t));
      if (newNodePtr != NULL)
      {
         newNodePtr->artistName_p = (char*)malloc((strlen(name)+1)*sizeof(char));
         if (newNodePtr->artistName_p != NULL)
         {
            strcpy(newNodePtr->artistName_p, name);
            newNodePtr->nextArtist_p = NULL;
            newNodePtr->disc_p = NULL;
            return newNodePtr;
         }
      }
    }
    void InsertArtist(struct artist *New)
    { //NEW
        struct artist *temp, *prev;
        if(startPtr == NULL)
        {
            startPtr = New;
            end = New;
            startPtr->nextArtist_p = NULL;
            return;
        }
        temp = startPtr;
        while(strcmp(temp->name, New->name) < 0)
        {
            temp = temp->nextArtist_p;
            if(temp == NULL)
            break;
        }
        if(temp == startPtr)
        {
            New->nextArtist_p = startPtr;
            startPtr = New;
        }
        else
        {
            prev = startPtr;
            while(prev->nextArtist_p != temp)
            {
                prev = prev->nextArtist_p;
            }
            prev->nextArtist_p = New;
            New-> nextArtist_p = temp;
            if(end == prev)
            end = New;
        }
    }
    struct artist *find(struct artist *Ptr, char *name)
    { //NEW
        while (strcmp(name, Ptr->name )!=0)
        {
            Ptr = Ptr->nextArtist_p;
            if (Ptr == NULL)
                break;
        }
        return Ptr;
    }
    
    /*discNodePtr findOrInsertDisc( discNodePtr *sPtr, char *discID, int releaseYear)
    {
    }*/
    /*void findOrInsertSong(songNodePtr *sPtr, char *songID, int trackID)
    {
    }*/
    
    void getNextLine(char buffer[], int bufferSize, FILE *fptr)
    {
        char temp;
        int i = 0;
        buffer[0] = fgetc(fptr);
        while ( (!feof(fptr)) && (buffer[i] != '\n') &&  i<(bufferSize-1))
        {
            i = i +1;
            buffer[i]=fgetc(fptr);
        }
        if ((i == (bufferSize-1)) && (buffer[i] != '\n'))
        {
            temp = fgetc(fptr);
            while (temp != '\n')
            {
                temp = fgetc(fptr);
            }
        }
        buffer[i] = '\0';
    }
    Last edited by Chucker; 04-09-2012 at 05:38 PM.

  10. #10
    Registered User
    Join Date
    Mar 2011
    Posts
    546
    you need to create a project to get that dialog. if you are just editing a c file without a project it won't let you set the arguments.

  11. #11
    Registered User
    Join Date
    Jun 2010
    Location
    In a house
    Posts
    15
    Obviously he did create a project otherwise it will throw an error if he just ran it.

  12. #12
    Registered User
    Join Date
    Feb 2012
    Posts
    27
    Yea i mentioned i fixed that problem. everything is def there.. im pretty sure. to input the data file. Anyone know where to put the free( ?

  13. #13
    Registered User
    Join Date
    Mar 2011
    Posts
    546
    tripleA, then why did Chucker answer that not having a project was his problem with the arguments?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Command Line Arguements
    By Tha_Rappa in forum C Programming
    Replies: 18
    Last Post: 11-26-2006, 11:03 PM
  2. command line arguements
    By screamer903 in forum C Programming
    Replies: 3
    Last Post: 03-23-2005, 07:59 AM
  3. Command Line Arguements
    By scaven in forum C Programming
    Replies: 2
    Last Post: 04-13-2003, 05:36 PM
  4. Command Line Arguements
    By Jay2337 in forum C Programming
    Replies: 4
    Last Post: 03-05-2003, 01:57 PM
  5. Working With Command Line Arguements
    By mart_man00 in forum C Programming
    Replies: 4
    Last Post: 08-03-2002, 04:31 PM