1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74
| #include<windows.h>
#include<stdio.h>
#define BUF_SIZE 256
int main (int argc, LPTSTR argv [])
{
HANDLE hFile;
HANDLE hTempFile;
DWORD dwBytesRead, dwBytesWritten, dwPos;
char szTempName[MAX_PATH];
char buffer[4096];
// Open the existing file.
hFile = CreateFile("original.txt", // file name
GENERIC_READ, // open for reading
0, // do not share
NULL, // default security
OPEN_EXISTING, // existing file only
FILE_ATTRIBUTE_NORMAL, // normal file
NULL); // no template
if (hFile == INVALID_HANDLE_VALUE)
{
printf("Could not open file.",GetLastError());
return 1 ;// process error
}
// Create a temporary file.
GetTempFileName("\\TEMP", // directory for temp files
"NEW", // temp file name prefix
0, // create unique name
szTempName); // buffer for name
hTempFile = CreateFile((LPTSTR)szTempName, // file name
GENERIC_READ | GENERIC_WRITE, // open for read/write
0, // do not share
NULL, // default security
CREATE_ALWAYS, // overwrite existing file
FILE_ATTRIBUTE_NORMAL, // normal file
NULL); // no template
if (hTempFile == INVALID_HANDLE_VALUE)
{
printf("Could not create temporary file.",GetLastError());
return 2 ;
}
// Read 4K blocks to the buffer.
// Change all characters in the buffer to uppercase.
// Write the buffer to the temporary file.
do
{
if (ReadFile(hFile, buffer, 4096, &dwBytesRead, NULL))
{
CharUpperBuff(buffer, dwBytesRead);
WriteFile(hTempFile, buffer, dwBytesRead, &dwBytesWritten, NULL);
}
} while (dwBytesRead == 4096);
// Close both files.
CloseHandle(hFile);
CloseHandle(hTempFile);
// Move the temporary file to the new text file.
if (!MoveFile(szTempName, "allcaps.txt"))
{
printf("Could not move temp file.",GetLastError());
return 3;
}
return 0;
} |
Partager