-
reading from argv[]
Hey does anyone have any clue why filename is never getting allocated in the code below?
I read it afterwards and its giving me a seg fault.
Code:
for (i = 1; i < (argc + 1); i++){
if (strcmp(argv[i], "-h") == 0){ /* -h operand is in command line */
isHtml = 1;
}
else if (strcmp(argv[i], "-f") == 0){
hasFilter = 1;
if (argv[i + 1] != NULL){
filterfile = argv[++i];
}
else{
fprintf(stderr, "Usage: tvprogs [-h] [-f filterfile] epgXMLfile");
return EXIT_FAILURE;
}
}
else {
filename = argv[i];
}
}
-
For starters, your loop is wrong. You should be stopping at less than argc, not less than or equal to. Next, you can't copy strings using the assignment operator. Look into strcpy.
Quzah.
-
Ok changed those two, still getting a seg fault though
Code:
for (i = 1; i < argc; i++){
.
.
.
else{
fprintf(stderr, "Usage: tvprogs [-h] [-f filterfile] epgXMLfile");
return EXIT_FAILURE;
}
-
sorry I meant
Code:
else {
strcpy(filename, argv[i]);
}
-
Your code sample doesn't actually help us here, since it doesn't show us anything you're actually doing, other than printing an error. Try making a small compilable program, with just you reading through the arguments, printing them. Then try copying them to an array, and printing that.
Quzah.
-
alright thanks quzah I'll try that
-
Code:
strcpy(filename, argv[i]);
filename must be an array of chars