Hi All,

I have a program which takes a file as an input and does some processing depending upon the type of file passed.
The file types are : abc_txt, abc_ply, abc_cnvinf, abc_laneinfo etc

eg: DoWork [FileType] [File]

./DoWork txt abc_txt (or) ./DoWork cnvinf abc_cnvinf

Now I have to perform a check, like if the type and file do not match it should give an error.

That is it should check if the type and _XXX are same.

How do I proceed doing it?? I have no idea.. can anyone help??

Squirrel

I will assume that you know to work with argc and argv. It should be simple just parse the string argv[2] till the '_' char and then copy what left (from '_' till the end including '\0') to temporary string. Then just use

if (strcmp(fileExtension, tempString) != 0)
{
   // ERROR
}
#include <stdio.h>
#include <string.h>

int main(int argc, char ** argv)
{
   char * c = NULL;
   
   if (argc != 3)
   {
      printf("Invalid input parametars!\n");
      return 1;
   }
   c = strrchr (argv[2], '_');
   if (c != NULL)
   {
      if (strcmp(argv[1], ++c) != 0)
      {
         printf("Error\n");
      }
      else
      {
         printf("Match\n");
      }
   }
   
   return 0;
}

Compiled with gcc

commented: Stop Doing Homework +0
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.