Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

help with command line input ....(unix)csh

Status
Not open for further replies.

JaybOt

Programmer
Apr 18, 2001
101
GB
Hi there,

Im new to 'c' and i am trying to do a pass a command line option to my program but i cannot get it to work.

Here is my code ...

#include <stdio.h>
#include <stdlib.h>

main(argv,argc)
int argc;
char *argv[];
{

printf(&quot;\nYour input was %s\n\n&quot;,argv[1]);
printf(&quot;\n&quot;);

}

when i compile it i get ....

Segmentation fault (core dumped)

Where have i gone wrong? Can somebody point me in the right direction.

Thanks,

Jaybot :-(
&quot;Always know what you say, but don't always say what you know!&quot;
 
Code:
#include <stdio.h>
#include <stdlib.h> 

int main(char *argv[])
{

printf(&quot;\nYour input was %s\n\n&quot;,argv[1]);
printf(&quot;\n&quot;);

return 0;
}

//the function declaration of your main must be
//followed by the brackets containing your code.
//main can be of void type but takes the parameter
//of an array of charachter pointers that point
//to the array of command line arguments


hope that helped MYenigmaSELF:-9
myenigmaself@yahoo.com
&quot;If debugging is the process of removing bugs, then programming must be the process of putting them in.&quot; --Dykstra
 
No, same result!

Jaybot :-( &quot;Always know what you say, but don't always say what you know!&quot;
 
Hello Jay,
You shouldn't get a segmentation fault when you compile it,
but maybe when you run it.
Don't use old style function prototypes ( K&R ), use the ANSI ones - are you using a very old book?
Check you've passed in a parameter before you try and
print it, otherwise you'll get a segmentation fault...

argv[0] = the program name
argv[1] = the first parameter

something like:

int main( int argc, char* argv[] )
{
if( argc > 0 )
{
/* print it! */
}
else
{
/* you passed no parameters... */
}
return 0;
}
 
good call heeryis. Might I pose the question, what are K&R function prototypes? I've only been programming for about 5 years and I've never heard of them, nor an &quot;old style&quot; function prototype. Is his code above legal? I've never seen anything like that but if I ever do it would be good to know. Good call on the arg checking. The other stuff just hit me first. MYenigmaSELF:-9
myenigmaself@yahoo.com
&quot;If debugging is the process of removing bugs, then programming must be the process of putting them in.&quot; --Dykstra
 
argc will always be >= 1 i believe. The first argv will be the name of the executable (a.out etc...)

Code:
for(int i = 0;i<argc;i++)
{
      printf on argv[i];
}

Matt
 
What heeryis & Zyrenthian say is right. Your code has
argc and argv reversed which will definitely cause
the program to fail. Switch 'em round.
 
Hi all,

Thanks for all your help, its now working! I did actually mean that it was giving a error when running not compiling the program.

Changing the 'main' argument to read ..

int main(int argc, char* argv[])

seemed to do the trick, although i dont quite understand why the avove and ...

int main(char *argv[])

seem to have different effects, and i dont need or use the argc anywhere else in my code. Appart from the missing 'int argc' section, which i understand is the number of arguments, there is no diference in the code. Do i need to use argc with argv?

Sorry for my ignorance.

Jaybot :-|

&quot;Always know what you say, but don't always say what you know!&quot;
 
You do need argc with argv (the names are arbitrary, btw, but traditional). I'm not sure exactly how it happens (as you can declare main() legally w/o either), but argc and argv are passed to the program by the shell. In your first example (with argv[] in the wrong place), you got a core dump because your program tried to read the argc value as a character pointer - I'm pretty sure that 0x1 is in the kernel's space, not user space. Get used to writing:
int main(int argc, char*argv[])
(or better yet, set up a macro on your editor!), and you won't have this problem again.
 
Hi Dear,
can u please show me what the program does?
email:ripulmodi@hotmail.com
#include<stdio.h>
#include<unistd.h>

#define BUFFSIZE 8192

int main(void)
{
int n;
char buf[BUFFSIZE];

while ((n=read(0,buf,BUFFSIZE))>0)
if(write(1,buf,n) !=n)
perror(&quot;write error&quot;);

if(n<0)
perror(&quot;read error&quot;);
exit(0);
}
 
Hi,

I presume this really a new thread, but anyway it creates a buffer, then uses this to read from stdin (0) and writes it to stdout. The read is limited by the size of buf (the is BUFFSIZE). If an error occurs or the read does not read any bytes it stops. n less than 0 is an error.

The write simply write the chunks out to standard out (1).

So basic it just pass through the stdin to stdout.

cheers,
Andrew
 
in main(int argc,char *argv[])
it is correct that argc will always equal at least 1.
if you wanted to find a certain character from argv
you could use argv[0][0] which would give you the first character of the command line. if you called ./a.out
and used printf (&quot;%c&quot;,argv[0][0]); you would get a '.' on the screen.

this is because you're declaring a pointer to a group of strings. or essentially an array of strings, which in the process is a two dimensional array.

here is what it looks like
[.][/][a][.][o][t][\0]
[n][e][x][t][a][r][g][\0]
 
also, if you're using g++ to compile...
compile with: g++ -g <the rest>

then type gdb a.out
while in gdb type run
it'll tell you where it fails..

wait.. with command line input... it might work..
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top