From the learning C language, it has been for a long time, and I have been using it when I use it.
Stream I / O Routines also believes that these functions can be used very skilled. But today when I transplanted a MFC code, I did something that made me unexpected.
The code I have transplanted is very simple. The function is to export a resource from the resource of EXE (this resource is a zip file, and there is an operation that is decompressed) and generates files on the hard disk. The MFC code is as follows:
HRSRC Hexefile = :: FindResource (NULL, "stub.exe", "exe_resource"); ??? hglobal hres = :: loading (null, hexefile); ??? dword dwsize = :: sizeofResource (null, hexefile);
? if (hres! = null) ??? {?? uint far * lpnres = (uint far *) :: LockResource (hres);
??????? Try ??????? {?????????? cfile file (LPSZFILENAME, CFILE :: MODECREATE | CFILE :: ModeWrite);
????????????? w? User-defined resource to the .exe file ??????????? file.writehuge (lpnres, dwsize); ?????? ????? file.flush (); ????????} ??????? caratch (cfileException, e) ??????? {??} ??????? End_catch ??????? :: freeeresource (hres);
Procedures after I transplant:
FILE * EXEFILE; ??? HRSRC Hexefile = :: FindResource (NULL, "stub.exe", "exe_resource"); ??? Hglobal hres = :: loadResource (null, hexefile); ??? dword dwsize = :: SizeOfResource (null, hexefile);
? if (hres! = null) ??? {?? uint far * lpnres = (uint far *) :: LockResource (hres);
EXEFILE = FOPEN (LPSZFILENAME, "W"); ?? IF (exefile! = null)? {??? @ w = user-defined resource to the .exe file ??? fwrite (lpnres, sizeof (char ), DWSIZE, EXEFILE); ??? fflush (exefile); ??? fclose (exefile); ??} ???????? :: freeeresource (hres);
Maybe some careful friends have seen the mistakes in the transplantation process, but I didn't notice at the beginning. The result is that when the procedure after debugging during noon, the results found that the operation results were different from the procedure of the MFC. The error occurred, and the file found that the generated file was not consistent with the original file, and could not run. So I re-put the attention to this paragraph, I have seen it simply. But where is wrong? I put the attention on fwrite, my debug returned the return value, found that the return value is not consistent with DWSize, which is strange, and actually written than DWSIZE, this shows that in the process of writing There is a problem, and I suddenly realized that I made a very low-level mistake. ? exefile = fopen (LPSZFILENAME, "W"); is this, Fopen is written in the case of Default Mode "T", this is the case, must have some characters to be turned during fwrite Righteous. So I changed it into Exefile = Fopen (LPSZFILENAME, "WB"); compiled, running, normal.
In fact, this is just a simple, and it's usually hard, but I still have trouble, I think this may have a habit of programming with me, the programming method is not good, try thinking if I use FOpen every time When you are very clear, it doesn't show this problem today if you use this way to open a file. Here I also take this little problem to remind all the programmers who have seen this article, regardless of the program, the structure is simple, and it is necessary to develop a good program habit. This will make our work more than half.
?