Results 1 to 5 of 5

Thread: Simple C/C++ program

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Feb 2006
    Posts
    91
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows
    Thanks
    4

    Default Simple C/C++ program

    It was not long time ago that I wrote a simple C/C++ program that uses XOR encryption/decryption ( the detailed post in this topic was interesting). However, I am not so good at programming, I'm just at intermediate or may be even at beginner level. Nowe, someone quoted form seeing the following code that I don't know nothing about escaping special characters, can any one point out

    Qt Code:
    1. #include<iostream>
    2. #include<cstdlib>
    3. #include<fstream>
    4. #include<cstdio>
    5. using namespace std;
    6. void encryptdecrypt(string& data,string& key)
    7. {
    8. int keypos = 0;
    9.  
    10. for(int i = 0;i<data.length();i++)
    11. {
    12. data[i] = data[i] ^ key[keypos];
    13. keypos++;
    14. if(keypos >= key.length())
    15. keypos = 0;
    16. }
    17. }
    18. int main(int argc,char** argv)
    19. {
    20. string key = "";
    21.  
    22. char c;
    23. string data = "";
    24. if(argc == 1)
    25. {
    26. cout << "Usage: progname filename";
    27. return 0;
    28. }
    29. cout << "Enter a password : ";
    30. while((c = getchar()) != '\n' )
    31. {
    32. key += c;
    33. }
    34.  
    35. FILE *fp;
    36. fp = fopen(argv[1],"rb");
    37. if(fp == NULL)
    38. {
    39. cout << "Sorry, File could not be opened.";
    40. return 0;
    41. }
    42.  
    43. while(1)
    44. {
    45. c = fgetc(fp);
    46. if(feof(fp))
    47. break;
    48. else
    49. data+=c;
    50.  
    51. }
    52.  
    53. encryptdecrypt(data,key);
    54.  
    55. fclose(fp);
    56.  
    57. FILE *fout;
    58. fout = fopen(argv[1],"wb");
    59. if(fout == NULL)
    60. {
    61. cout << "Sorry, File could not be opened.";
    62. return 0;
    63. }
    64. for(int i = 0;i<data.length();i++)
    65. fputc(data[i],fout);
    66.  
    67. fclose(fout);
    68. getchar();
    69. return 0;
    70.  
    71. }
    To copy to clipboard, switch view to plain text mode 

    Also, how can I improve this small program ...
    Thanks.
    Last edited by ct; 22nd August 2007 at 18:57. Reason: Indented Code
    Humans make mistake because there is really NO patch for HUMAN STUPIDITY

Similar Threads

  1. QT MySQL
    By sabeeshcs in forum Newbie
    Replies: 6
    Last Post: 12th January 2007, 05:19
  2. how to use progressBar in simple program
    By jyoti in forum Newbie
    Replies: 1
    Last Post: 1st December 2006, 13:16
  3. Writing a simple Draw program
    By neomax in forum General Discussion
    Replies: 1
    Last Post: 23rd November 2006, 07:53
  4. Release my simple program to other users ?
    By probine in forum Qt Programming
    Replies: 9
    Last Post: 10th July 2006, 00:42
  5. Enter key causing program to exit
    By welby in forum Qt Programming
    Replies: 2
    Last Post: 9th March 2006, 17:11

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Qt is a trademark of The Qt Company.