Visual C ++ implements file batch conversion function

zhaozj2021-02-11  259

Visual C implements file batch conversion function

Tang Yang

First, the preface I realized the function of converting all the chromatographic data files into a plain text file in the development of HP chromatographic workstation enhancements. Hereinafter, the implementation method of this function is explained by using a specific example. All code is compiled by Visual C 6.0 in Windows 95/98/2000. Second, the example first uses the MFC AppWizard to generate an SDI style application TEST, which is all defaults during the generation. Secondly, use the resource editor, add a menu item "Conversion" under the main menu "file", the property is: ID: ID_CONVERT CAPTION: Convert Prompt: Convert / N conversion file between different format files and then use "Ctrl- W "Hotkey activates the MFC ClassWizard, adds the command function on the response ID_convert message to the CMAINFRAME class.

The code to join the conversion function is as follows: void cmainframe :: onConvert () {lpmalloc pmalloc; // Using the shell extension BrowseInfo Bi; if (SUCCEETMALLOC (& PMalloc)) // Assign Free Memory for Generate Directory Selection dialog {ZeromeMory (& BI, SIZEOF (BI)); // Clear allocated space char pszdirname [max_path]; // Store selected directory name LPITEMIDLIST PIDL; bi.hwndowner = getsafehwnd (); bi.PIDLROOT = NULL; bi. pszDisplayName = pszDirName; bi.lpszTitle = _T ( "select batch convert file's directory"); bi.ulFlags = BIF_RETURNFSANCESTORS | BIF_RETURNONLYFSDIRS; bi.lpfn = NULL; bi.lParam = 0; if ((pidl = :: SHBrowseForFolder (& bi))! = null) // Call Select Directory dialog {if (: shgetpathfromidlist (pidl, pszdirname)) // Get the selected directory {file: // Set the selected directory for the current directory to find setCurrentDirectory (pszdirname); file: // Define a lookup cfileFind Findch1; CString strconv; cstring strsour; if (Findch1.FindFile ("*. CH1")) // Find {CFile Sourcefile; CSTDIOFILE TARGETFILE; Bool bfindResult; do {file: // Find the next eligible file bFINDRESULT = FINDCH1.FINDNEXTFILE (); file: // Get the found file name strsour = Findch1.GetFilePath (); strconv = strsour; file: // Convert file names to lowercase strconv.maker (); file: // convert * .ch1 type file to * .txt strconv.replace (". CH1", ". Txt"); file: // open *. CH1 type file as a source file sourcefile.open (strsour, cfile :: moderad); file: // Open * .txt type file as the target file targetfile.open (strconv, cfile :: modecreate | cfile :: modewrite);

File: // Decoding function file for files in * .ch1 type This file: // This button is called to convert the function file: // file for use, to turn off sourcefile.close (); targetfile.close () WHILE (BFINDRESULT); MessageBox ("Transformation is complete!", "Convert Food!", MB_OK;} else {MessageBox ("Did not find a ch1 file", "did not find", MB_OK);} Findch1.close () ; // Close this search} PMalloc-> free (PIDL); // Release the resource} PMalloc-> release (); // Release the resource}} to compile and run the program, select the "File" menu The "Conversion" command, select a directory to complete the conversion work of all files with the .ch1 extension of this directory. Third, the process uses the shell extension in Windows95 / 98/2000 to realize picking of a user specified directory, and then use the MFC's cfilefind class to retrieve all source files that want to convert, and finally by coding conversion Output source file content to the target file of the specified type. This method is also suitable for conversion between files between files in different formats, such as graphical files, sound files, and the like.

转载请注明原文地址:https://www.9cbs.com/read-4535.html

New Post(0)