-
April 9th, 2012, 05:26 PM
#1
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 */
-
April 10th, 2012, 01:03 AM
#2
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
-
April 10th, 2012, 05:44 AM
#3
Re: input txt file using Cmd line arguements
Originally Posted by chucker
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|