
www.Usenet.com
| <-- __Chronological__ --> | <-- __Thread__ --> |
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" <[EMAIL PROTECTED]> wrote in message
news:[EMAIL PROTECTED]
> 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
>
>
>
>
| <-- __Chronological__ --> | <-- __Thread__ --> |