首页 > 其他分享 >C语言函数大全-- _w 开头的函数(3)

C语言函数大全-- _w 开头的函数(3)

时间:2023-05-18 23:00:37浏览次数:38  
标签:函数 -- wprintf C语言 int ls wchar include

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() 函数时需要注意以下几点:

  1. 目录路径必须以 null 结尾,并且必须是宽字符串(即 wchar_t 类型的字符串);
  2. Windows 上,目录路径中应该使用反斜杠 (\) 而不是正斜杠 (/);
  3. 如果目录已经存在,则 _wmkdir() 函数会失败并返回 -1
  4. 调用 _wmkdir() 函数需要适当的权限,以便在指定的位置创建新目录;
  5. 当程序执行完毕后,应该记得关闭打开的文件句柄,释放内存等资源。

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 运行结果

在这里插入图片描述

标签:函数,--,wprintf,C语言,int,ls,wchar,include
From: https://blog.51cto.com/huazie/6307644

相关文章

  • Golang - go:embed
    总结GoEmbed有什么用处能够在命令行工具里嵌入WEBgoinstall快速安装,启动web该web可以提供生成代码的平台该web可以提供例如jsontostruct等数据结构转换可以大大提高Go的工具链能力能够将前端资源打包到一个二进制包里,方便部署和安装静态资源访问没有io操作,速度非常......
  • JS组合继承
    组合继承<!DOCTYPEhtml><htmllang="en"><head><metacharset="UTF-8"/><metahttp-equiv="X-UA-Compatible"content="IE=edge"/><metaname="viewport"content=&......
  • Go常见问题(一)Visual Studio Code 无法识别 go 指令
    参考链接:https://www.jianshu.com/p/e2ddc10588fc $env:Path=[System.Environment]::GetEnvironmentVariable("Path","Machine")......
  • c++ stoll
    stoll,字符串转换为longlong(114条消息)C/C++编程笔记:stol和stoll函数,函数调用中的字符串转换_c++stoll_一起学编程的博客-CSDN博客......
  • 编程一小时2023.5.18
    #include<iostream>#include<fstream>usingnamespacestd;classDog{public:intgetdogage(){returnage;}intgetdogweight(){returnweight;}voidsetdog(intx,inty){age=x;weight=y;}private:intage,weight;}; intmain(){intv1,v2;......
  • Bean注入
    传统的Spring做法是使用.xml文件来对bean进行注入,这样做既麻烦又难维护。所以Spring后来引入了注解,大大减少配置文件,增加了web代码的可读性。(一)xml配置注入bean1.创建beanpublicclassStudent{publicStudent(Stringname,intage){this.name=name;......
  • Vue 自定义指令实践
    Vue自定义指令一个自定义指令由一个包含类似组件生命周期钩子的对象来定义。钩子函数会接收到指令所绑定元素作为其参数。在 <scriptsetup> 中,任何以 v 开头的驼峰式命名的变量都可以被用作一个自定义指令。eg: 在上面的例子中,vFocus 即可以在模板中以 v-focus 的形......
  • APP中RN页面热更新流程-ReactNative源码分析
    平时使用WebStorm或VSCode对RN工程中的文件修改后,在键盘上按一下快捷cmd+s进行文件保存,此时当前调试的RN页面就会自动进行刷新,这是RN开发相比于原生开发一个很大的优点:热更新。那么,从按一下快捷cmd+s到RN页面展示出最新的JS页面,这个过程是怎样发生的呢?下面根据时间顺序来梳理一下......
  • python+playwright 学习-63 table表格定位
    前言定位table表格内容以及获取table表格数据。table表格场景网页table表格示例table页面有这几个明显的标签:table、tr、th、td<table>标示一个表格<tr>标示这个表格的一行</th>定义表头单元格</td>定义单元格标签,一组<td>标签将将建立一个单元格,<td>标签必须放......
  • GPU 相关配置和使用建议
    基本信息和配置方法显卡,驱动,CUDA,cuDNN显卡显卡又称图形处理器(graphprocessingunit,GPU),是一个硬件,主要功能是图形显示和处理,现在也是深度学习里面主流的并行计算硬件。常见的有NVIDIA的显卡(N卡)和AMD的显卡(A卡)GPU和CPU(中央处理器,CentralProcessingUnit)在设计上的主要......