1 // createfile.cpp : Defines the entry point for the console application.
2 //
3
4 #include "stdafx.h"
5
6 #include "windows.h"
7 #include "string"
8 int main(int argc, char* argv[])
9 {
10
11 char path[255];
12 char fileName[255];
13 char data[] = "this is a test!";
14
15 for(int i = 0;i < 20;i ++)
16 {
17 //get windows dir
18 GetWindowsDirectory(path,sizeof(path));
19
20 wsprintf(fileName,"%d.txt",i);
21
22 strcat(path,fileName);
23 printf("%s\n",fileName);
24 printf("%s\n",path);
25 HANDLE hFile;
26 hFile = CreateFile(path,GENERIC_WRITE,0,NULL,
27 CREATE_ALWAYS,FILE_ATTRIBUTE_NORMAL,NULL);
28 if(hFile == INVALID_HANDLE_VALUE)
29 {
30 continue;
31 }
32
33 DWORD dwWrite;
34 WriteFile(hFile,&data,strlen(data),&dwWrite,NULL);
35
36 CloseHandle(hFile);
37 memset(path,0,sizeof(path));
38 memset(fileName,0,sizeof(fileName));
39
40 }
41
42 return 5;
43 }
2 //
3
4 #include "stdafx.h"
5
6 #include "windows.h"
7 #include "string"
8 int main(int argc, char* argv[])
9 {
10
11 char path[255];
12 char fileName[255];
13 char data[] = "this is a test!";
14
15 for(int i = 0;i < 20;i ++)
16 {
17 //get windows dir
18 GetWindowsDirectory(path,sizeof(path));
19
20 wsprintf(fileName,"%d.txt",i);
21
22 strcat(path,fileName);
23 printf("%s\n",fileName);
24 printf("%s\n",path);
25 HANDLE hFile;
26 hFile = CreateFile(path,GENERIC_WRITE,0,NULL,
27 CREATE_ALWAYS,FILE_ATTRIBUTE_NORMAL,NULL);
28 if(hFile == INVALID_HANDLE_VALUE)
29 {
30 continue;
31 }
32
33 DWORD dwWrite;
34 WriteFile(hFile,&data,strlen(data),&dwWrite,NULL);
35
36 CloseHandle(hFile);
37 memset(path,0,sizeof(path));
38 memset(fileName,0,sizeof(fileName));
39
40 }
41
42 return 5;
43 }
UINT GetWindowsDirectory(LPTSTR lpBuffer,UINT uSize) 获取systrm32文件夹的路径
原型
extern char *strcat(char *dest,char *src);
用法
#include <string.h>
在C++中,则存在于<cstring>头文件中。
功能
把src所指字符串添加到dest结尾处(覆盖dest结尾处的'\0')并添加'\0'。
说明
src和dest所指内存区域不可以重叠且dest必须有足够的空间来容纳src的字符串。
返回指向dest的指针。