home *** CD-ROM | disk | FTP | other *** search
/ Internet Pratica / IPRAT_01.iso / ASP / ASPFusion_Basic_Suite / examples / c++ / advfile / AdvFile.cpp next >
Encoding:
C/C++ Source or Header  |  2002-01-26  |  14.9 KB  |  344 lines

  1. /********************************************************************************************************************
  2. Methods:
  3. 1. Append()        append the source file with data given
  4. 2. Copy()        copy the given source file to destination
  5. 3. Delete()        delete the given source file 
  6. 4. Move()        move the given source file to destination
  7. 5. Read()        read the given source file
  8. 6. Rename()        rename the given source file to destination
  9. 7. Write()        write the source file with data given
  10.  
  11. Properties:
  12. 1. PutAddNewLine()    if this property is set to TRUE, a new line character is appended to the text that is written
  13.                     to the file. If this attribute is set to FALSE, no new line character is appended to the text.
  14.                     valid values are
  15.                     TRUE or 1 (default)
  16.                     FALSE or 0
  17. 2. PutAttributes()    a comma-delimited list of file attributes to be set on the file being processed. valid values are
  18.                     Normal (default)
  19.                     ReadOnly
  20.                     Temporary
  21.                     Archive
  22.                     Hidden
  23.                     System
  24. 3. GetErrorReason() reports any errors that occur during the request
  25. 4. GetIsError()        returns 1 if any error occur during the request otherwise 0
  26. ********************************************************************************************************************/
  27.  
  28. /***********************************/
  29. /*include all required header files*/
  30. /***********************************/
  31. #include <stdio.h>
  32. #include <conio.h>
  33.  
  34. /************************************************************************/
  35. /*import AdvDirectory COM library here to get its methods and properties*/
  36. /************************************************************************/
  37. #import "C:\AdvFile.dll" no_namespace
  38.  
  39. /*************************/
  40. /*prototype for functions*/
  41. /*************************/
  42. void Append();//Append function
  43. void Copy();//Copy function
  44. void Delete();//Delete function
  45. void Move();//Move function
  46. void Read();//Read function
  47. void Rename();//Rename function
  48. void Write();//Write function
  49.  
  50. /**************/
  51. /*main program*/
  52. /**************/
  53. void main(void)
  54. {
  55.     char Ch;
  56.     do
  57.     {
  58.         printf("\r\nAppend    [A]");
  59.         printf("\r\nCopy    [C]");
  60.         printf("\r\nDelete    [D]");
  61.         printf("\r\nMove    [M]");
  62.         printf("\r\nRead    [R]");
  63.         printf("\r\nRename    [N]");
  64.         printf("\r\nWrite    [W]");
  65.         printf("\r\nQuit    [Q]");
  66.         printf("\r\n\r\nEnter Choice = ");
  67.         scanf("%c", &Ch);
  68.         getchar();
  69.         Ch = toupper(Ch);
  70.         switch (Ch)
  71.         {
  72.             case 'A':Append();break;//call Append function
  73.             case 'C':Copy();break;//call Copy function
  74.             case 'D':Delete();break;//call Delete function
  75.             case 'M':Move();break;//call Move function
  76.             case 'R':Read();break;//call Read function
  77.             case 'N':Rename();break;//call Rename function
  78.             case 'W':Write();break;//call Write function
  79.             case 'Q':printf("\r\nGood Bye\r\n");break;
  80.             default: printf("\r\nError: Please enter valid choice\r\n");
  81.         }
  82.     }
  83.     while (Ch != 'Q');
  84. }
  85.  
  86. /*******************************************************************************/
  87. /*Append function: This function is used to append a given file with given data*/
  88. /*******************************************************************************/
  89. void Append()
  90. {
  91.     char m_Source[256], m_Data[256], m_Attributes[256];//define charecter buffer of 256 to read source file name, data and attributes
  92.     long l_NewLine;//new line flag
  93.     printf("\r\nEnter Source File Name = ");
  94.     gets(m_Source);//read source file name
  95.     printf("\r\nEnter File Data = ");
  96.     gets(m_Data);//read file data
  97.     printf("\r\nEnter New Line Flag [0/1] = ");
  98.     scanf("%d", &l_NewLine);//read new line flag
  99.     getchar();
  100.     printf("\r\nEnter File Attributes = ");
  101.     gets(m_Attributes);//read file attributes
  102.     try//for COM library call, try...catch is used to handle exceptions
  103.     {
  104.         IFilesPtr obj;//take smart pointer object obj for COM library
  105.         CoInitialize(NULL);//Initializes the COM library on the current thread
  106.         obj = IFilesPtr("AdvFile.File");
  107.         obj->PutAddNewLine(l_NewLine);//set add new line flag
  108.         obj->PutAttributes(m_Attributes);//set attributes
  109.         HRESULT hr = obj->Append(m_Source, m_Data);//call Append method of COM library
  110.         if (!SUCCEEDED(hr))//if COM call is not successfull then display error message and return
  111.         {
  112.             printf("\r\nError: %s\r\n", (char*)obj->GetErrorReason());//display error message
  113.             return;
  114.         }
  115.         //call GetIsError() property of COM library and check its value, if its value is 1, then
  116.         //GetErrorReason() property of COM will contain error message, for which this error occured
  117.         if (obj->GetIsError() == 1)
  118.             printf("\r\nError: %s\r\n", (char*)obj->GetErrorReason());//display error message
  119.         else
  120.             printf("\r\nSuccessfully Append\r\n");//display successfull message
  121.     }
  122.     catch (_com_error &error)//catch exception here
  123.     {
  124.         printf(TEXT("%S[%X]"), error.ErrorMessage(), error.Error());//display error message
  125.     }
  126. }
  127.  
  128. /*************************************************************************************/
  129. /*Copy function: This function is used to copy file form given source to given target*/
  130. /*************************************************************************************/
  131. void Copy()
  132. {
  133.     char m_Source[256], m_Destination[256], m_Attributes[256];//define charecter buffer of 256 to read source file name, destination and attributes
  134.     printf("\r\nEnter Source File Name = ");
  135.     gets(m_Source);//read source file name
  136.     printf("\r\nEnter Destination File Name = ");
  137.     gets(m_Destination);//read destination file name
  138.     printf("\r\nEnter File Attributes = ");
  139.     gets(m_Attributes);//read file attributes
  140.     try//for COM library call, try...catch is used to handle exceptions
  141.     {
  142.         IFilesPtr obj;//take smart pointer object obj for COM library
  143.         CoInitialize(NULL);//Initializes the COM library on the current thread
  144.         obj = IFilesPtr("AdvFile.File");
  145.         obj->PutAttributes(m_Attributes);//set attributes
  146.         HRESULT hr = obj->Copy(m_Source, m_Destination);//call Copy method of COM library
  147.         if (!SUCCEEDED(hr))//if COM call is not successfull then display error message and return
  148.         {
  149.             printf("\r\nError: %s\r\n", (char*)obj->GetErrorReason());//display error message
  150.             return;
  151.         }
  152.         //call GetIsError() property of COM library and check its value, if its value is 1, then
  153.         //GetErrorReason() property of COM will contain error message, for which this error occured
  154.         if (obj->GetIsError() == 1)
  155.             printf("\r\nError: %s\r\n", (char*)obj->GetErrorReason());//display error message
  156.         else
  157.             printf("\r\nSuccessfully Copied\r\n");//display successfull message
  158.     }
  159.     catch (_com_error &error)//catch exception here
  160.     {
  161.         printf(TEXT("%S[%X]"), error.ErrorMessage(), error.Error());//display error message
  162.     }
  163. }
  164.  
  165. /*************************************************************************/
  166. /*Delete function: This function is used to delete file with given source*/
  167. /*************************************************************************/
  168. void Delete()
  169. {
  170.     char m_Source[256];//define charecter buffer of 256 to read source file name
  171.     printf("\r\nEnter Source File Name = ");
  172.     gets(m_Source);//read source file name
  173.     try//for COM library call, try...catch is used to handle exceptions
  174.     {
  175.         IFilesPtr obj;//take smart pointer object obj for COM library
  176.         CoInitialize(NULL);//Initializes the COM library on the current thread
  177.         obj = IFilesPtr("AdvFile.File");
  178.         HRESULT hr = obj->Delete(m_Source);//call Delete method of COM library
  179.         if (!SUCCEEDED(hr))//if COM call is not successfull then display error message and return
  180.         {
  181.             printf("\r\nError: %s\r\n", (char*)obj->GetErrorReason());//display error message
  182.             return;
  183.         }
  184.         //call GetIsError() property of COM library and check its value, if its value is 1, then
  185.         //GetErrorReason() property of COM will contain error message, for which this error occured
  186.         if (obj->GetIsError() == 1)
  187.             printf("\r\nError: %s\r\n", (char*)obj->GetErrorReason());//display error message
  188.         else
  189.             printf("\r\nSuccessfully Deleted\r\n");//display successfull message
  190.     }
  191.     catch (_com_error &error)//catch exception here
  192.     {
  193.         printf(TEXT("%S[%X]"), error.ErrorMessage(), error.Error());//display error message
  194.     }
  195. }
  196.  
  197. /*************************************************************************************/
  198. /*Move function: This function is used to move file form given source to given target*/
  199. /*************************************************************************************/
  200. void Move()
  201. {
  202.     char m_Source[256], m_Destination[256], m_Attributes[256];//define charecter buffer of 256 to read source file name, destination and attributes
  203.     printf("\r\nEnter Source File Name = ");
  204.     gets(m_Source);//read source file name
  205.     printf("\r\nEnter Destination File Name = ");
  206.     gets(m_Destination);//read destination file name
  207.     printf("\r\nEnter File Attributes = ");
  208.     gets(m_Attributes);//read file attributes
  209.     try//for COM library call, try...catch is used to handle exceptions
  210.     {
  211.         IFilesPtr obj;//take smart pointer object obj for COM library
  212.         CoInitialize(NULL);//Initializes the COM library on the current thread
  213.         obj = IFilesPtr("AdvFile.File");
  214.         obj->PutAttributes(m_Attributes);//set attributes
  215.         HRESULT hr = obj->Move(m_Source, m_Destination);//call Move method of COM library
  216.         if (!SUCCEEDED(hr))//if COM call is not successfull then display error message and return
  217.         {
  218.             printf("\r\nError: %s\r\n", (char*)obj->GetErrorReason());//display error message
  219.             return;
  220.         }
  221.         //call GetIsError() property of COM library and check its value, if its value is 1, then
  222.         //GetErrorReason() property of COM will contain error message, for which this error occured
  223.         if (obj->GetIsError() == 1)
  224.             printf("\r\nError: %s\r\n", (char*)obj->GetErrorReason());//display error message
  225.         else
  226.             printf("\r\nSuccessfully Moved\r\n");//display successfull message
  227.     }
  228.     catch (_com_error &error)//catch exception here
  229.     {
  230.         printf(TEXT("%S[%X]"), error.ErrorMessage(), error.Error());//display error message
  231.     }
  232. }
  233.  
  234. /*************************************************************************************/
  235. /*Read function: This function is used to read file form given source to given target*/
  236. /*************************************************************************************/
  237. void Read()
  238. {
  239.     char m_Source[256];//define charecter buffer of 256 to read source file name
  240.     printf("\r\nEnter Source File Name = ");
  241.     gets(m_Source);//read source file name
  242.     try//for COM library call, try...catch is used to handle exceptions
  243.     {
  244.         IFilesPtr obj;//take smart pointer object obj for COM library
  245.         CoInitialize(NULL);//Initializes the COM library on the current thread
  246.         obj = IFilesPtr("AdvFile.File");
  247.         IDispatch *p = obj->Read(m_Source);//call Read method of COM library
  248.         //call GetIsError() property of COM library and check its value, if its value is 1, then
  249.         //GetErrorReason() property of COM will contain error message, for which this error occured
  250.         if (obj->GetIsError() == 1)
  251.             printf("\r\nError: %s\r\n", (char*)obj->GetErrorReason());//display error message
  252.         else
  253.         {
  254.             printf("\r\nSuccessfully Read\r\n");//display successfull message
  255.             IReadPtr readobj;
  256.             p->QueryInterface(IID_IDispatch,(void**)&readobj);//call query interface
  257.             printf("\r\nData = %s\r\n", (char*)readobj->GetFileContent());
  258.         }
  259.     }
  260.     catch (_com_error &error)//catch exception here
  261.     {
  262.         printf(TEXT("%S[%X]"), error.ErrorMessage(), error.Error());//display error message
  263.     }
  264. }
  265.  
  266. /*****************************************************************************************/
  267. /*Rename function: This function is used to rename file with given source to given target*/
  268. /*****************************************************************************************/
  269. void Rename()
  270. {
  271.     char m_Source[256], m_Destination[256], m_Attributes[256];//define charecter buffer of 256 to read source file name, destination and attributes
  272.     printf("\r\nEnter Source File Name = ");
  273.     gets(m_Source);//read source file name
  274.     printf("\r\nEnter Destination File Name = ");
  275.     gets(m_Destination);//read destination file name
  276.     printf("\r\nEnter File Attributes = ");
  277.     gets(m_Attributes);//read file attributes
  278.     try//for COM library call, try...catch is used to handle exceptions
  279.     {
  280.         IFilesPtr obj;//take smart pointer object obj for COM library
  281.         CoInitialize(NULL);//Initializes the COM library on the current thread
  282.         obj = IFilesPtr("AdvFile.File");
  283.         obj->PutAttributes(m_Attributes);//set attributes
  284.         HRESULT hr = obj->Rename(m_Source, m_Destination);//call Rename method of COM library
  285.         if (!SUCCEEDED(hr))//if COM call is not successfull then display error message and return
  286.         {
  287.             printf("\r\nError: %s\r\n", (char*)obj->GetErrorReason());//display error message
  288.             return;
  289.         }
  290.         //call GetIsError() property of COM library and check its value, if its value is 1, then
  291.         //GetErrorReason() property of COM will contain error message, for which this error occured
  292.         if (obj->GetIsError() == 1)
  293.             printf("\r\nError: %s\r\n", (char*)obj->GetErrorReason());//display error message
  294.         else
  295.             printf("\r\nSuccessfully Renamed\r\n");//display successfull message
  296.     }
  297.     catch (_com_error &error)//catch exception here
  298.     {
  299.         printf(TEXT("%S[%X]"), error.ErrorMessage(), error.Error());//display error message
  300.     }
  301. }
  302.  
  303. /*****************************************************************************/
  304. /*Write function: This function is used to write a given file with given data*/
  305. /*****************************************************************************/
  306. void Write()
  307. {
  308.     char m_Source[256], m_Data[256], m_Attributes[256];//define charecter buffer of 256 to read source file name, data and attributes
  309.     long l_NewLine;//new line flag
  310.     printf("\r\nEnter Source File Name = ");
  311.     gets(m_Source);//read source file name
  312.     printf("\r\nEnter File Data = ");
  313.     gets(m_Data);//read file data
  314.     printf("\r\nEnter New Line Flag [0/1] = ");
  315.     scanf("%d", &l_NewLine);//read new line flag
  316.     getchar();
  317.     printf("\r\nEnter File Attributes = ");
  318.     gets(m_Attributes);//read file attributes
  319.     try//for COM library call, try...catch is used to handle exceptions
  320.     {
  321.         IFilesPtr obj;//take smart pointer object obj for COM library
  322.         CoInitialize(NULL);//Initializes the COM library on the current thread
  323.         obj = IFilesPtr("AdvFile.File");
  324.         obj->PutAddNewLine(l_NewLine);//set add new line flag
  325.         obj->PutAttributes(m_Attributes);//set attributes
  326.         HRESULT hr = obj->Write(m_Source, m_Data);//call Write method of COM library
  327.         if (!SUCCEEDED(hr))//if COM call is not successfull then display error message and return
  328.         {
  329.             printf("\r\nError: %s\r\n", (char*)obj->GetErrorReason());//display error message
  330.             return;
  331.         }
  332.         //call GetIsError() property of COM library and check its value, if its value is 1, then
  333.         //GetErrorReason() property of COM will contain error message, for which this error occured
  334.         if (obj->GetIsError() == 1)
  335.             printf("\r\nError: %s\r\n", (char*)obj->GetErrorReason());//display error message
  336.         else
  337.             printf("\r\nSuccessfully Write\r\n");//display successfull message
  338.     }
  339.     catch (_com_error &error)//catch exception here
  340.     {
  341.         printf(TEXT("%S[%X]"), error.ErrorMessage(), error.Error());//display error message
  342.     }
  343. }
  344.