thieumao

Code nhập dữ liệu - anhkhang

Sep 15th, 2013
132
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.96 KB | None | 0 0
  1. // •  Code nhập dữ liệu
  2.  
  3. System::String ^strFile;
  4. System::String ^tmp;
  5. FILE *f;
  6. //Mở file sẽ được lấy mẫu
  7. FILE *fs;
  8. char strPathSign[]="mn.kkt";
  9. char strDatFile[32];
  10. m_Sign sign;
  11. System::Boolean isHave=false;
  12. int iTmpOffset;
  13. int i;
  14. strFile = txtPath->Text;
  15. //Convert từ chuỗi trong TextBox qua chuỗi dạng chuẩn của chương trình
  16. //Từ đưa về chuỗi với 18 ký tự
  17. //Chuyển từ dạng String -> Char
  18. char tmpName[18];
  19. System::String ^strTmpName;
  20. strTmpName=txtName->Text;
  21. for (i=0; i < strTmpName->Length;i++)
  22. {
  23.     tmpName[i] = strTmpName[i];
  24. }
  25. for (int i=strTmpName->Length; i <= 18;i++)
  26. {
  27.     tmpName[i] = 32;
  28. }
  29. //Do trong strFile là kiểu string
  30. //cần phải chuyển đổi ra thành kiểu Char để đưa vào hàm
  31. int itmp = strFile->Length;
  32. char *chTmpData = new char[itmp+1];
  33. for(int i = 0; i<itmp;i++)
  34. {
  35.     *(chTmpData + i) = strFile[i];
  36. };
  37. *(chTmpData + itmp) = '\0';
  38. fopen_s(&fs,chTmpData,"rb");
  39. fopen_s(&f,strPathSign,"a+b");
  40. int posEnd;
  41. //Xác định kích thước file
  42. fseek(f,0,SEEK_END);
  43. posEnd=ftell(f);
  44. //Trả về vị trí ban đầu
  45. fseek(f,0,SEEK_SET);
  46. //Tiến hành vòng lặp liệt kê mã nhận dạng
  47. while (ftell(f) < posEnd)
  48. {
  49.     //Đọc mẫu tin lưu mã nhận dạng virus
  50.     fread(&sign,sizeof(m_Sign),1,f);
  51.     //Nhảy trong file muốn quét tới offset muốn xét
  52.     //Offset này lấy từ sign.lPos
  53.     fseek(fs,sign.lPos,SEEK_SET);
  54.     //Lấy 32 byte tại offset đó
  55.     fread(strDatFile,32,1,fs);
  56.     if (compareStr(strDatFile,sign.Sign,32))
  57.     {
  58.         //Nhận dạng trùng mã nhận dạng
  59.         tmp = gcnew System::String(sign.Name);
  60.         isHave=true;
  61.         System::Windows::Forms::MessageBox::Show(L"Mã nhận dạng này đã có trong bảng mã",L"Trùng mã nhận dạng");
  62.         break;
  63.     }
  64.     if (compareStr(tmpName,sign.Name,18))
  65.     {
  66.         //Nhận dạng trùng tên virus đã lưu
  67.         tmp = gcnew System::String(sign.Name);
  68.         isHave=true;
  69.         System::Windows::Forms::MessageBox::Show(L"Tên virus này đã có trong bảng mã nhận dạng",L"Trùng mã nhận dạng");
  70.         break;
  71.     }
  72. }
  73. if (!isHave)
  74. {
  75.     //Sau khi kiểm tra là chưa có mã nhận dạng này + Tên virus là thao tác ghi mẫu tin dữ liệu virus mới vào
  76.     //Lấy dữ liệu tại Offset cần lấy (Tại txtOffset)
  77.     iTmpOffset=System::Int32::Parse(txtOffset->Text);
  78.     fseek(fs,iTmpOffset,SEEK_SET);
  79.     fread(strDatFile,32,1,fs);
  80.     //Đưa dữ liệu vào một biến theo Struct m_Sign
  81.     //Hàm strcpy xử lý lỗi trong những trường hợp này (Do có ký tự NULL)
  82.     //Vì vậy hàm copy 2 mảng char sẽ cần tự code lại
  83.     for (i=0;i < 32;i++)
  84.         sign.Sign[i]=strDatFile[i];
  85.     sign.lPos=iTmpOffset;
  86.     strcpy(sign.Name,tmpName);
  87.     //Nhảy đến cuối file
  88.     fseek(f,0,SEEK_END);
  89.     //Ghi dữ liệu vào cuối file
  90.     fwrite(&sign,sizeof(sign),1,f);
  91.     System::Windows::Forms::MessageBox::Show(L"Đã lưu xong mẫu tin dữ liệu virus mới",L"Thành công");
  92. }
  93. fclose(f);
  94. fclose(fs);
Advertisement
Add Comment
Please, Sign In to add comment