Solved

argc parameter

Posted on 2003-10-25
8
358 Views
Last Modified: 2012-06-27
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);
  }
0
Comment
Question by:romram
8 Comments
 
LVL 48

Expert Comment

by:AlexFM
ID: 9620424
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.
0
 

Author Comment

by:romram
ID: 9622114
can you tell me how to do such thing???
0
 

Author Comment

by:romram
ID: 9622118
I put a file with .tny extention in the same root of this file , you don't think that it may be the problem...
0
Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

 
LVL 48

Expert Comment

by:AlexFM
ID: 9622126
Your program should be called with argument:

myprogram.exe filename

If it is called without argument, it prints Usage message and exits.
0
 

Author Comment

by:romram
ID: 9622145
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...


0
 
LVL 6

Accepted Solution

by:
Ajar earned 250 total points
ID: 9625649
romram  .. argc is the number of parameters that you pass to your program for example when you run a program like notepad.exe as

$ Notepad.exe     romram.txt  

then you are passing  romram.txt as an argument to your program ..

In  C the  program name is also considered as a argument so in the above program the argc variable will be     2

A simple example
=example.c=============================
int main(int argc,char * argv)
{
  int i =0;
  while( i < argc) {printf("ARGUMENT  NUMBER %d  is %s\n",i,argv[i]);i++;}
}
==============================
Now suppose you run compile this program and make an  example.exe out of it then
when you run it like this
$example.exe    abc    xyz  
the output will be

$ARGUMENT NUMBER 0 is  example.exe
$ARGUMENT NUMBER 1 is  abc
$ARGUMENT NUMBER 2 is  xyz

============================================

so what you need to do in your program is that pass  ".tny "  as the argument to the prgoram


Hope you understand
0
 
LVL 8

Expert Comment

by:ssnkumar
ID: 9786120
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....:-)
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

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…
Windows programmers of the C/C++ variety, how many of you realise that since Window 9x Microsoft has been lying to you about what constitutes Unicode (http://en.wikipedia.org/wiki/Unicode)? They will have you believe that Unicode requires you to use…
The goal of this video is to provide viewers with basic examples to understand opening and writing to files in the C programming language.
The goal of this video is to provide viewers with basic examples to understand recursion in the C programming language.

708 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

13 Experts available now in Live!

Get 1:1 Help Now