help in C

 
SAMI AHMAD
Regular Advisor

help in C

can someone give me small example of a C program taking a string input and printing it?

thanks
19 REPLIES 19
Hoff
Honored Contributor

Re: help in C

Sure. See attached.

$ cc x
$ link x
$ run x
hi
hi
$ type x.c
#include
main()
{
char foo[255];
gets( foo );
printf( foo );
return 0;
}
$

The above code runs and does exactly what you've asked for, but it's not particularly secure nor entirely reliable. The gets call has vulnerabilities.

I've posted a far larger example C program (that does this prompting, and a whole lot more) in the newuser code in the zip archive available available via:

http://64.223.189.234/node/1135

The OpenVMS C manuals and the OpenVMS programming concepts manuals (all of which are required reading here) are available at:

http://www.hp.com/go/openvms/doc

The C manuals and the Programming Concepts manual introduce you to the language and to programming C on OpenVMS.


SAMI AHMAD
Regular Advisor

Re: help in C

hi Hoff !
I am sorry i think i explained it wrong, i dont want to input a string from command line but i want to be able to pass an argument to a procedure say x("test string")
and the program should print the string ?

thanks for help
RBrown_1
Trusted Contributor

Re: help in C

$ create demo.c
#include
void main ()
{
while (putchar (getchar ()) != EOF)
{}
}
^Z
$ cc demo
$ link demo
$ run demo
the quick brown fox jumps over the lazy dog!
the quick brown fox jumps over the laxy dog!
^Z

Figuring out how to get rid of the y-umlaut at the end is left as an exercise.
SAMI AHMAD
Regular Advisor

Re: help in C

ok here is what I am trying to do , below is my program and I want to pass 'msg' as parameter not on command line ..

SUNNY2$ type snmp.c
#include
#include

int main( char *msg)
{
int sts;
char *cmd = "mcr sys$system:tcpip$snmp_trapsnd.exe 0.0 local 0 0 0 -h 10.100.18.245 -v2c 1.3.6.1.6.3.1.1.4.1.0 \"D\" \"msg\"";

printf( " cmd: >%s<.\n", cmd);
sts = system( cmd);

printf( " sts = %%x%08x.\n", sts);
}
Hoff
Honored Contributor

Re: help in C

The C call you want here is sprintf().
RBrown_1
Trusted Contributor

Re: help in C

We have a problem with terminology here. Typically, "command line" means those lines in my and Hoff's replies like:

$ cc demo
$ link demo
$ run demo

Neither my nor Hoff's programs accepted input from the command line in that sense.

You say you want to "pass an argument to a procedure".

Do you mean like a function call? For example the printf function call:

printf ("test string\n");

Or you want to write a function that accepts a string argument and sends it to printf?

Or do you want to write a program that prints strings from the command line as in

$ demo "test string"
test string

?

In the latter case, refer to the DECC Language Reference Manual "Passing Arguments to the main Function", which is section 5.6.3 in the very old copy that I have.

You can find the DECC documentation in the usual places on the HP VMS documentation web site.
SAMI AHMAD
Regular Advisor

Re: help in C

the above program i gave is a perfectly working program ,but it has hard code string called 'msg' that it sends as snmp trap, all i want to do is to be able to replace 'msg' with a dynamic value ,
so from command prompt i should be able to do this
$sndsnmp :== $sys$login:snmp.exe
$sndsnmp "test msg"
$sndsnmp "another test"

run snmp.exe "test msg" and the prog should replace the 'msg' value in the program with "test msg" , do you know what iam saying ?
let me know otherwise i will explain further
RBrown_1
Trusted Contributor

Re: help in C

Yup. You need to read section 5.6.3.

Probably your solution will include a statement something like this:

sprintf (cmd, "long string prefix %s", argv[1]);
SAMI AHMAD
Regular Advisor

Re: help in C

hi Brown !

let me explain in a better way , I think what i need is very simple .

this is my working program now :

#include
#include

int main()
{
int sts;
char *cmd = "mcr sys$system:tcpip$snmp_trapsnd.exe 0.0 local 0 0 0 -h 10.100.18.245 -v2c 1.3.6.1.6.3.1.1.4.1.0 \"D\" \"msg\"";

printf( " cmd: >%s<.\n", cmd);
sts = system( cmd);

printf( " sts = %%x%08x.\n", sts);
}

I run it like this :

SNDSNMP == "$SYS$LOGIN:SNMP.EXE"
SUNNY2$ sndsnmp
cmd: >mcr sys$system:tcpip$snmp_trapsnd.exe 0.0 local 0 0 0 -h 10.100.18.245 -v2c 1.3.6.1.6.3.1.1.4.1.0 "D" "msg"<.
sts = %x00000001.


you see above its constructing a command with hard coded "msg" .
i want to be able to do this

$sndsnmp "test msg"
and it should construct the command

cmd: >mcr sys$system:tcpip$snmp_trapsnd.exe 0.0 local 0 0 0 -h 10.100.18.245 -v2c 1.3.6.1.6.3.1.1.4.1.0 "D" "test msg"<.