Link to home
Start Free TrialLog in
Avatar of romram
romram

asked on

argc parameter

I have a problem in the following code, the problem is that when I dubug it I found that it print the first massege and then exit immidiatly without going to the code that will take the file name (not that I try to simulate compiler's work) I noticed that the error may in argc, but I really don't know how it is work???


main( int argc, char * argv[] )
{
  char pgm[120]; /* source code file name */
  if (argc != 2)
    { fprintf(stderr,"usage: %s <filename>\n",argv[0]);
      exit(1);
    }
  strcpy(pgm,argv[1]) ;
  if (strchr (pgm, '.') == NULL)
     strcat(pgm,".tny");
  source = fopen(pgm,"r");
  if (source==NULL)
  { fprintf(stderr,"File %s not found\n",pgm);
    exit(1);
  }
Avatar of AlexFM
AlexFM

This means you run program without arguments.
Your debugger should allow to pass arguments to debugged program. For example, VC++ allows to add arguments in Project - Settings - Debug - Program arguments. If you are working with another debugger, find such option in it.
Avatar of romram

ASKER

can you tell me how to do such thing???
Avatar of romram

ASKER

I put a file with .tny extention in the same root of this file , you don't think that it may be the problem...
Your program should be called with argument:

myprogram.exe filename

If it is called without argument, it prints Usage message and exits.
Avatar of romram

ASKER

what do yo mean by this?
"Your program should be called with argument:"
 
I would like to explain my program more for you:
1. the cod I put is a part of a program that is going to work as a compiler
2. so this is part of my main file which will start with the code you see
3. I put in the same root with all the files of my small compiler ,a .tny file "a file for the language of my compiler"

4.strcpy(pgm,argv[1]) ;
  if (strchr (pgm, '.') == NULL)
     strcat(pgm,".tny");
  source = fopen(pgm,"r");
  if (source==NULL)
  { fprintf(stderr,"File %s not found\n",pgm);
    exit(1);
  }

this code must take the file .tny file and open it>>>but I dont reach this stage because of the argc problem...

Thankyou...


ASKER CERTIFIED SOLUTION
Avatar of Ajar
Ajar

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Narendra Kumar S S
romram,

There is no problem with argc. It is just a parameter to the main() function.
argc will get its value when main() is called....
Who calls the main funcion!?
main() is called by the startup routine......and this takes the number of command line paramters that you gave while executing the program and assigns it to argc!
So, if the name of your executable is my_prog and at command prompt(%) you type:
%my_prog file1
and hit enter, then argc will get the value of 1.

I think the problem is not faced during execution....it is during debugging only.....am I right!?:-)
If yes, then do this.
Supposing you are using gdb (gnu debugger) and sending commands thru command prompt, type the following command:
%gdb my_prog
Now you will get gdb prompt and at that prompt type:
gdb: run file1

Now it will not goto the first printf() and will reach the next statements.....

But before all this......
You must compile your program with debugging flag enabled. So, if you are using gcc, then the compilation command will be:
%gcc -g my_prog.c -o my_prog

You might have done all this and still facing problem......if that's true, then you will have to explain the problem more clearly so that we will understand it and can think of some solution....:-)