C ++ applets (1) -- file organization tools and applets
The cartoon files downloaded from the Internet are jpg, png, and other image files. it is inconvenient to use the image manager provided by the system. If you want to view images like webpages, I found a lot of image manager without this function, so I wrote a small program to implement it myself.
The idea is to add an html file in the image directory, which contains all cartoon images, so you can use a browser to watch it. The effect is much better than simply opening it on your computer.
There are two main steps:
(1) Get the names of all images in the directory and save them to a vector for backup.
(2) generate the corresponding html file.
The second step is relatively simple. You only need some simple html statements:
<body></body>
The key is the first step. How do I obtain all the file names in the target folder? The solution is to use the _ finddata_t struct and _ findfirst, _ findnext, and _ fineclose functions in <io. h> in C ++.
Struct _ finddata_t is roughly defined as follows:
Struct _ finddata_t {unsigned attrib; // time_t time_create; <span> </span> // File Creation Time: time_t time_access; <span> </span> // The last time the file was accessed: time_t time_write; <span> </span> // The last time the file was modified: _ fsize_t size; <span> </span> // file size char name [_ MAX_FNAME]; <span> </span> // file name string };
Unsigned atrrib is the storage location of the file attribute. It stores an unsigned unit to indicate the attributes of a file. File attributes are represented by BITs, mainly including the following:
_ A_ARCH (archive ),
_ A_HIDDEN (hidden ),
_ A_NORMAL (normal ),
_ A_RDONLY (read-only ),
_ A_SUBDIR (folder ),
_ A_SYSTEM (system ).
These are all in the <io. h> the macro defined in can be used directly, and its meaning is actually an unsigned integer (but this integer should be several power of 2, so that only one digit is 1, and the other digit is 0 ). Since it is a bit representation, when a file has multiple attributes, it often obtains the synthesis of several attributes by bit or. For example, the read-only + hide + system attribute should be: _ A_HIDDEN | _ A_RDONLY | _ A_SYSTEM.
This struct is used to store file information. How can we store the file information of a hard disk file in the memory space indicated by this struct? This depends on the combination of the _ findfirst, _ findnext, and _ fineclose functions.
Long _ findfirst (char * filespec, struct _ finddata_t * fileinfo);/* return value: if the search is successful, returns a long-type unique query handle (that is, a unique number) * // * this handle will be used in the _ findnext function. If it fails,-1 */is returned */
Parameters:
Filespec: Specifies the file string. Wildcards are supported. For example, *. c indicates all files with the suffix C in the current folder.
Fileinfo: the pointer to the struct used to store file information. This struct must be declared before calling this function, but it does not need to be initialized, as long as the memory space is allocated. After the function is successful, the function puts the information of the found file into this struct.
Int _ findnext (long handle, struct _ finddata_t * fileinfo);/* return value: If the return value is successful, 0 is returned; otherwise,-1 */is returned */
Parameters:
Handle: The handle returned by the _ findfirst function.
Fileinfo: pointer to the object information structure. After finding the file, the function puts the file information in this struct.
Int _ findclose (long handle);/* return value: 0 for success,-1 for failure */
Parameters:
Handle: handle returned by the _ findfirst function.
To sum up, the general process is: first use _ findfirst to find the first file. If it succeeds, use the returned handle to call the _ findnext function to find other files. It will be used after the search is complete, use the _ findclose function to end the search. The following describes how to compile a program to find all the executable files of exe in the C: \ WINDOWS folder.
# Include <stdio. h> # include <io. h> const char * to_search = "C: \ WINDOWS \\*. exe "; // the file to be searched, supporting the wildcard int main () {long handle; // the query handle struct _ finddata_t fileinfo; // The structure of the file information: handle = _ findfirst (to_search, & fileinfo); // if (-1 = handle) return-1; printf ("% s \ n", fileinfo. name); // print the file name found while (! _ Findnext (handle, & fileinfo) // search for other conforming files cyclically and find other matching files until {printf ("% s \ n", fileinfo. name);} _ findclose (handle); // do not forget to close the handle system ("pause"); return 0 ;}
Well, the most important thing has been solved. The following is code writing, debugging, and the final program is directly pasted without a table:
# Include <iostream> # include <fstream> # include <string> # include <vector> # include <iomanip> # include <io. h> using namespace std; class Comic {public: // constructor Comic () = default; Comic (const string & newPath): sourcePath (newPath) {}// value assignment function void setPath (const string & newPath) {sourcePath = newPath;} // output function void outHtml (void); private: // source path string sourcePath; // all file names vector <string> allFilesName; // obtain all file names in the directory voi D getFiles (const string & path, vector <string> & files) ;}; void Comic: getFiles (const string & path, vector <string> & files) {// file handle long hFile = 0; // File Information struct _ finddata_t fileinfo; string pathName, s; if (hFile = _ findfirst (pathName. assign (path ). append ("\\*"). c_str (), & fileinfo ))! =-1) {do {// if there are still folders in the folder, iterate // if not, add the list if (fileinfo. attrib & _ A_SUBDIR) {if (strcmp (fileinfo. name ,". ")! = 0 & strcmp (fileinfo. name ,"..")! = 0) getFiles (pathName. assign (path ). append ("\\"). append (fileinfo. name), files);} else {if (strcmp (fileinfo. name ,". ")! = 0 & strcmp (fileinfo. name ,"..")! = 0) files. push_back (s. assign (fileinfo. name) ;}}while (_ findnext (hFile, & fileinfo) = 0); _ findclose (hFile) ;}} void Comic: outHtml (void) {getFiles (sourcePath, allFilesName); ofstream outfile; string s; outfile. open (s. assign (sourcePath ). append ("\ 0.html"); if (! Outfile. is_open () cout <"file opening error" <endl; outfile <"