windows编程,使用文件和目录,CreateFile, WriteFile, CreateDirectory

本文介绍了如何在Windows环境下进行文件和目录操作,包括使用CreateDirectory创建Dir_1和Dir_2目录,用CreateFile创建并覆盖test.bin文件,使用WriteFile写入DWORD数组和字符串,通过GetFileSize获取文件大小,最后使用CopyFile复制文件到另一个目录。示例代码基于Visual Studio 2013。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

文章内容

之前写的一点windows下编程的内容,每一篇都会设立几个目标(题目来自国外某大学),并写一个可运行的实例出来用于熟悉各种API,并把相关的API的官方MSDN文档附上,大伙可自行参考学习相关API用法

目标

原版

You should implement a function, which performs the following actions:

  1. Create two new directories in the current working directory named Dir_1 and Dir_2 (CreateDirectory).
  2. Create an array of 10 elements of DWORD type. Fill in the array as follows: the value of each i-th element is equal to i*i.
  3. In the Dir_1 directory, create a file named test.bin (CreateFile). When launching the application, if a file with the same name already exists, then it must be overwritten (CREATE_ALWAYS).
  4. Write an array of 10 elements of the DWORD type (WriteFile) created in step 2 to the file.
  5. Write to the end of the file a string of TCHAR elements with the following content:
    TCHAR str[] = TEXT(“Hello, World!”);
    To calculate the length of a string in characters, use the _tcslen function from the <tchar.h> header file.
  6. Get the size of the test.bin file (GetFileSize) and display it on the screen.
  7. Close the handle of the test.bin file (CloseHandle).
  8. Copy the test.bin file from the Dir_1 directory to the Dir_2 directory (CopyFile). If the file is named test.bin already exists in the Dir_2, then it must be overwritten.

翻译版本

  1. 在当前工作目录中创建两个名为Dir_1和Dir_2的新目录(CreateDirectory)。
  2. 创建一个包含10个DWORD类型元素的数组。按如下方式填充数组:每个第i个元素的值等于i*i。
  3. 在Dir_1目录中,创建名为test.bin(CreateFile)的文件。启动应用程序时,如果已存在同名文件,则必须覆盖该文件(CREATE_ALWAYS)。
  4. 将步骤2中创建的DWORD类型(WriteFile)的10个元素数组写入文件。
  5. 在文件末尾写入一个TCHAR元素字符串,内容如下:
    TCHAR str[] = TEXT(“Hello, World!”);
    要计算字符串的字符长度,请使用<tchar.h>头文件中的_tcslen函数。
  6. 获取test.bin文件的大小(GetFileSize)并将其显示在屏幕上。
  7. 关闭test.bin文件的句(CloseHandle)。
  8. 将test.bin文件从Dir_1目录复制到Dir_2目录(CopyFile)。如果名为test.bin的文件已存在于Dir_2中,则必须覆盖该文件。

编译器

visual studio2013 ,不同版本有细微差异,不过都是小修即可完整运行

代码

#include<iostream>
#include<windows.h>
#include<tchar.h>

using namespace std;

void task_two_part_one() {
    CreateDirectoryA(".\\Dir_1", nullptr);
    CreateDirectoryA(".\\Dir_2", nullptr);
    DWORD p[10];
    for (int i = 0; i < 10; ++i) {
        p[i] = (i + 1) * (i + 1);
    }
    HANDLE f = CreateFileA(".\\Dir_1\\test.bin", GENERIC_READ | GENERIC_WRITE, 0, nullptr, CREATE_ALWAYS,
                           FILE_ATTRIBUTE_NORMAL,
                           nullptr);
    WriteFile(f, p, 10 * sizeof(DWORD), 0, nullptr);
    TCHAR str[] = TEXT("Hello,World!");
    WriteFile(f, str, _tcslen(str), 0, nullptr);
    cout << GetFileSize(f, nullptr) << endl;
    CloseHandle(f);
    CopyFileA(".\\Dir_1\\test.bin", ".\\Dir_2\\test.bin", FALSE);
};

int main() {
    task_two_part_one();
}

解释

CreateDirectoryA
CreateFileA
WriteFile
_tcslen
GetFileSize
CloseHandle
CopyFile

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值