CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3
  1. #1
    Join Date
    Feb 2012
    Posts
    68

    input txt file using Cmd line arguements

    Im trying to use the command line to put a txt file in this code. I do Project->set project arguements in code blocks and nothing happens. How does one set up the arguements and where do i do it in codeblocks?


    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 */

  2. #2
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,398

    Re: input txt file using Cmd line arguements

    Define "nothing happens".
    And, BTW, did you debug your code? Did you see what happened after file opening (if it was opened), after reading its lines, and so on?
    Victor Nijegorodov

  3. #3
    Join Date
    Jun 2009
    Location
    France
    Posts
    2,513

    Re: input txt file using Cmd line arguements

    Quote Originally Posted by chucker View Post
    Im trying to use the command line to put a txt file in this code. I do Project->set project arguements in code blocks and nothing happens.
    That is correct, but keep in mind you can set a different argument for each "Build Target": It is the drop down menu when you are writing the arguments.

    If you set your arguments for the "Debug" build, but are compiling in "Release" then "nothing happens".
    Is your question related to IO?
    Read this C++ FAQ article at parashift by Marshall Cline. In particular points 1-6.
    It will explain how to correctly deal with IO, how to validate input, and why you shouldn't count on "while(!in.eof())". And it always makes for excellent reading.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured