By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,190 Members | 912 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,190 IT Pros & Developers. It's quick & easy.

What's Wrong with this program

dfound
P: 52
Expand|Select|Wrap|Line Numbers
  1. #include<windows.h>
  2. #include<dos.h>
  3. #include<dir.h>
  4. #include<fstream.h>
  5. #include<stdio.h>
  6. #include<process.h>
  7.  
  8. #include"resource.h"       //resource header file
  9.  
  10.  
  11. long convert(char *pass)   //to convert password in to long integer
  12. {
  13. long total=0;
  14. for(int i=0;i<strlen(pass);i++)
  15. {
  16.  total+=pass[i];
  17.  }
  18.  total=total;
  19. return(total);
  20. }
  21.  
  22.  
  23. int check(char *p)     // to check if the path entered is a file or directory
  24. {
  25. int done;
  26. struct ffblk ffblk;
  27. done=findfirst(p,&ffblk,0);
  28. while(!done)
  29. {
  30. if(ffblk.ff_attrib&&FA_DIREC)    //returns 1 if its a directory
  31. return(1);
  32. done=findnext(&ffblk);
  33. }
  34. return(0);
  35. }
  36.  
  37. int encode(char *path,int op,char *pass) //fuction to encode file
  38. {
  39. long password=0;
  40. int done;
  41. char ch;
  42. double ctr;
  43. ifstream fin;
  44. ofstream fout;
  45. struct ffblk ffblk;
  46. switch(op)
  47. {
  48. case 0:ctr=0;
  49.         password=convert(pass);
  50.        fin.open(path,ios::in|ios::binary);
  51.        fout.open(path,ios::out|ios::ate|ios::nocreate|ios::binary);
  52.        if(!fin||!fout)     //place where something goes wrong I think
  53.        return(-1);
  54.        while(!fin.eof())
  55.        {
  56.         fin.seekg(ctr);
  57.         fin.get(ch);         //reading each character
  58.         if(fin.eof())
  59.         break;
  60.         ch=int(ch+password); //adding some value to character(encoding)
  61.         fout.seekp(ctr);
  62.         fout.put(ch);
  63.         ctr++;
  64.         }
  65.         fout.flush();
  66.         fin.close();
  67.         fout.close();
  68.         break;
  69.  
  70. case 1:chdir(path);         //changing directory if the entere path is a folder
  71.        done=findfirst("*.*",&ffblk,0);     //searching every file
  72.        while(!done)
  73.        {
  74.        encode(ffblk.ff_name,0,pass);  
  75.        done=findnext(&ffblk);
  76.        }
  77.        break;
  78. default:return(-1);
  79. }
  80. return(0);
  81. }
  82.  
  83. int decode(char *path,int op,char *pass) //to decode file:similar to encode
  84. {
  85. long password=0;
  86. int done;
  87. char ch;
  88. double ctr;
  89. ifstream fin;
  90. ofstream fout;
  91. struct ffblk ffblk;
  92. if(op==-1)
  93. return(-1);
  94. switch(op)
  95. {
  96. case 0:ctr=0;
  97.         password=convert(pass);
  98.        fin.open(path,ios::in|ios::binary);
  99.        fout.open(path,ios::out|ios::ate|ios::nocreate|ios::binary);
  100.        if(!fout||!fin)
  101.        return(-1);
  102.        while(!fin.eof())
  103.        {
  104.         fin.seekg(ctr);
  105.         fin.get(ch);
  106.         if(fin.eof())
  107.         break;
  108.         ch=ch-int(password);
  109.         fout.seekp(ctr);
  110.         fout.put(ch);
  111.         ctr++;
  112.         }
  113.         fout.flush();
  114.         fin.close();
  115.         fout.close();
  116.         break;
  117. case 1:chdir(path);
  118.        done=findfirst("*.*",&ffblk,0);
  119.        while(!done)
  120.        {
  121.        decode(ffblk.ff_name,0,pass);
  122.        done=findnext(&ffblk);
  123.        }
  124.        break;
  125. }
  126. return(0);
  127. }
  128.  
  129.  
  130. //No problem here I think.Here's where the dialog box stuff go
  131.  
  132. BOOL CALLBACK DlgProc(HWND hwnd,UINT msg,WPARAM wParam,LPARAM lParam)
  133. {
  134. switch(msg)
  135. {
  136. case WM_INITDIALOG:SetDlgItemText(hwnd,IDC_TEXT_I,""); 
  137.               SetDlgItemText(hwnd,IDC_TEXT_II,"");
  138.               break;
  139. case WM_COMMAND   :
  140.                    switch(LOWORD(wParam))
  141.                    {
  142.                    case IDC_ENCODE:{
  143.                    int len1=GetWindowTextLength(GetDlgItem(hwnd,IDC_TEXT_I));
  144.                    int len2=GetWindowTextLength(GetDlgItem(hwnd,IDC_TEXT_II));
  145.                    if(len1>0&&len2>0)
  146.                    {
  147.                     char *buf1,*buf2;
  148.                     buf1=(char*)GlobalAlloc(GPTR,len1+1);
  149.                     buf2=(char*)GlobalAlloc(GPTR,len2+1);
  150.                     GetDlgItemText(hwnd,IDC_TEXT_I,buf1,len1+1);
  151.                     GetDlgItemText(hwnd,IDC_TEXT_II,buf2,len2+1);
  152.                     int i=check(buf1);
  153.                     int e=encode(buf1,i,buf2);
  154.                     if(e==0)
  155.                     {
  156.                     MessageBox(hwnd,"ENCODING COMPLETED","REPORT",MB_OK|MB_ICONINFORMATION);
  157.                     SetDlgItemText(hwnd,IDC_TEXT_I,"");
  158.                     SetDlgItemText(hwnd,IDC_TEXT_II,"");
  159.                     }
  160.                     else if(e==-1)
  161.                     {
  162.                     MessageBox(hwnd,"ERROR IN ENCODING PROCESS","REPORT",MB_OK|MB_ICONEXCLAMATION);//This shows up as error
  163.  
  164.                     }
  165.                     GlobalFree((HWND)buf1);
  166.                     GlobalFree((HWND)buf2);
  167.                     }
  168.                     else
  169.                     {
  170.                     MessageBox(hwnd,"PARAMETER(S) MISSING","WARNING",MB_OK|MB_ICONEXCLAMATION);
  171.                     }}
  172.                     break;
  173.                     case IDC_DECODE:{
  174.                    int len1=GetWindowTextLength(GetDlgItem(hwnd,IDC_TEXT_I));
  175.                    int len2=GetWindowTextLength(GetDlgItem(hwnd,IDC_TEXT_II));
  176.                    if(len1>0&&len2>0)
  177.                    {
  178.                     char *buf1,*buf2;
  179.                     buf1=(char*)GlobalAlloc(GPTR,len1+1);
  180.                     buf2=(char*)GlobalAlloc(GPTR,len2+1);
  181.                     GetDlgItemText(hwnd,IDC_TEXT_I,buf1,len1+1);
  182.                     GetDlgItemText(hwnd,IDC_TEXT_II,buf2,len2+1);
  183.                     int i=check(buf1);
  184.                     int e=decode(buf1,i,buf2);
  185.                     if(e==0)
  186.                     {
  187.                     MessageBox(hwnd,"ENCODING COMPLETED","REPORT",MB_OK|MB_ICONINFORMATION);
  188.                     SetDlgItemText(hwnd,IDC_TEXT_I,"");
  189.                     SetDlgItemText(hwnd,IDC_TEXT_II,"");
  190.                     }
  191.                     else if(e==-1)
  192.                     {
  193.                     MessageBox(hwnd,"ERROR IN DECODING PROCESS","REPORT",MB_OK|MB_ICONEXCLAMATION);//This shows up as error
  194.                     }
  195.                     GlobalFree((HWND)buf1);
  196.                     GlobalFree((HWND)buf2);
  197.                     }
  198.                     else
  199.                     {
  200.                     MessageBox(hwnd,"PARAMETER(S) MISSING","WARNING",MB_OK|MB_ICONEXCLAMATION);
  201.                     }}
  202.                     break;
  203.                     case IDC_about:MessageBox(hwnd,"FE for WINDOWS\r\n   -by-  \r\nRAHUL.ES\r\nCreated:18-2-07\r\nContact:es_rahul@yahoo.co.in","ABOUT",MB_ICONINFORMATION|MB_OK);
  204.                     break;
  205.                     }
  206.                     break;
  207. case WM_CLOSE       : EndDialog(hwnd,0);
  208.                       break;
  209. default             : return FALSE;
  210. }
  211. return TRUE;
  212.  
  213.  
  214. //WinMain
  215.  
  216. int WINAPI WinMain(HINSTANCE hInstance,HINSTANCE hPrevInstance,LPSTR lpCmdLine, int nCmdShow)
  217. {
  218. HICON hMyIcon = LoadIcon(hInstance, MAKEINTRESOURCE(IDI_MYICON));
  219.  
  220. return DialogBox(hInstance, MAKEINTRESOURCE(IDD_MAIN), NULL, DlgProc)
  221. ;
  222. }
  223.  
  224.  
The above program is a simple program using WinApi to encode and decode files.But every time I execute it,the dialog box mentioned above in the comments appear.I can't figure out how to correct the error...

Please Help.... :-)
Feb 18 '07 #1
Share this Question
Share on Google+
3 Replies


DeMan
100+
P: 1,806
Looks to me like that's what it does......sorry maybe I don't understand ur question
Feb 20 '07 #2

RedSon
Expert 5K+
P: 5,000
The dialog box does appear, what is your problem exactly?
Feb 20 '07 #3

dfound
P: 52
Sorry,I meant the Message Box ,not Dialog Box.The Message Box appears saying "ERROR DURING ENCODING PROCESS"
What should I do???
Feb 22 '07 #4

Post your reply

Sign in to post your reply or Sign up for a free account.