Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > Newbie seeking VB to ANSI C Conversion assistance

Reply
Thread Tools

Newbie seeking VB to ANSI C Conversion assistance

 
 
Mark McIntyre
Guest
Posts: n/a
 
      11-07-2003
On Thu, 6 Nov 2003 06:40:40 +0000 (UTC), in comp.lang.c , Richard
Heathfield <(E-Mail Removed)> wrote:

>[followups set]
>
>Alan Balmer wrote:
>
>> On Wed, 5 Nov 2003 16:19:41 -0500, "Jeff Goslin" <(E-Mail Removed)>
>> wrote:
>>
>>>We make the variables substantially
>>>larger than they will ever need to be(who has a 250 character name, after
>>>all?),


The heir assumptive to the throne of the Great Britain for one. And I
recently worked with a chap from Sri Lanka whose first and last names
were both over 32 chars long. Which gave him a nasty problem logging
into Windows NT by the way.

>It is much more likely to be tried by a cracker or a script-kiddie.


whats sad is that it still seems to work....

--
Mark McIntyre
CLC FAQ <http://www.eskimo.com/~scs/C-faq/top.html>
CLC readme: <http://www.angelfire.com/ms3/bchambless0/welcome_to_clc.html>


----== Posted via Newsfeed.Com - Unlimited-Uncensored-Secure Usenet News==----
http://www.newsfeed.com The #1 Newsgroup Service in the World! >100,000 Newsgroups
---= 19 East/West-Coast Specialized Servers - Total Privacy via Encryption =---
 
Reply With Quote
 
 
 
 
Alan Balmer
Guest
Posts: n/a
 
      11-07-2003
On Fri, 07 Nov 2003 00:22:00 +0000, Mark McIntyre
<(E-Mail Removed)> wrote:

>On Thu, 6 Nov 2003 06:40:40 +0000 (UTC), in comp.lang.c , Richard
>Heathfield <(E-Mail Removed)> wrote:
>
>>[followups set]
>>
>>Alan Balmer wrote:
>>
>>> On Wed, 5 Nov 2003 16:19:41 -0500, "Jeff Goslin" <(E-Mail Removed)>
>>> wrote:
>>>
>>>>We make the variables substantially
>>>>larger than they will ever need to be(who has a 250 character name, after
>>>>all?),

>
>The heir assumptive to the throne of the Great Britain for one. And I
>recently worked with a chap from Sri Lanka whose first and last names
>were both over 32 chars long. Which gave him a nasty problem logging
>into Windows NT by the way.
>
>>It is much more likely to be tried by a cracker or a script-kiddie.

>
>whats sad is that it still seems to work....


And will continue to work as long as we have the type of programming
practices espoused by the OP.

BTW, you missed a step in the attribution chain above - Nothing quoted
was written by me.

--
Al Balmer
Balmer Consulting
http://www.velocityreviews.com/forums/(E-Mail Removed)
 
Reply With Quote
 
 
 
 
Mark McIntyre
Guest
Posts: n/a
 
      11-08-2003
On Fri, 07 Nov 2003 08:15:38 -0700, in comp.lang.c , Alan Balmer
<(E-Mail Removed)> wrote:

>
>BTW, you missed a step in the attribution chain above - Nothing quoted
>was written by me.


oops, sorry, clumsy scissors.


--
Mark McIntyre
CLC FAQ <http://www.eskimo.com/~scs/C-faq/top.html>
CLC readme: <http://www.angelfire.com/ms3/bchambless0/welcome_to_clc.html>


----== Posted via Newsfeed.Com - Unlimited-Uncensored-Secure Usenet News==----
http://www.newsfeed.com The #1 Newsgroup Service in the World! >100,000 Newsgroups
---= 19 East/West-Coast Specialized Servers - Total Privacy via Encryption =---
 
Reply With Quote
 
John H. Guillory
Guest
Posts: n/a
 
      11-12-2003
On Wed, 5 Nov 2003 16:19:41 -0500, "Jeff Goslin" <(E-Mail Removed)>
wrote:

>> What happens if strlen(argv[1]) is larger than 255? 255 characters will

>be

The problem I forsee isn't with argv[1] being 255 characters. What I
forsee is the fact that in his original message, he planned on passing
eg. Jeff Goslin as the name, and argv[1] in that case will return
Jeff..... You'll need to do something like:

int main(int argc, char **argv, char **arge) {
int t;
char username[255];
char regcode[255];

if (argc > 0) {
strcpy(username,argv[1]);
for (t=2; t<argc; ++t) {
strcat(username,argv[t]);
}
}

}

And yes, you'd want another if/then, or handle if argc is just 1.....
I just have 2 questions....

1> Why can't this program be done in Visual Basic?
2> How do you plang to pass the regcode back to the program?

>> written to userName, giving you an unterminated string. Even if the

>prototype
>> for main were correct (it isn't) and if the code checked argc to make sure
>> that argv[1] exists before attempting to use it (it doesn't), this fact

>makes
>> this a dangerous example to follow.

>
>Yes, it has been pointed out to me that this code sample is a fairly inept
>sample set of code, but it was the sample template that was provided, so I
>thought I should stick to it as best as possible.
>
>While technically you are correct that a well written program should handle
>all idiot-user mistakes and all types of input, the practicality of the
>situation is far easier to deal with. We make the variables substantially
>larger than they will ever need to be(who has a 250 character name, after
>all?), and everything more or less takes care of itself. Instead of
>spending all sorts of time figuring out how users will work to break the
>code, it's going to be used so infrequently as to warrant handling
>exceptions on a case by case basis. Risk vs Reward analysis and all that.
>I could spend many hours making DAMN sure the thing never broke, OR I could
>spend a lot less time, and just make it so huge that it'll never be pressed
>like that. I realize that it's not the ideal way to do code, but time is
>money, and this code doesn't warrant any more time than the bare minimum to
>get it working.
>
>I can hear all you coding purists out there gasping for breath right now...
>on this job, it's just not worth it to totally idiot-proof the thing...
>Besides, I believe the argument being passed in is going to be truncated at
>250 characters or something, so we should be covered.


 
Reply With Quote
 
Jimmy
Guest
Posts: n/a
 
      11-23-2003
Try this.
The issue with the strncpy is that it will always copy 'n' number of chars.
If the source string is fewer than 'n' then the destination string will be
padded out to 'n' chars with nulls.
If you're continuing to parse the command line, such as *(argv+2) into
regCode, you just repeat the same steps. Obviously change the argc test to
the equivalent number you're expecting.

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

int main(int argc, char **argv)
{
// First parameter is user's full name
char userName[255];
char regCode[255];

if(argc == 2)
strncpy(userName, *(argv+1), strlen(*(argv+1)));
else
return 1;
// Code or function calls to
// place registration code into
// regCode variable

printf("%s\n",regCode);
return 0;
};

//alternative 2
#include <string.h>
#include <stdio.h>

int main(int argc, char **argv)
{
// First parameter is user's full name
char userName[255];
char regCode[255];

if(argc == 3)
{
strncpy(userName, *(argv+1), strlen(*(argv+1)));
strncpy(regCode, argv[2], strlen(argv[2])); //you can use array
notation. it does make it easier to read, though there is a little more
overhead involved at runtime.
}
else
return 1;
// Code or function calls to
// place registration code into
// regCode variable

printf("%s %s\n",userName,regCode);
return 0;
};


--
J
"Jeff Goslin" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> Hello everyone,
>
> I'm an MCSD(on the VB track) who has to convert a relatively simple

program
> from VB into ANSI compliant C. The problem is that it's giving me memory
> access violation errors when I run the program. The program compiles just
> fine, but when I try to run it, I get the error. I can only assume that
> this problem is associated with my admittedly limited understanding of
> string manipulation and pointer usage in C. Any suggestions at all are
> greatly appreciated.
>
> The program is a fairly simple registration code generator. Given a

user's
> name, it spits out a registration code specific to that user. It's meant

to
> be as generic as possible so I can use it in different programs with
> different seeds and decoding strings.
>
> The code template I was told to follow is this:
>
> #include <string.h>
>
> int main(int argc, void *argv[])
> {
> // First parameter is user's full name
> char userName[255];
> char regCode[255];
>
> strncpy(userName, argv[1], 255);
>
> // Code or function calls to
> // place registration code into
> // regCode variable
>
> printf("%s\n",regCode);
> return 0;
> };
>
>
> I had to make a few modifications to get it to compile(which leads me to
> believe that something is wrong with my code), so... anyone??? Help???
>
> The following code COMPILES, but does not RUN. Examples of what is trying
> to be accomplished has been included for ease of following the code. If

the
> input from the command line is my name, "Jeff Goslin", the output, given

the
> following code, should be "131HFASRR4"
>
> // RegKeyC.cpp : Defines the entry point for the application.
> //
>
> #include <string.h>
> #include <stdio.h>
>
> int main(int argc, void *argv[])
> {
> // First parameter is user's full name
> // EX: "Jeff Goslin"
> char *userName;
> char *regCode;
>
> char *seed;
> char *decoder;
>
> char *tmpRevPre;
> char *tmpFwdPre;
> char *tmpFinal;
> char *preencode;
> int curpos=1;
> int totalasc;
>
> // snag the user name from the passed in arguments
> strcpy(userName, (char *)argv[1]);
>
> // initialize the seed string, for use in the encoding
> strcpy(seed, "SEEDSTRING\0");
>
> // initialize the decoder string, for use in the encoding
> strcpy(decoder, "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ\0");
>
> // Code or function calls to
> // place registration code into
> // regCode variable
>
> // reverse the preencode string into a temp variable
> // hack it off to the length of the seed
> // the provided username("Jeff Goslin") is reversed("nilsoG ffeJ"),
> // the seed("SEEDSTRING") is concatenated, and the whole
> // thing is placed in preencode("nilsoG ffeJSEEDSTRING")
> strcpy(preencode,strcat(strrev(userName),seed));
>
> // the preencode variable is hacked off at 10 characters("nilsoG ffe")
> strncpy(tmpFwdPre, preencode, strlen(seed));
>
> // the preencode variable is reversed, and hacked off at 10
> characters("GNIRTSDEES")
> strncpy(tmpRevPre, strrev(preencode), strlen(seed));
>
> // encode the stuff
> while (curpos <= strlen(seed))
> {
>
> // take the ascii codes of the characters at the specified

position
> and add them together
> // in position 1, the codes for G and n will be added
> together(71+110=181)
> totalasc = tmpRevPre[curpos] + tmpFwdPre[curpos];
>
> // find the remainder when we divide it by the length of the
> decoder(36)
> // 181 mod 36 = 1
> totalasc = (totalasc % strlen(decoder)) + 1;
>
> // append the character from the decoder string in the position
> calculated to the final string
> // the character in position 1 is a "1"(0 based array, of course)
> strcat(tmpFinal, (char *)decoder[totalasc]);
>
> // increment the position counter, and repeat until done
> curpos++;
> }
>
> // put the final string("131HFASRR4") into the right variable
> strcpy(regCode, tmpFinal);
>
> // output the right variable
> printf("%s\n",regCode);
> return 0;
> };
>
> --
> Jeff Goslin - MCSD - www.goslin.info
>
>
>
>



 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
pre-ansi to ansi c++ conversion? Frank Iannarilli C++ 2 07-21-2009 11:05 PM
Seeking reentrant+threadsafe XML parser written in ANSI-C ... Roland Mainz XML 5 07-12-2008 02:23 PM
Are there statistics packages in ANSI C and/or ANSI C++? lbrtchx@gmail.com C Programming 11 04-28-2008 03:00 AM
Are there statistics packages in ANSI C and/or ANSI C++? lbrtchx@gmail.com C++ 1 04-24-2008 06:44 PM
Seeking assistance with networking addresses Anders Cisco 0 02-01-2006 02:10 PM



Advertisments