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

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

时间:2023-05-19 22:02:20浏览次数:36  
标签:函数 -- C语言 locale int str wchar include

C语言函数大全

本篇介绍C语言函数大全-- _w 开头的函数

1. _wstrtime

1.1 函数说明

函数声明 函数功能
wchar_t *_wstrtime(wchar_t *buffer); 用于获取当前系统时间并返回一个宽字符字符串表示,格式为 "HH:MM:SS"(小时:分钟:秒)

参数:

  • buffer : 一个指向 wchar_t 类型数组的指针,用于存储表示当前系统时间的宽字符字符串。如果该参数为 NULL,则 _wstrtime 函数会使用静态内部缓冲区来存储返回的时间字符串。

注意: 在多线程环境下,不应使用静态内部缓冲区,而应将 buffer 参数传递给函数以避免竞争条件。

1.2 演示示例

#include <stdio.h>
#include <time.h>

int main() 
{
    // 存储时间字符串的缓冲区
    wchar_t time_buffer[9];   
    // 获取当前时间并存储到缓冲区 
    _wstrtime(time_buffer);    
    // 打印时间字符串到控制台
    wprintf(L"The current time is %ls.\n", time_buffer);    
    return 0;
}

注意: 在不同平台上,本地化设置可能会影响 _wstrtime() 函数的输出格式

1.3 运行结果

在这里插入图片描述

2. _wstrtime_s

2.1 函数说明

函数声明 函数功能
errno_t _wstrtime_s(wchar_t *buffer, size_t sizeInWords); 用于获取当前系统时间并返回一个宽字符字符串表示,格式为 "HH:MM:SS"(小时:分钟:秒)。相比于 _wstrtime 函数,_wstrtime_s 函数增加了一个额外的参数,用于指定缓冲区的大小,在多线程环境下更加安全可靠

参数:

  • buffer : 一个指向 wchar_t 类型数组的指针,用于存储表示当前系统时间的宽字符字符串。如果该参数为 NULL,则 _wstrtime_s() 函数会使用静态内部缓冲区来存储返回的时间字符串。
  • sizeInWords : 指定了 buffer 缓冲区的大小,以 wchar_t 单位计算。如果 buffer 参数不为 NULL,则应将该参数设置为 buffer 的大小,以确保在写入时间字符串时不会发生缓冲区溢出。

2.2 演示示例

#include <stdio.h>
#include <time.h>

int main() 
{
    // 存储时间字符串的缓冲区
    wchar_t time_buffer[9];    
    // 获取当前时间并存储到缓冲区
    errno_t err = _wstrtime_s(time_buffer, sizeof(time_buffer)/sizeof(wchar_t));   
    
    if (err == 0) // 成功获取当前时间
    {
        // 打印时间字符串到控制台
        wprintf(L"The current time is %ls.\n", time_buffer);    
    } 
    else // 获取失败
    {
        fprintf(stderr, "Error getting time: %d\n", err);
    }
    return 0;
}

2.3 运行结果

在这里插入图片描述

3. _wsetlocale

3.1 函数说明

函数声明 函数功能
wchar_t* _wsetlocale(int category, const wchar_t* locale); 用于设置当前线程的本地化环境

参数:

  • category : 要设置的本地化类别,可以是如下值之一:
    • LC_ALL :设置所有本地化类别
    • LC_COLLATE :设置字符串比较规则类别
    • LC_CTYPE :设置字符分类和转换规则类别
    • LC_MONETARY :设置货币格式类别
    • LC_NUMERIC :设置数字格式类别
    • LC_TIME :设置日期和时间格式类别
  • locale : 一个指向以 null 结尾的宽字符字符串的指针,用于指定要使用的本地化信息。例如,"zh-CN" 可以指定为中国大陆地区的本地化环境。如果该参数为 NULL,则函数会根据系统默认设置来进行本地化。

返回值:

  • 如果设置成功,则返回一个指向以 null 结尾的宽字符字符串的指针,表示当前设置的本地化环境;
  • 如果设置失败,则返回 NULL

3.2 演示示例

#include <stdio.h>
#include <wchar.h>
#include <locale.h>
#include <time.h>

int main() {
    // 设置本地化环境为系统默认设置
    _wsetlocale(LC_ALL, L"");    
    // 获取当前时间
    time_t now = time(NULL);  
    // 转换为本地时间  
    struct tm* tm_now = localtime(&now);   
    // 存储时间字符串的缓冲区 
    wchar_t time_buffer[64];  
    // 格式化时间字符串  
    wcsftime(time_buffer, sizeof(time_buffer)/sizeof(wchar_t), L"%c", tm_now); 
    // 打印时间字符串到控制台   
    wprintf(L"The current date and time is: %ls.\n", time_buffer);    
    
    return 0;
}

在上面的示例代码中,

  • 首先,我们调用 _wsetlocale() 函数将本地化环境设置为系统默认设置;
  • 接着,使用 time() 函数获取当前时间 now
  • 然后,再使用 localtime() 函数将 now 转换为本地时间;
  • 再接着,定义一个 wchar_t 类型的数组作为存储格式化时间字符串的缓冲区;
  • 再然后,使用 wcsftime() 函数将当前日期和时间格式化为指定的宽字符格式 %c,并将结果存储到缓冲区中;
  • 最后,使用 wprintf() 函数打印格式化后的时间字符串到控制台。

3.3 运行结果

在这里插入图片描述

4. _wtmpnam

4.1 函数说明

函数声明 函数功能
wchar_t *_wtmpnam(wchar_t *s);

参数:

  • s : 一个指向 wchar_t 类型字符数组的指针 s,用于存储生成的唯一文件名。该数组必须至少具有 L_tmpnam 个元素

返回值:

  • 如果成功生成临时文件时,则返回指向生成的唯一文件名的 wchar_t 类型的指针;
  • 如果出现错误,则返回 NULL

4.2 演示示例

#include <stdio.h>
#include <stdlib.h>
#include <wchar.h>

#define MAX_PATH 300

int main()
{
    wchar_t filename[L_tmpnam];
    wchar_t *fullpath;
    FILE *fp;

    // 生成唯一的临时文件名
    if (_wtmpnam(filename) == NULL)
    {
        fwprintf(stderr, L"Failed to generate temp file name\n");
        return 1;
    }

    // 获取完整的文件路径
    fullpath = _wfullpath(NULL, filename, MAX_PATH);
    if (fullpath == NULL)
    {
        fwprintf(stderr, L"Failed to get full path for temp file\n");
        return 1;
    }

    wprintf(L"Temp file path: %ls\n", fullpath);

    // 打开文件进行写操作
    fp = _wfopen(filename, L"w");
    if (fp == NULL)
    {
        fwprintf(stderr, L"Failed to open temp file for writing\n");
        return 1;
    }

    // 写入一些数据
    fwprintf(fp, L"This is some sample text.\n");

    // 关闭文件
    fclose(fp);

    return 0;
}

4.3 运行结果

在这里插入图片描述

在这里插入图片描述

5. _wtof

5.1 函数说明

函数声明 函数功能
double _wtof(const wchar_t *str); 将一个宽字符串转换为浮点数

参数:

  • str : 待转换的宽字符串

5.2 演示示例

#include <stdio.h>
#include <wchar.h>

int main()
{
    const wchar_t *str = L"3.14159";
    double num;

    // 将字符串转换为浮点数
    num = _wtof(str);

    wprintf(L"String: %ls\n", str);
    wprintf(L"Number: %lf\n", num);

    return 0;
}

5.3 运行结果

在这里插入图片描述

6. _wtof_l

6.1 函数说明

函数声明 函数功能
double _wtof_l(const wchar_t *str, _locale_t locale); 用于将一个宽字符串转换为浮点数,并使用不同的本地化环境

参数:

  • str : 待转换的宽字符串
  • locale : 要使用的本地化环境。如果传递 NULL 指针,则使用当前本地化环境

6.2 演示示例

#include <stdio.h>
#include <wchar.h>
#include <locale.h>

int main()
{
    const wchar_t *str = L"3.14159";
    double num;
    _locale_t locale;

    // 使用 C 本地化环境进行转换
    locale = _create_locale(LC_ALL, "C");
    num = _wtof_l(str, locale);
    _free_locale(locale);

    wprintf(L"String: %ls\n", str);
    wprintf(L"Number (using C locale): %lf\n", num);

    // 使用中国本地化环境进行转换
    locale = _create_locale(LC_NUMERIC, "zh-CN");
    num = _wtof_l(str, locale);
    _free_locale(locale);

    wprintf(L"Number (using Chinese locale): %lf\n", num);

    return 0;
}

6.3 运行结果

在这里插入图片描述

7. _wtoi

7.1 函数说明

函数声明 函数功能
int _wtoi(const wchar_t *str); 用于将一个宽字符串转换为整数

参数:

  • str : 待转换的宽字符串

7.2 演示示例

#include <stdio.h>
#include <wchar.h>

int main()
{
    const wchar_t *str = L"314159";
    int num;

    // 将字符串转换为整数
    num = _wtoi(str);

    wprintf(L"String: %ls\n", str);
    wprintf(L"Number: %d\n", num);

    return 0;
}

7.3 运行结果

在这里插入图片描述

8. _wtoi_l

8.1 函数说明

函数声明 函数功能
int _wtoi_l(const wchar_t *str, _locale_t locale); 用于将一个宽字符串转换为整数,并使用不同的本地化环境

参数:

  • str : 待转换的宽字符串
  • locale : 要使用的本地化环境。如果传递 NULL 指针,则使用当前本地化环境

8.2 演示示例

#include <stdio.h>
#include <wchar.h>
#include <locale.h>

int main()
{
    const wchar_t *str = L"314159";
    int num;
    _locale_t locale;

    // 使用 C 本地化环境进行转换
    locale = _create_locale(LC_ALL, "C");
    num = _wtoi_l(str, locale);
    _free_locale(locale);

    wprintf(L"String: %ls\n", str);
    wprintf(L"Number (using C locale): %d\n", num);

    // 使用中文本地化环境进行转换
    locale = _create_locale(LC_NUMERIC, "zh-CN");
    num = _wtoi_l(str, locale);
    _free_locale(locale);

    wprintf(L"Number (using Chinese locale): %d\n", num);

    return 0;
}

8.3 运行结果

在这里插入图片描述

9. _wtol

9.1 函数说明

函数声明 函数功能
long _wtol(const wchar_t *str); 用于将一个宽字符串转换为长整形

参数:

  • str : 待转换的宽字符串

9.2 演示示例

#include <stdio.h>
#include <wchar.h>

int main()
{
    const wchar_t *str = L"1234567890";
    long num;

    // 将字符串转换为长整数
    num = _wtol(str);

    wprintf(L"String: %ls\n", str);
    wprintf(L"Number: %ld\n", num);

    return 0;
}

9.3 运行结果

在这里插入图片描述

10. _wtol_l

10.1 函数说明

函数声明 函数功能
long _wtol_l(const wchar_t *str, _locale_t locale); 用于将一个宽字符串转换为长整形,并使用不同的本地化环境

参数:

  • str : 待转换的宽字符串
  • locale : 要使用的本地化环境。如果传递 NULL 指针,则使用当前本地化环境

10.2 演示示例

#include <stdio.h>
#include <wchar.h>
#include <locale.h>

int main()
{
    const wchar_t *str = L"1234567890";
    long num;
    _locale_t locale;

    // 使用 C 本地化环境进行转换
    locale = _create_locale(LC_ALL, "C");
    num = _wtol_l(str, locale);
    _free_locale(locale);

    wprintf(L"String: %ls\n", str);
    wprintf(L"Number (using C locale): %ld\n", num);

    // 使用中文本地化环境进行转换
    locale = _create_locale(LC_NUMERIC, "zh-CN");
    num = _wtol_l(str, locale);
    _free_locale(locale);

    wprintf(L"Number (using Chinese locale): %ld\n", num);

    return 0;
}

10.3 运行结果

在这里插入图片描述

11. _wsopen

11.1 函数说明

函数声明 函数功能
int _wsopen(const wchar_t *filename, int oflag, int shflag, int pmode); 用于打开指定文件

参数:

  • filename : 要打开的文件名
  • oflag : 打开文件的方式,可以是如下值之一或组合使用:
    • _O_RDONLY :以只读方式打开文件
    • _O_WRONLY :以只写方式打开文件
    • _O_RDWR :以读写方式打开文件
    • _O_CREAT :如果文件不存在,则创建该文件
    • _O_TRUNC :如果文件已存在,则截断该文件
    • _O_EXCL :与 _O_CREAT 配合使用,确保创建的文件是原来不存在的
  • shflag : 共享方式,仅在 oflag 中包括 _O_CREAT 标志时有效。可以是如下值之一:
    • _SH_DENYRW :拒绝其他程序读取或写入打开的文件
    • _SH_DENYWR :拒绝其他程序写入打开的文件
    • _SH_DENYRD :拒绝其他程序读取打开的文
    • _SH_SHARE_DENYRW :允许其他程序只读打开打开的文件
    • _SH_SHARE_DENYWR :允许其他程序只写打开的文件
    • _SH_SHARE_DENYRD :允许其他程序读取打开的文件
  • pmode : 权限模式,仅在 oflag 中包括 _O_CREAT 标志时有效。该参数指定新文件访问权限的位掩码

11.2 演示示例

#include <sys/stat.h>
#include <stdio.h>
#include <wchar.h>
#include <fcntl.h>
#include <share.h>
#include <io.h>

int main()
{
    const wchar_t *filename = L"test.txt";
    int file;

    // 打开文件以进行写入
    file = _wsopen(filename, _O_WRONLY | _O_CREAT | _O_TRUNC, _SH_DENYRW, _S_IREAD | _S_IWRITE);
    if (file == -1) 
    {
        wprintf(L"Unable to open the file.\n");
        return 1;
    }

    // 写入数据到文件
    const wchar_t *data = L"Hello, huazie!";
    int len = wcslen(data);
    int written = _write(file, data, len * sizeof(wchar_t));
    if (written != len * sizeof(wchar_t)) 
    {
        wprintf(L"Error writing to the file.\n");
        return 1;
    }

    // 关闭文件句柄
    _close(file);

    wprintf(L"Data written to the file successfully!\n");

    return 0;
}

11.3 运行结果

在这里插入图片描述

在这里插入图片描述

12. _wsopen_s

12.1 函数说明

函数声明 函数功能
errno_t _wsopen_s(int *pfh, const wchar_t *filename, int oflag, int shflag, int pmode); 打开指定文件的安全版本,它避免了一些漏洞和错误,提高了程序的安全性

参数:

  • pfh : 存储打开文件的句柄
  • filename : 要打开的文件名或路径
  • oflag : 打开文件的方式和访问模式,可以使用 _O_RDONLY_O_WRONLY_O_RDWR_O_CREAT_O_TRUNC 等常量进行组合
  • shflag : 共享模式,可以使用 _SH_DENYRW_SH_DENYWR_SH_DENYRD 等常量进行组合
  • pmode : 权限标志,例如 _S_IREAD_S_IWRITE

12.2 演示示例

#include <sys/stat.h>
#include <stdio.h>
#include <wchar.h>
#include <fcntl.h>
#include <share.h>
#include <io.h>

int main()
{
    int fileHandle;
    errno_t err = _wsopen_s(&fileHandle, L"example.txt", _O_RDWR | _O_CREAT, _SH_DENYRW, _S_IREAD | _S_IWRITE);

    if (err != 0) 
    {
        printf("Failed to open file.\n");
        return err;
    }

    char buffer[20] = "Hello, huazie!";
    if (_write(fileHandle, buffer, sizeof(buffer)) == -1) 
    {
        printf("Failed to write to file.\n");
    }

    if (_close(fileHandle) == -1) 
    {
        printf("Failed to close file.\n");
    }

    return 0;
}

标签:函数,--,C语言,locale,int,str,wchar,include
From: https://blog.51cto.com/huazie/6315329

相关文章

  • Mybatis框架1
    XML配置:MyBatis的配置文件包含了会深深影响Mybatis行为的设置和属性信息。配置文档的顶层结构如下:configuration(配置)properties(属性)settings(设置)typeAliases(类型别名)typeHandlers(类型处理器)objectFactory(对象工厂)plugins(插件)enviroment(环境配置)environment(环境变量)transactionMa......
  • springboot异常处理
    在SpringBoot中,我们可以使用@ControllerAdvice和@ExceptionHandler来处理系统错误异常。下面是一个简单的例子:@ControllerAdvicepublicclassGlobalExceptionHandler{@ExceptionHandler(Exception.class)publicResponseEntity<String>handleException(Exceptione......
  • 与众不同的夜间开关交互效果
    这个夜间模式切换开关效果是不是很炫酷,在短视频曾刷到过是一个国外的设计师看似是为了难为我们前端开发设计了一个元素超多且动画复杂的开关切换效果。结果在逛codepen的时候发现真的被一个大佬给做出来了,效果真的很不错,而且还在原来的基础上增加了额外的元素,本文将逐步解析大......
  • 网络数据库安全概述1
    一.概述   20世纪70年代初,美国军方率先发起对多级安全数据库管理系统(MultilevelSecureDatabaseManagementSystem,MLSDBMS)的研究,此后提出了一系列的数据库安全模型。  20世纪80年代,美国国防部根据军用计算机系统安全需要,制定了《可信计算机系统安全评估标准》(Trus......
  • Autodesk Maya 2024 for Mac软件安装包Maya2024Mac安装教程支持M1/M2芯片
    安装步骤1,双击打开下载好的安装包。2,选择installmaya2024双击打开启动安装程序。3,点击打开。4,安装准备中...5,输入电脑密码。6,勾选同意使用条款,然后点击下一步。7,点击下一步。8,点击安装。9,软件安装中,耐心等待一会...10,安装结束点击,关闭。11,返回打开的镜像,选择补丁双击打开它。12......
  • Autodesk AutoCAD 2024 Mac软件安装包下载CAD 2024Mac安装教程
    安装步骤1,双击打开下载好的安装包。2,选择installAutodeskAutoCAD2024forMac双击打开启动安装程序。3,选择打开。4,安装准备中...5,输入电脑密码。6,勾选我同意使用条款,然后下一步。7,点击【安装】。8,软件安装中...9,安装结束关闭安装页面。10,返回到打开的镜像选择补丁双击打开。11,将......
  • PHP 基础
    <?php__FUNCTION__:获取函数名标记和注释<?phpecho'helloworld';?>/*注释*///注释语法结束符<?php?><?php变量//定义变量使用$关键字$a=1;//访问变量echo$a;//修变量$a=2;//删除变量使用unset关键字unset(变量名);变量命名规则......
  • 【NLP】Bert下载
    不区分大小写bert-base-uncased版本如果要下载其他版本点modelcard往下滑,根据需要选择对应版本比如这里下载bert-base-uncased版本,点进去后点filesandversions然后下载红框圈住的,其他的可以下也可以不下......
  • LeetCode 113. 路径总和 II
    题目链接:LeetCode113.路径总和II题意:给你二叉树的根节点root和一个整数目标和targetSum,找出所有从根节点到叶子节点路径总和等于给定目标和的路径。解题思路:与LeetCode112.路径总和相似,在遍历过程中,记录遍历过的每一个点即可。递归法递归代码:varres[][]intva......
  • 实验四
    task1.pyprint(sum)sum=42print(sum)definc(n):sum=n+1print(sum)returnsumsum=inc(7)+inc(7)print(sum) task2_1.pydeffunc1(a,b,c,d,e,f):return[a,b,c,d,e,f]deffunc2(a,b,c,*,d,e,f):return[a,b,c,d,e,f]deffunc3(a,b,c......