Solved

Help in writing in C to parse a command line program

Posted on 2000-05-05
15
207 Views
Last Modified: 2010-04-15
Can anyone help write a simple function to check through a directory of programs and extract the *.ico files and use a command line program(lets say its icon2bitmap.exe and it doesn't convert multiple files, just one at a time) to convert each .ico file in the directory into a .bmp file? then dump into another directory say \image

please help

thanx
0
Comment
Question by:airworm
  • 8
  • 6
15 Comments
 
LVL 1

Expert Comment

by:ntdragon
Comment Utility
you see we don't write programs for you
ask a direct question?
send us your code.
0
 
LVL 10

Expert Comment

by:makerp
Comment Utility
bellow is a recursive scanning function. if you dont want it to recurse just take out the recurice call and sub-dir if.

use the system() command to fire an external program to change the .bmp to whatever etc.

i.e

sprintf(command,"change %s %s\n\r",tofile.bmp,fromfile.ico);
system(command);

the system command will exe a program and then when it finishes return control to you program.

/*
this function recursivly scan directries. you will need to change it so it gets the first file only.

i done this in VC++ on NT 4.0
*/

#include <stdio.h>
#include <io.h>


int scan_path(char *path)
{
     char spath[256];
     struct _finddata_t ff;
     long fhandle;
      
     sprintf(spath,"%s\\*",path);
     fhandle=_findfirst(spath,&ff);
     if(fhandle==-1L)
     {
          printf("Bad path\n");
          return -1;
     }
     else
     {
          /* loop through each etry in the dir */
          while(!_findnext(fhandle,&ff))
        {
               /* if its a sub-dir then recursivly call our self*/
               if(ff.attrib&_A_SUBDIR)
               {
                     if(strcmp(ff.name,".."))
                     {
                            sprintf(spath,"%s\\%s",path,ff.name);
                            scan_path(path2);
                     }
               }
               else
               {
                     /* if its a file then */
                     if(ff.attrib^_A_SYSTEM)
                     {
                            /* in here you will have to check the extension */
                            printf("Found File %s\n",ff.name);
                     }
               }
          }
     }
     return 1;
}
      
0
 

Author Comment

by:airworm
Comment Utility
thanx makerp!

will try this out but not very clear on how you would use the system() command to fire an external program

could u elaborate?
i am using VC++ too on NT 4.0
0
 

Author Comment

by:airworm
Comment Utility
makerp

is this what u mean for the system()command?

sprintf(command,"C:\\bmp2icon.exe %s %s\n\r",tofile.bmp,fromfile.ico);
system(command);
0
 
LVL 10

Expert Comment

by:makerp
Comment Utility
yes .. the system command will do a command as if it was types at the cmd line. the \n\r is like pessing the enter button and hence fires the cmd off.

hope you get it done.
0
 
LVL 10

Expert Comment

by:makerp
Comment Utility
it might be \n on its own, try both
0
 

Author Comment

by:airworm
Comment Utility
makerp

i needed to parse all *.ico files in that directory and convert them using this command line icon to bmp convertor
eg. utility.ico becomes utility.bmp (it will auto create the same name...)

i've work on ur code this way but it still shows some errors. Please help!


#include <stdio.h>
#include <conio.h>
#include <string.h>
#include <io.h>

char command[50];


int scan_path(char *path)
{
     char spath[256];
     struct _finddata_t ff;
     long fhandle;

     sprintf(spath,"%s\\*",path);
     fhandle=_findfirst(spath,&ff);
     if(fhandle==-1L)
     {
          printf("Bad path\n");
          return -1;
     }
     else
     {
          /* loop through each entry in the dir */
          while(!_findnext(fhandle,&ff))
              {
               /* if its a sub-dir then recursivly call our self*/
               if(ff.attrib&_A_SUBDIR)
               {
                     if(strcmp(ff.name,".."))
                     {
                            sprintf(spath,"%s\\%s",path,ff.name);
                            scan_path("c:\\temp\\graphic\\");
                     }
               }
               else
               {
                     /* if its a file then */
                     if(ff.attrib^_A_SYSTEM)
                     {
                            /* in here you will have to check the extension */
                         if(strcmp(ff.name,"*.ico"))  
                                     {
                                          printf("Found File %s\n",ff.name);
                                          sprintf(command); {"c:\\temp\\icon\\Ico2Bmp.exe %s \n\r",ff.name;}  
                                          system(command);
                                     }
                     }
               }
          }
     }
     return 1;
}
 

---------------------
error msg shown compiled using MS VC++


c:\temp\icon\ico2bmp.c(65) : error C2198: 'sprintf' : too few actual parameters
c:\temp\icon\ico2bmp.c(66) : warning C4013: 'system' undefined; assuming extern returning int
Error executing cl.exe.

ico2bmp.exe - 1 error(s), 1 warning(s)
0
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 10

Accepted Solution

by:
makerp earned 150 total points
Comment Utility
you need to #include <stdlib.h> or <process.h> for the system command.

sprintf is wrong sprintf usage is : (command,"c:\\temp\\icon\\Ico2Bmp.exe %s \n\r",ff.name)

now you will need to split the path into
      char drive[_MAX_DRIVE];  
      char dir[_MAX_DIR];  
      char fname[_MAX_FNAME];
      char ext[_MAX_EXT];
using

#include <io.h> function

_splitpath( ff.name, drive, dir, fname, ext );

then in

if(!strcmp(ext,".ico"))    
{

}

now you are testing the file ext aganist .ico.
   

 
0
 

Author Comment

by:airworm
Comment Utility
thanx makerp


but as i do not hold even half of your strong grasp on C, i received these after compiling

Compiling...
ico2bmp.c
Linking...
LIBCD.lib(crt0.obj) : error LNK2001: unresolved external symbol _main
Debug/ico2bmp.exe : fatal error LNK1120: 1 unresolved externals
Error executing link.exe.

ico2bmp.exe - 2 error(s), 0 warning(s)


perhaps you would also be kind enough to elaborate ur _splitpath function?
as i am afraid i have gotten it wrongly done
0
 
LVL 10

Expert Comment

by:makerp
Comment Utility
splitpath takes ff.name (a valid full path) and splits it into 4 consituant parts. drive dir filename and extension. now the reason i suggested this is so every time you find a file in your path you spilt it and then just compare the extenstion against ".ico"

#include <io.h>

_splitpath( ff.name, drive, dir, fname, ext );

now you other error is a linker error which means your code is compiling correctly but when the linker is parsing the object files it cannt resolve a external symbol _main. this is probably because your projerct settings are incorrect for the type of work youy are doing ..

now when you create a new project select 'Win 32 consolue application' not 'Win 32 application' , remember that your writting a cmd line app.

also you will have to define a main function as the entry point for your program.

i.e

void main(void)
{
   scan_path("D:\iconpath")
}

either define the scan function ABOVE main or provide a function declaration above main

i.e

// headers
#include <stdio.h>
#include <conio.h>
#include <string.h>
#include <io.h>

// globals
char command[50];

// function declarations
int scan_path(char *path);

// our entry point function
void main(void)
{
   scan_path("D:\iconpath")
}

// our function definitions
int scan_path(char *path)
{
   // code
}

try creating a new project a do what ive suugested. you should be fine..

good luck, let us know how you get on.


0
 

Author Comment

by:airworm
Comment Utility
makerp!!
thanx..overjoyed..almost there..it worked but maybe i wrote it wrongly..
it scanned through the directories and it read all files and converts the exe files to ico then to bmp. That's one of the functionality of the ico2bmp.exe but i am not sure why it doesn't convert *.ico files to *.bmp when it should.it reads the .ico files says Found file but it doesn't convert it.
Could i have overlooked somewhere again?


 else
               {
                     /* if its a file then */
                     if(ff.attrib^_A_SYSTEM)
                     {
                             
                               _splitpath( ff.name, drive, dir, fname, ext );
                               /*now you are testing the file ext aganist .ico.*/
                              if(strcmp(ext,".ico"))    
                              {
                                          printf("Found File %s\n",ff.name);
                                          sprintf(command,"c:\\temp\\icon\\Ico2Bmp.exe %s \n\r",ff.name);
                                          system(command);
                              }
                              

almost there! thank you so much!
0
 
LVL 10

Expert Comment

by:makerp
Comment Utility
glad to help.

its if(!str,, see below

if(!strcmp(ext,".ico"))      
{
    printf("Found File %s\n",ff.name);
    sprintf(command,"c:\\temp\\icon\\Ico2Bmp.exe %s \n\r",ff.name);
    system(command);
}


0
 
LVL 10

Expert Comment

by:makerp
Comment Utility
also i just use \n not \n\r at the end of command.

if this dont work it sound like a problem with your Ico2Bmp.exe..
0
 

Author Comment

by:airworm
Comment Utility
Yes!just added the path in front of the icon to bitmap program.

thanks a million makerp!

the final code on that last part is as follows

_splitpath( ff.name, drive, dir, fname, ext );
                               /*now you are testing the file ext aganist .ico.*/
                              if(!strcmp(ext,".ico"))    
                              {
                                          printf("Found File %s\n",ff.name);
                                          sprintf(command,"c:\\temp\\icon\\Ico2Bmp.exe c:\\temp\\icon\\%s \n",ff.name);
                                        printf("**%s**\n",command);
                                          system(command);
                              }


thank you once again makerp!
0
 
LVL 10

Expert Comment

by:makerp
Comment Utility
no problem
0

Featured Post

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

Summary: This tutorial covers some basics of pointer, pointer arithmetic and function pointer. What is a pointer: A pointer is a variable which holds an address. This address might be address of another variable/address of devices/address of fu…
This is a short and sweet, but (hopefully) to the point article. There seems to be some fundamental misunderstanding about the function prototype for the "main" function in C and C++, more specifically what type this function should return. I see so…
The goal of this video is to provide viewers with basic examples to understand recursion in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use conditional statements in the C programming language.

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now