VC 创建文件

本文介绍了一段使用C++创建文件并写入数据的基本代码示例,包括使用GetWindowsDirectory函数获取系统目录,使用wsprintf和strcat进行字符串拼接,以及使用CreateFile和WriteFile进行文件操作。
 
 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 }

 

 

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的指针。

 

转载于:https://www.cnblogs.com/cody1988/archive/2012/05/13/2498207.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值