Like Tree3Likes

Help with strncpy - crypt code

This is a discussion on Help with strncpy - crypt code within the C++ Programming forums, part of the General Programming Boards category; Hi everybody! This is my first post in this forum : ) I am newbie but with a huge interest ...

  1. #1
    Registered User
    Join Date
    May 2011
    Posts
    8

    Help with strncpy - crypt code

    Hi everybody!
    This is my first post in this forum : )
    I am newbie but with a huge interest to learn Borland C++ ( 5.5.1)

    I have a piece of code here that is burning my brain : (

    The code is simple :

    I put a word(or phrase - I do not want limitation ,but lets say here “abc” ) and my code will get each letter of this word and replace with my “Code[] array”

    So lets say that
    A=duL
    B= 1TQ
    And c = YTp

    In the end of the function , instead of I have “abc” , I will get a string with “ duL1TQ YTp”. Later ,I have the decrypt function , which is going to write back to me “abc”.
    However , I am facing 2 problems :


    a) For all word that I write, always there is some “extra ASC characters” in the begin og my string.
    For example to “abc” , I get “asc code abc” . I want just my “abc” ... : ( .
    I think this issue is on line 123 when I write “strncpy (letter, &text[c], 3); ”

    b) If I write a huge word like “amfrnbcduo;werlbnbdvgwetgfdefoerp” , it returns me just “asc code amfrnb”. Nothing else. I do not want limitation...

    Thank you everybody and I hope I can help anyone here as well

    Cheers!
    Szilvia
    Attached Files Attached Files

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    I've not used Borland, but if you are using it correctly you should get something like this when you compile:
    Code:
    warning: address of local variable 'Result' returned
    The array Result ceases to exist once the function stops, so your pointer that you return points to nothing in particular.

    Also I don't think you can rely on strncpy to put a \0 at the end of your string, so there's no reason to think that "letter" will hold just a letter. (Why not use an array of chars anyway?)

  3. #3
    Registered User
    Join Date
    May 2011
    Posts
    8
    Quote Originally Posted by tabstop View Post
    I've not used Borland, but if you are using it correctly you should get something like this when you compile:
    Code:
    warning: address of local variable 'Result' returned
    The array Result ceases to exist once the function stops, so your pointer that you return points to nothing in particular.

    Also I don't think you can rely on strncpy to put a \0 at the end of your string, so there's no reason to think that "letter" will hold just a letter. (Why not use an array of chars anyway?)

    Hi tabstop,
    Thank you for your comments! : )

    As for “The array Result ceases to exist...” in fact I am just having a problem in the begin of the string and not in the end...so if I write a word “abc”, it comes like “&^%abc”..I do not know why.. : (
    Re to “Why not use an array of chars anyway” , yes, I am using that :

    char* Letter[91] = {"a","b","c","d","e","f","g",...
    char* Code[91] = {"duL","1TQ","YTp","IoU","1oM","goJ....

    Any other help ?
    Thanks a million for replying me

    Szilvia

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    It doesn't run at all here (encrypt dies, probably because all your strncpy calls are broken), so who knows where you're pulling extra characters from.

    If your Letter array was in fact an array of chars, I wouldn't have made the suggestion. I again suggest you make Letter an array of chars (rather than an array of char*).

  5. #5
    Registered User
    Join Date
    May 2011
    Posts
    8
    Quote Originally Posted by tabstop View Post
    It doesn't run at all here (encrypt dies, probably because all your strncpy calls are broken), so who knows where you're pulling extra characters from.

    If your Letter array was in fact an array of chars, I wouldn't have made the suggestion. I again suggest you make Letter an array of chars (rather than an array of char*).
    Hi tabstop , thank you for trying to help me on this issue

    Unfortunately, even I am a newbie, I am afraid to disagree with you about the function is not working, because here it is working normally.. : (
    Now, a trick that I found out:

    In the function “encrypt” line : strncpy (letter, &word[b], 1);
    Is there the issue that I am gettinh the “asc code” in front of my result BUT, if after that line I put these 2 extra lines just to show me the values :

    MessageBox (0,"word to find in Position:", "S",MB_OK);
    MessageBox (0,letter, "S",MB_OK);


    It stops getting those “asc values”!!!! : S .Well, It “partially” solve the problem but I cannot have these message boxes in the code... : (
    Any clue?

    And also, about the “size of the word” , it returns to me MAX size 16 .
    So if my crypt word is char* p="abcdefghijklmnopqrstuvxz";
    It will be crypted correct, but decrypted just till “abcdefghijklmnop”.

    I noticed that my array into the encrypt function is
    char letter[16];
    which can be this the issue, but if I put something like letter[256], s in this case the function really brokes...


    Thanks again for seeing it with me

    Regards
    Szilvia

  6. #6
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    1. At some point, you need to go back to start and learn the difference between char and char*. Until that happens, I doubt you'll get much farther on this particular project.
    2. Speaking of: you discuss your letter[16] array. How much of it ever gets used? Why is it 16 long?
    3. Your use of strncpy is interesting, but broken. Until it is fixed, it is doubtful that you will ever actually match any characters at all.

  7. #7
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by tabstop View Post
    I've not used Borland, but if you are using it correctly you should get something like this when you compile:
    It's turbo C++ ... all 16 bits worth. ( http://en.wikipedia.org/wiki/Borland_C%2B%2B )

    The OP should know this compiler is so severely outdated that the code it produces not even run on 64 bit operating systems. If you're on Windows... use MSVC++ or MinGW... at least something from this millenium!

    And just in case he or she is about to rebut with "This is what the course requires"... you need to get to a school with a corriculum from this millenium... Really.
    Last edited by CommonTater; 05-14-2011 at 12:24 AM.

  8. #8
    Registered User
    Join Date
    May 2011
    Posts
    8
    Hello tabstop and CommonTater

    Thank you for your reply
    Please see my comments below.. I hope you can help me sort it out , I think I am almost there : (
    Thank you!


    1. At some point, you need to go back to start and learn the difference between char and char*. Until that happens, I doubt you'll get much farther on this particular project.
    Szilvia: Could you explain me the difference in use the pointer and the array here ? because as I humble know, in my function “encrypt” I am receiving a char * word , which I extract a letter from that and send this char* to the other function. All code is working “almost well” so far, since that I use that “message box” in the middle of the code.. : (
    Please your thoughts how can I change it in the code for it works.

    2. Speaking of: you discuss your letter[16] array. How much of it ever gets used? Why is it 16 long?
    Szilvia: I need a string with around 300 characters , how can I define that?

    3 Your use of strncpy is interesting, but broken. Until it is fixed, it is doubtful that you will ever actually match any characters at all.
    Please help me fix that..I



    PS: I am using this c++ 5.5.1 via command line just to create a DLL , which so far, I am doing it pretty well here. I agree with CommonTater that is quite old, but for what I do , it has been ok..
    The point is , the C++Builder XE Starter does the same thing that I am doing now? I always need just small pieces of code like this one here, with no interface or any other stuff.
    I installed the Visual studio 2010 here but believe or not, I coded 1-2 files there that did not work, while in c++ 5.5.1 was ok. I know that was my fault, lack of information of the language, but I think that c++5.5.1 is easier to what I need ( or perhaps C++Builder XE ?)


    Thanks!

  9. #9
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    So for starters, compare "right":
    Code:
    char Letter[91] = {'a', 'b', 'c', ...};
    to "wrong":
    Code:
    char *Letter[91] = {"a", "b", "c", ...};
    You want to compare letters with ==, as opposed to (failing to build) one-character strings and comparing with strcmp. (Notice that strings must end with a \0 character, which you are consistently failing to do -- this is why your decrypt fails so horribly; you are using strcat to tack things on to the end of your Result array, except for the part where you never initialize Result to be a valid string in the first place, hence the end of it may not exist.)

    If you think you are using more than one (in encrypt) or three (in decrypt) elements of your letter[16] array, then you are sadly deluded.

    EDIT: While we're here: why are we even using char*? Why aren't we using std::string? (I have the sneaking suspicion the answer is "because ... what's that?", or worse, "because my compiler doesn't know about it", both of which are very bad reasons.)
    Last edited by tabstop; 05-14-2011 at 04:18 AM.

  10. #10
    Registered User
    Join Date
    May 2011
    Posts
    8
    Quote Originally Posted by tabstop View Post
    So for starters, compare "right":
    Code:
    char Letter[91] = {'a', 'b', 'c', ...};
    to "wrong":
    Code:
    char *Letter[91] = {"a", "b", "c", ...};
    You want to compare letters with ==, as opposed to (failing to build) one-character strings and comparing with strcmp. (Notice that strings must end with a \0 character, which you are consistently failing to do -- this is why your decrypt fails so horribly; you are using strcat to tack things on to the end of your Result array, except for the part where you never initialize Result to be a valid string in the first place, hence the end of it may not exist.)

    If you think you are using more than one (in encrypt) or three (in decrypt) elements of your letter[16] array, then you are sadly deluded.

    EDIT: While we're here: why are we even using char*? Why aren't we using std::string? (I have the sneaking suspicion the answer is "because ... what's that?", or worse, "because my compiler doesn't know about it", both of which are very bad reasons.)
    Hi tabstop ,
    Thanks for the reply .Wow..you can see many issues that I could not and I still do not know how to sort it out : (
    I just want code a function to receive a string(”abc...n”) and for each letter, replace with my “Code” array, print, and later “decrypt” it again. Just that : ( .
    How would be the best and simplest way to code that? Could you help me with that? I am sorry,but I do not know how to fix it with your clue.. too high for a beginner : (
    Thanks!
    Szylvia

  11. #11
    Registered User
    Join Date
    May 2011
    Posts
    8
    Quote Originally Posted by tabstop View Post
    So for starters, compare "right":
    Code:
    char Letter[91] = {'a', 'b', 'c', ...};
    to "wrong":
    Code:
    char *Letter[91] = {"a", "b", "c", ...};
    You want to compare letters with ==, as opposed to (failing to build) one-character strings and comparing with strcmp. (Notice that strings must end with a \0 character, which you are consistently failing to do -- this is why your decrypt fails so horribly; you are using strcat to tack things on to the end of your Result array, except for the part where you never initialize Result to be a valid string in the first place, hence the end of it may not exist.)

    If you think you are using more than one (in encrypt) or three (in decrypt) elements of your letter[16] array, then you are sadly deluded.

    EDIT: While we're here: why are we even using char*? Why aren't we using std::string? (I have the sneaking suspicion the answer is "because ... what's that?", or worse, "because my compiler doesn't know about it", both of which are very bad reasons.)
    Hi tabstop ,
    Just to let you (and everybody) know, I ‘ve got a new code from another user that is working perfectly!
    I will share with everybody here as soon as I do some improvements.. : )
    Thanks for your help!
    S

  12. #12
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Just to let you (and everybody) know, I ‘ve got a new code from another user that is working perfectly!
    I will share with everybody here as soon as I do some improvements.. : )
    So, rather than understanding the problem and working out a solution --i.e. learning something-- you just when the scoop and poop route?

    Please don't expect my approval.
    Salem, phantomotap and Elysia like this.

  13. #13
    Registered User
    Join Date
    May 2011
    Posts
    8
    Hello CommonTater
    At least, the other user help me out and also explained the changes, instead of just say that there is something wrong and put some abstract info in the air.
    In my humble opinion, talking about a code with a newbie, we must not just say that a new version of the software is better or a link of wikipedia, but comment the code, make suggestions and point out where is the issue and why.
    Otherwise,there is no point to anybody ask help in the forums ... thanks pseudocoder that could explain it for me .
    Best Regards
    Szilvia

  14. #14
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    22,915
    Were you not given advice already?
    Here is my advice:
    1) Dump Borland C++. No, it's not better for your purpose. No, it does not work fine. Seriously, DUMP IT. Get Visual Studio or Mingw. If your code doesn't compile on them, your code is broken. So fix it, instead of going back to your old compiler. Don't know how? ASK! You will not do anyone any favors by using it. You will be doing every a disfavor by using that old compiler.
    2) Use proper, safe C++. Use std::string. Dump char, char* and strncpy, strcpy, strcmp and what else crap is there.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  15. #15
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by szylvia View Post
    Hello CommonTater
    At least, the other user help me out and also explained the changes, instead of just say that there is something wrong and put some abstract info in the air.
    Ok... and how much did you learn by having someone simply hand you the fixed code?
    Are you a better programmer for the experience, did it help you develop new coding skills?

    I rather think not.

    There's an old saying: "Give a man a fish and you feed him for one day. Teach a man to fish and you've fed his entire family for life."


    In my humble opinion, talking about a code with a newbie, we must not just say that a new version of the software is better or a link of wikipedia, but comment the code, make suggestions and point out where is the issue and why.
    Otherwise,there is no point to anybody ask help in the forums ... thanks pseudocoder that could explain it for me .
    But, in all sincerity... one of your issues is that compiler. We dislike it for very good reasons.

    If you want us to comment on your code, post the code (in code tags) not as a link and ask specific questions...

    80% of the response a forum poster gets is HOW they ask the question.

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Strncpy
    By Poincare in forum C Programming
    Replies: 2
    Last Post: 07-15-2009, 10:13 PM
  2. Bitshift Crypt
    By ratte in forum C++ Programming
    Replies: 11
    Last Post: 01-10-2008, 02:48 PM
  3. strncpy
    By zmaker5 in forum C Programming
    Replies: 12
    Last Post: 07-28-2007, 05:15 AM
  4. CRYPT don' t save file
    By krakz in forum C Programming
    Replies: 5
    Last Post: 02-28-2003, 04:51 AM
  5. Problem with crypt
    By Unregistered in forum C Programming
    Replies: 15
    Last Post: 03-12-2002, 05:35 AM

Tags for this Thread


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21