C语言函数大全
本篇介绍C语言函数大全-- _w 开头的函数
1. _wmkdir
1.1 函数说明
| 函数声明 | 函数功能 | 
|---|---|
| int _wmkdir(const wchar_t* dirname); | 用于创建指定路径名的新目录 | 
参数:
- dirname :  指向以 null结尾的宽字符数组,该数组包含要创建的目录的路径
1.2 演示示例
#include <stdio.h>
#include <wchar.h>
#include <direct.h>
int main()
{
    // 要创建的目录路径(在 Windows 上需要使用反斜杠)
    const wchar_t* dirname = L"E:\\Software\\C\\Demo\\C\\tmp\\test";
    // 创建目录
    int result = _wmkdir(dirname);
    if (result == 0)
    {
        wprintf(L"Directory \"%ls\" created successfully\n", dirname);
    }
    else
    {
        wprintf(L"Directory \"%ls\" creation failed\n", dirname);
    }
    return 0;
}
使用 _wmkdir() 函数时需要注意以下几点:
- 目录路径必须以 null结尾,并且必须是宽字符串(即wchar_t类型的字符串);
- 在 Windows上,目录路径中应该使用反斜杠 (\) 而不是正斜杠 (/);
- 如果目录已经存在,则 _wmkdir()函数会失败并返回-1;
- 调用 _wmkdir()函数需要适当的权限,以便在指定的位置创建新目录;
- 当程序执行完毕后,应该记得关闭打开的文件句柄,释放内存等资源。
1.3 运行结果


2. _wmktemp
2.1 函数说明
| 函数声明 | 函数功能 | 
|---|---|
| wchar_t* _wmktemp(wchar_t* template); | 用于生成唯一的临时文件名 | 
参数:
- template :  指向以 null 结尾的宽字符数组,该数组包含一个文件名模板。模板必须由 6个或更多的X字符组成,并且必须以文件名扩展名结尾
2.2 演示示例
#include <stdio.h>
#include <wchar.h>
#include <io.h>
int main()
{
    // 声明并初始化文件名模板
    wchar_t template_file[] = L"tmp\\newtempfile-XXXXXX";
    // 通过在文件名模板中插入随机字符来创建唯一的临时文件名
    if (_wmktemp(template_file) == NULL)
    {
        wprintf(L"Failed to create temporary file name !\n");
        return 1;
    }
    wprintf(L"temporary file %ls\n", template_file);
    // 打开临时文件
    FILE* fp;
    if ( _wfopen_s(&fp, template_file, L"w") != 0 )
    {
        wprintf(L"Unable to open temporary file %ls\n", template_file);
        return 1;
    }
    // 写入数据到临时文件
    fwprintf(fp, L"This is the data in temporary file %ls\n", template_file);
    // 关闭文件句柄
    fclose(fp);
    return 0;
}
2.3 运行结果


3. _wopen
3.1 函数说明
| 函数声明 | 函数功能 | 
|---|---|
| int _wopen(const wchar_t* filename, int oflag, ...); | 用于打开一个文件并返回一个文件描述符 | 
参数:
- filename :  指向以 null结尾的宽字符数组,该数组包含要打开的文件的路径和名称
- str :  打开文件时的标志,可以是以下标志之一或它们的组合:
- O_RDONLY: 以只读方式打开文件
- O_WRONLY: 以只写方式打开文件
- O_RDWR: 以读/写方式打开文件
- O_APPEND: 将数据追加到文件末尾而不是覆盖原有内容
- O_CREAT: 如果文件不存在则创建新文件
- O_TRUNC: 将文件长度截断为零
 
- ... : 可选参数,用于指定文件权限
3.2 演示示例
#include <sys/stat.h>
#include <stdio.h>
#include <fcntl.h>
#include <wchar.h>
#include <io.h>
int main()
{
    // 要打开的文件路径
    const wchar_t* filename = L"tmp\\tempfile-P1kAlM";
    // 打开文件
    int fd = _wopen(filename, _O_CREAT | _O_WRONLY | _O_TRUNC, _S_IWRITE);
    if (fd == -1)
    {
        wprintf(L"Unable to open file %ls\n", filename);
        return 1;
    }
    // 向文件中写入数据
    const wchar_t* data = L"This is the data to be written to the file";
    int result = _write(fd, data, wcslen(data) * sizeof(wchar_t));
    if (result == -1)
    {
        wprintf(L"Unable to write to file %ls\n", filename);
        return 1;
    }
    // 关闭文件句柄
    _close(fd);
    return 0;
}
注意: 如果文件不存在,则在打开文件时使用 _O_CREAT 标志可以创建新文件。
3.3 运行结果

4. _wputenv
4.1 函数说明
| 函数声明 | 函数功能 | 
|---|---|
| int _wputenv(const wchar_t* envstring); | 用于在进程的环境中设置一个环境变量 | 
参数:
- envstring :  指向以 null结尾的宽字符数组,该数组包含要设置的环境变量和它们的值。数组格式为"VARIABLE=value"
4.2 演示示例
#include <stdio.h>
#include <wchar.h>
#include <stdlib.h>
int main()
{
    // 定义要设置的环境变量和它的值
    const wchar_t* varname = L"[Key]";
    const wchar_t* varvalue = L"Huazie";
    // 将环境变量设置为指定的值
    wchar_t envstring[256];
    swprintf(envstring, 256, L"%ls=%ls", varname, varvalue);
    int result = _wputenv(envstring);
    if (result != 0)
    {
        wprintf(L"Unable to set environment variable !\n");
        return 1;
    }
    // 获取环境变量的当前值并输出
    wchar_t* getenv_result = _wgetenv(varname);
    if (getenv_result != NULL)
    {
        wprintf(L"The value of environment variable %ls is %ls\n", varname, getenv_result);
    }
    else
    {
        wprintf(L"The environment variable %ls does not exist.\n", varname);
    }
    // 清除环境变量并退出程序
    _wputenv(L"MYVAR=");
    return 0;
}
4.3 运行结果

5. _wperror
5.1 函数说明
| 函数声明 | 函数功能 | 
|---|---|
| void _wperror(const wchar_t* message); | 用于将系统定义的错误代码转换为对应的文本消息 | 
参数:
- message : 可选参数,是一个包含自定义错误消息的宽字符字符串。如果指定了该参数,则会在默认错误消息之后输出自定义消息;否则,仅输出默认错误消息
5.2 演示示例
#include <stdio.h>
#include <wchar.h>
#include <errno.h>
int main() 
{
    FILE *file;
    errno_t err = _wfopen_s(&file, L"nonexistent.txt", L"r");
    if (err != 0) 
    {
        _wperror(L"Failed to open file");
        return 1;
    }
    fclose(file);
    return 0;
}
5.3 运行结果

6. _wpopen
6.1 函数说明
| 函数声明 | 函数功能 | 
|---|---|
| FILE *_wpopen(const wchar_t *command, const wchar_t *mode); | 用于打开一个进程并返回该进程的标准输入或输出流 | 
参数:
- command : 要执行的命令。可以是任何有效的命令行命令或可执行文件的路径
- mode : 打开流的模式。必须是 "r" 或 "w",表示读模式或写模式
返回值:
- 如果打开流成功,则返回指向打开的流(即进程的标准输入或输出)的指针;
- 如果打开流失败,则返回 NULL。
6.2 演示示例
#include <stdio.h>
#include <wchar.h>
int main() 
{
    FILE *stream;
    wchar_t command[] = L"dir /b";
    wchar_t buffer[100];
    stream = _wpopen(command, L"r");
    if (stream == NULL) 
    {
        wprintf(L"Failed to execute command\n");
        return 1;
    }
    while (fgetws(buffer, sizeof(buffer)/sizeof(wchar_t), stream)) 
    {
        wprintf(L"%ls", buffer);
    }
    _pclose(stream);
    return 0;
}
在上面的示例代码中,我们使用 _wpopen() 打开一个命令提示符窗口,并在其中执行 dir /b 命令以列出当前目录下的所有文件名。然后我们从打开的流中读取输出,并将其输出到控制台上。
注意: 在使用 _wpopen() 函数时,要格外小心输入的命令字符串。如果该字符串可以由用户输入而来,则有可能受到他人的恶意输入(如命令注入)。因此,实际使用时,必须对输入进行适当的验证和过滤。
6.3 运行结果

7. _wremove
7.1 函数说明
| 函数声明 | 函数功能 | 
|---|---|
| int _wremove(const wchar_t* filename); | 用于删除指定路径下的文件 | 
参数:
- filename : 要删除的文件的路径和名称,必须是宽字符字符串
7.2 演示示例
#include <stdio.h>
#include <wchar.h>
int main() 
{
    const wchar_t* filename = L"tmp\\tempfile-P1kAlM";
    int result = _wremove(filename);
    if (result == 0) 
    {
        wprintf(L"%ls was successfully deleted.\n", filename);
    } 
    else 
    {
        wprintf(L"Failed to delete %ls. Error code: %d\n", filename, result);
    }
    return 0;
}
注意: 由于_wremove() 函数是直接从磁盘上删除文件,因此务必小心谨慎使用。在删除文件之前,请确保已经备份了需要保存的重要数据
7.3 运行结果

8. _wrename
8.1 函数说明
| 函数声明 | 函数功能 | 
|---|---|
| int _wrename(const wchar_t* oldname, const wchar_t* newname); | 用于重命名指定路径下的文件或目录 | 
参数:
- oldname : 要重命名的现有文件或目录的路径和名称,必须是宽字符字符串
- newname : 新文件或目录的路径和名称,必须是宽字符字符串
8.2 演示示例
#include <stdio.h>
#include <wchar.h>
int main() 
{
    const wchar_t* oldname = L"file1.txt";
    const wchar_t* newname = L"file2.txt";
    int result = _wrename(oldname, newname);
    if (result == 0) 
    {
        wprintf(L"%ls was successfully renamed to %ls.\n", oldname, newname);
    } 
    else 
    {
        wprintf(L"Failed to rename %ls. Error code: %d\n", oldname, result);
    }
    return 0;
}
8.3 运行结果

9. _wrmdir
9.1 函数说明
| 函数声明 | 函数功能 | 
|---|---|
| int _wrmdir(const wchar_t* dirname); | 用于删除指定路径下的目录 | 
参数:
- dirname : 要删除的目录的路径和名称,必须是宽字符字符串
9.2 演示示例
#include <stdio.h>
#include <wchar.h>
int main() 
{
    const wchar_t* dirname = L"tmp\\newdir";
    int result = _wrmdir(dirname);
    if (result == 0) 
    {
        wprintf(L"%ls was successfully deleted.\n", dirname);
    } 
    else 
    {
        wprintf(L"Failed to delete %ls. Error code: %d\n", dirname, result);
    }
    return 0;
}
9.3 运行结果

10. _wstrdate
10.1 函数说明
| 函数声明 | 函数功能 | 
|---|---|
| wchar_t* _wstrdate(wchar_t* date); | 用于获取当前日期并将其格式化为字符串 | 
参数:
- date :  指向一个缓冲区的指针,用于存储格式化后的日期。缓冲区大小必须足够容纳该日期和一个空字符('\0')
10.2 演示示例
#include <stdio.h>
#include <wchar.h>
int main() 
{
    wchar_t buffer[32];
    _wstrdate(buffer);
    wprintf(L"Current date is %ls\n", buffer);
    return 0;
}
10.3 运行结果

11. _wstrdate_s
11.1 函数说明
| 函数声明 | 函数功能 | 
|---|---|
| errno_t _wstrdate_s(wchar_t* buffer, size_t numberOfElements); | 用于获取当前日期并将其格式化为字符串。与 _wstrdate()不同的是,它可以在编译时指定缓冲区的大小和字符集,以提高安全性。 | 
参数:
- buffer : 指向一个缓冲区的指针,用于存储格式化后的日期。
- numberOfElements : 缓冲区中元素的数量(即缓冲区的长度)
返回值:
- 如果操作成功,则返回零;
- 否则,返回一个表示错误的代码
11.2 演示示例
#include <stdio.h>
#include <wchar.h>
int main() 
{
    wchar_t buffer[32];
    errno_t error = _wstrdate_s(buffer, sizeof(buffer)/sizeof(buffer[0]));
    if (error == 0) 
    {
        wprintf(L"Current date is %ls\n", buffer);
    } 
    else 
    {
        wprintf(L"Failed to get current date. Error code: %d\n", error);
    }
    return 0;
}
注意: 如果缓冲区的大小不足以容纳格式化后的日期字符串,则该函数会返回一个错误码表示缓冲区溢出(ERANGE)。在使用该函数时,请确保缓冲区的大小足够大以容纳日期字符串和一个空字符(L'\0')
11.3 运行结果











