Write a program to determine if the parameter you enter is a .txt file name. C L
ID: 3661139 • Letter: W
Question
Write a program to determine if the parameter you enter is a .txt file name. C LANGUAGE for example: #include int main( int argc, char *argv[] ) { if( argc == 2) printf("The parameter is a %s file name. ", argv[1]); else if( argc > 2 ) printf("There should be only one additional parameter. "); else ??? dont know the condition for this :S printf("The parameter is not a .txt file name. "); system("PAUSE"); return 0; } The only one I cannot get to work is the last one, if the parameter is not a .txt file name. HELP will rate you lifesaver!!!Explanation / Answer
#include<stdio.h>
#include<string.h>
int main( int argc, char *argv[] )
{
if(argc == 2)
{
const char * filename = argv[1];
int l = strlen(argv[1]);
if (*(filename + l - 3) == 't' &&
*(filename + l - 2) == 'x' &&
*(filename + l - 1) == 't')
{
printf("The parameter is a txt file name. ");
}
else
{
printf("The parameter is not a txt file name. ");
}
}
else if( argc > 2 )
{
printf("There should be only one additional parameter. ");
}
else
{
printf("no parameter "); //this is the case when argc = 1
}
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.