LPTSTR 问题
Example: A Simple Sequential File Copy
The following sections show short example programs implementing a simple sequential file copy program in three different ways:Using the Standard C libraryUsing WindowsUsing a single Windows convenience function, CopyFileIn addition to showing contrasting programming models, these examples show the capabilities and limitations of the C library and Windows. Alternative implementations will enhance the program to improve performance and increase flexibility.Sequential file processing is the simplest, most common, and most essential capability of any file system, and nearly any large program processes at least some files sequentially. Therefore, a simple file processing program is a good way to introduce Windows and its conventions.File copying, often with updating, and the merging of sorted files are common forms of sequential processing. Compilers and text processing tools are examples of other applications that access files sequentially.Although sequential file processing is conceptually simple, efficient processing that attains optimal speed can be much more difficult to achieve. It can require overlapped I/O, memory mapping, threads, or other techniques.Simple file copying is not very interesting by itself, but comparing programs gives us a quick way to contrast different systems and to introduce Windows. The following examples implement a limited version of the UNIX cp command, copying one file to another, where the file names are specified on the command line. Error checking is minimal, and existing files are simply overwritten. Subsequent Windows implementations of this and other programs will address these and other shortcomings. Note: A UNIX implementation is included on the book's Web site.File Copying with the Standard C Library
As illustrated in Program 1-1, the Standard C library supports stream FILE I/O objects that are similar to, although not as general as, the Windows HANDLE objects shown in Program 1-2.Program 1-1. cpC: File Copying with the C Library
/*   Chapter 1. Basic cp file copy program.
     C library Implementation. */
/*   cp file1 file2: Copy file1 to file2. */#include <stdio.h>
#include <errno.h>
#define BUF_SIZE 256int main (int argc, char *argv [])
{
   FILE *in_file, *out_file;
   char rec [BUF_SIZE];
   size_t bytes_in, bytes_out;
   if (argc != 3) {
      printf ("Usage: cpC file1 file2\n");
      return 1;
   }
   in_file = fopen (argv [1], "rb");
   if (in_file == NULL) {
      perror (argv [1]);
      return 2;
   }
   out_file = fopen (argv [2], "wb");
   if (out_file == NULL) {
      perror (argv [2]);
      return 3;
   }   /*   Process the input file a record at a time. */
   while ((bytes_in = fread (rec, 1, BUF_SIZE, in_file)) > 0) {
      bytes_out = fwrite (rec, 1, bytes_in, out_file);
      if (bytes_out != bytes_in) {
         perror ("Fatal write error.");
         return 4;
      }
   }
   fclose (in_file);
   fclose (out_file);
   return 0;
}This simple example clearly illustrates some common programming assumptions and conventions that do not always apply with Windows.Open file objects are identified by pointers to FILE structures (UNIX uses integer file descriptors). NULL indicates an invalid value. The pointers are, in effect, a form of handle to the open file object.The call to fopen specifies whether the file is to be treated as a text file or a binary file. Text files contain system-specific character sequences to indicate situations such as an end of line. On many systems, including Windows, I/O operations on a text file convert between the end-of-line character sequence and the null character that C interprets as the end of a string. In the example, both files are opened in binary mode.Errors are diagnosed with perror, which, in turn, accesses the global variable errno to obtain information about the function call failure. Alternatively, the ferror function could be used to return an error code that is associated with the FILE rather than the system.The fread and fwrite functions directly return the number of bytes processed, rather than return the value in an argument, and this arrangement is essential to the program logic. A successful read is indicated by a non-negative value, and 0 indicates an end of file.The fclose function applies only to FILE objects (a similar statement applies to UNIX file descriptors).The I/O is synchronous so that the program must wait for the I/O operation to complete before proceeding.The C library printf I/O function is useful for error messages and occurs even in the initial Windows example.The C library implementation has the advantage of portability to UNIX, Windows, and other systems that support ANSI C. Furthermore, as shown in Appendix C, C library performance for sequential I/O is competitive with alternative implementations. Nonetheless, programs are still constrained to synchronous I/O operations, although this constraint will be lifted somewhat when using Windows threads (starting in Chapter 7).C library file processing programs, like their UNIX equivalents, are able to perform random access file operations (using fseek or, in the case of text files, fsetpos and fgetpos), but that is the limit of sophistication of Standard C library file I/O. Note: Visual C++ does provide nonstandard extensions that support, for example, file locking. Finally, the C library cannot control file security.In summary, if simple synchronous file or console I/O is all that is needed, then use the C library to write portable programs that will run under Windows.File Copying with Windows
Program 1-2 shows the same program using the Windows API, and the same basic techniques, style, and conventions will be used throughout this book.Program 1-2. cpW: File Copying with Windows, First Implementation
/*   Chapter 1. Basic cp file copy program. Windows Implementation. */
/*   cpW file1 file2: Copy file1 to file2. */#include <windows.h>
#include <stdio.h>
#define BUF_SIZE 256
int main (int argc, LPTSTR argv [])
{
   HANDLE hIn, hOut;
   DWORD nIn, nOut;
   CHAR Buffer [BUF_SIZE];   if (argc != 3) {
      printf ("Usage: cpW file1 file2\n");
      return 1;
   }
   hIn = CreateFile (argv [1], GENERIC_READ, 0, NULL,
         CREATE_NEW, 0, NULL);
   if (hIn == INVALID_HANDLE_VALUE) {
      printf ("Cannot open input file. Error: %x\n",
             GetLastError ());
      return 2;
   } 
每次 建立的 新文件 都是乱码 这是什么问题呢? 每次输入参数之后, 建立的文件都是乱码 是不是 LPTSTR argv [] 的问题呢, 应该怎么改 谢谢