0
点赞
收藏
分享

微信扫一扫

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

C语言函数大全

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

1. _waccess

1.1 函数说明

函数声明 函数功能
int _waccess(const wchar_t* path, int mode); 用于测试文件或目录是否存在,并检查程序是否具有对它们的访问权限

参数:

  • path : 待测试的文件名或目录名的宽字符字符串
  • mode : 一个位掩码,用于指示要测试的访问权限;常见的访问权限如下:
    • R_OK:可读取权限
    • W_OK:可写入权限
    • X_OK:可执行权限
    • F_OK:存在性检查权限

1.2 演示示例

#include <stdio.h>
#include <io.h>

int main()
{
// 测试文件是否存在
const wchar_t* filename = Ltest.txt;
// 第二个参数 mode = 0,表示不指定任何访问权限
if (_waccess(filename, 0) != 0)
{
fprintf(stderr, File does not exist.\n);
return 1;
}

printf(File exists.\n);

return 0;
}

在上述的示例代码中,我们使用 _waccess() 函数测试文件是否存在。如果文件不存在,则输出一个错误信息并返回 1;否则输出一个成功信息并返回 0

注意: 第二个参数设置为 0,表示不指定任何访问权限。

如果要测试一个文件是否存在并且具有可读取和可写入权限,可以将第二个参数设置为 R_OK | W_OK,如下所示:

_waccess(filename, R_OK | W_OK);

1.3 运行结果

在这里插入图片描述

2. _wasctime

2.1 函数说明

函数声明 函数功能
wchar_t* _wasctime(const struct tm* timeptr); 用于将 tm 结构体类型的时间转换为字符串

参数:

  • timeptr : 要转换为字符串的 tm 结构体类型变量

2.2 演示示例

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

int main()
{
// 获取当前时间
time_t now = time(NULL);
struct tm* ptm = localtime(

// 将时间转换为字符串
wchar_t* str = _wasctime(ptm);

// 输出结果:类似 Mon May 15 15:10:55 2023\n
wprintf(L%ls, str);

return 0;
}

在上面的示例代码中,

  • 首先,我们使用 time() 函数获取当前时间;
  • 接着,使用 localtime() 函数将其转换为本地时间;
  • 然后,使用 _wasctime() 函数将转换后的时间格式化为一个字符串;
  • 最后,输出转换后的时间字符串。

2.3 运行结果

在这里插入图片描述

3. _wasctime_s

3.1 函数说明

函数声明 函数功能
errno_t _wasctime_s(wchar_t* buffer, size_t bufsz, const struct tm* timeptr); 用于将 tm 结构体类型的时间转换为字符串

参数:

  • buffer : 指向一个 wchar_t 类型的缓冲区,该缓冲区存储函数将时间格式化后的结果
  • bufsz : 缓冲区的大小,以字节为单位
  • timeptr : 一个指向 tm 结构体的指针,其中包含要转换为字符串的时间信息

3.2 演示示例

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

int main()
{
// 获取当前时间
time_t now = time(NULL);
struct tm* ptm = localtime(

// 创建目标缓冲区
wchar_t buffer[32];

// 将时间转换为字符串
errno_t err = _wasctime_s(buffer, sizeof(buffer), ptm);

// 检查转换是否成功
if (err == 0)
{
// 输出结果:类似 Sun May 15 12:34:56 2023\n
wprintf(L%ls, buffer);
}
else
{
// 输出错误信息
fprintf(stderr, Error converting time to string. Error code: %d\n, err);
}

return 0;
}

在上面的示例代码中,

  • 首先,我们使用 time() 函数获取当前时间;
  • 接着,使用 localtime() 函数将其转换为本地时间;
  • 然后,创建一个目标缓冲区,并使用 _wasctime_s() 函数将转换后的时间格式化为一个字符串;
  • 最后,输出转换后的时间字符串。

3.3 运行结果

在这里插入图片描述

4. _wchdir

4.1 函数说明

函数声明 函数功能
int _wchdir(const wchar_t* path); 用于更改当前工作目录

参数:

  • path: 新工作目录路径的宽字符字符串

4.2 演示示例

#include <stdio.h>
#include <direct.h>

#define MAX_PATH 200

int main()
{
// 输出当前工作目录
wchar_t buffer[MAX_PATH];
_wgetcwd(buffer, MAX_PATH);
wprintf(LCurrent working directory: %ls\n, buffer);

// 更改当前工作目录
const wchar_t* new_dir = LC:\\Windows\\system32;
if (_wchdir(new_dir) == 0)
wprintf(LChanged working directory to: %ls\n, new_dir);
else
fprintf(stderr, Error changing working directory.\n);

return 0;
}

在上面的示例代码中,

  • 首先,我们使用 _wgetcwd() 函数获取当前工作目录;
  • 接着,调用 wprintf() 函数将当前工作目录输出到控制台;
  • 然后,使用 _wchdir() 函数将当前工作目录更改为 C:\Windows\system32
  • 最后,将更改的目录输出到控制台中。

4.3 运行结果

在这里插入图片描述

5. _wchmod

5.1 函数说明

函数声明 函数功能
int _wchmod(const wchar_t* path, int mode); 用于更改指定文件的访问权限

参数:

  • path : 一个指向 wchar_t 类型的字符串,表示要更改权限的文件路径
  • mode : 新的访问权限模式,可以是以下常量之一或其组合:
    • _S_IREAD:所有用户可读取文件
    • _S_IWRITE:所有用户可写入文件
    • _S_IEXEC:所有用户可执行文件
    • _S_IRUSR:文件所有者可读取文件
    • _S_IWUSR:文件所有者可写入文件
    • _S_IXUSR:文件所有者可执行文件
    • _S_IRGRP:文件组成员可读取文件
    • _S_IWGRP:文件组成员可写入文件
    • _S_IXGRP:文件组成员可执行文件
    • _S_IROTH:其他用户可读取文件
    • _S_IWOTH:其他用户可写入文件
    • _S_IXOTH:其他用户可执行文件

5.2 演示示例

#include <sys/stat.h>
#include <stdio.h>
#include <io.h>

int main()
{
// 更改文件的访问权限
const wchar_t* filename = Ltest.txt;
int result = _wchmod(filename, _S_IREAD | _S_IWRITE);

if (result) {
fprintf(stderr, Error changing file permission.\n);
return 1;
}

printf(File permission changed successfully.\n);

return 0;
}

5.3 运行结果

在这里插入图片描述

6. _wcreat

6.1 函数说明

函数声明 函数功能
int _wcreat(const wchar_t* path, int mode); 用于创建一个新文件,并返回一个文件指针

参数:

  • path : 一个指向 wchar_t 类型的字符串,表示要创建的文件路径名
  • mode : 要打开文件的方式,可以是以下常量之一或其组合:
    • _S_IREAD:所有用户可读取文件
    • _S_IWRITE:所有用户可写入文件
    • _S_IEXEC:所有用户可执行文件
    • _S_IRUSR:文件所有者可读取文件
    • _S_IWUSR:文件所有者可写入文件
    • _S_IXUSR:文件所有者可执行文件
    • _S_IRGRP:文件组成员可读取文件
    • _S_IWGRP:文件组成员可写入文件
    • _S_IXGRP:文件组成员可执行文件
    • _S_IROTH:其他用户可读取文件
    • _S_IWOTH:其他用户可写入文件
    • _S_IXOTH:其他用户可执行文件

注意: mode 参数中的这些常量可以用位运算符 | 进行组合,以指定文件的不同访问权限。

6.2 演示示例

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

int main()
{
// 打开新创建的文件
const wchar_t* filename = Lexample.txt;
int file_handle = _wcreat(filename, _S_IWRITE);

if (file_handle == -1)
{
fprintf(stderr, Error creating file.\n);
return 1;
}

// 写入数据到文件
const wchar_t* message = LHello, huazie!\n;
const size_t num_bytes = wcslen(message) * sizeof(wchar_t);
const ssize_t bytes_written = _write(file_handle, message, num_bytes);

if (bytes_written == -1)
{
fprintf(stderr, Error writing to file.\n);
return 1;
}

// 输出结果
wprintf(L%zd bytes written to file.\n, bytes_written);

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

return 0;
}

在上述的示例代码中,

  • 首先,我们使用 _wcreat() 函数创建一个名为 "example.txt" 的新文本文件,并将其存储在当前目录中;
  • 接着,定义一个宽字符指针 message,其值为"Hello, huazie!\n",同时使用了 wcslen() 函数来计算字符串的长度,并乘以 sizeof(wchar_t) 来计算实际的字节数
  • 然后,调用 _write() 函数将数据以字节形式写入先前创建的 "example.txt" 文件。根据返回值 bytes_written ,检查是否成功。如果函数执行成功,则输出写入的字节数;否则输出一个错误信息。
  • 最后,关闭文件句柄,结束程序。

注意:_write() 函数是 Windows 平台和 Linux 平台均可用的函数,但在使用时需要注意不同平台上换行符的差异。在 Windows 平台上,文本文件中的换行符是 "\r\n",而在 Linux 平台上则是 "\n"

6.3 运行结果

在这里插入图片描述

在这里插入图片描述

7. _wcserror

7.1 函数说明

函数声明 函数功能
wchar_t* _wcserror(int errnum); 用于将系统错误代码(例如通过 _wopen()_wsopen()_wexec() 等函数返回的错误代码)转换为对应的错误消息字符串

参数:

  • errnum : 要转换的错误代码。如果传递 0,则函数返回一个表示 "no error" 的字符串

7.2 演示示例

#include <fcntl.h>
#include <io.h>
#include <stdio.h>
#include <errno.h>
#include <wchar.h>

int main()
{
// 尝试打开不存在的文件
const wchar_t* filename = Lnonexistent.txt;
int file_handle = _wopen(filename, _O_RDONLY);

if (file_handle == -1)
{
// 获取最后发生的系统错误代码
const int error_code = errno;

// 将错误代码转换为错误消息字符串
const wchar_t* error_message = _wcserror(error_code);

// 输出错误消息字符串
fwprintf(stderr, LError opening file: %s\n, error_message);
return 1;
}

// 因为文件不存在,所以走不到这里
printf(File opened successfully.\n);

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

return 0;
}

在上述的示例代码中,

  • 首先,我们使用 _wopen() 函数尝试打开一个不存在的文件。
  • 接着,如果函数执行失败,则获取最后发生的系统错误代码 errno
  • 然后,使用 _wcserror() 函数将其转换为对应的错误消息字符串。
  • 最后,使用 fwprintf() 函数输出错误消息字符串并返回 1,结束程序;

7.3 运行结果

在这里插入图片描述

8. _wctime

8.1 函数说明

函数声明 函数功能
wchar_t* _wctime(const time_t* _Time); 用于将时间转换为可读格式的、宽字符字符串

参数:

  • _Time : 要转换为可读格式字符串的时间值,通常使用 time() 函数获取当前时间值

8.2 演示示例

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

int main()
{
// 获取当前时间
const time_t now = time(NULL);

// 将时间转换为可读格式字符串
wchar_t* str_time = _wctime(

// 输出当前时间的字符串
wprintf(LThe current date and time is: %s, asctime(localtime(
wprintf(L_wctime() output: %ls, str_time);

return 0;
}

8.3 运行结果

在这里插入图片描述

9. _wfullpath

9.1 函数说明

函数声明 函数功能
wchar_t *_wfullpath(wchar_t *absPath, const wchar_t *relPath, size_t maxLength); 用于将相对路径转换为绝对路径

参数:

  • absPath : 用于存储返回结果的缓冲区指针
  • relPath : 待转换的相对路径字符串
  • maxLength : 缓冲区最大长度

返回值:

  • 如果转换成功,则返回转换后的绝对路径;
  • 如果发生错误,则返回 NULL

9.2 演示示例

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

int main()
{
// 相对路径
const wchar_t* rel_path = L../test/file.txt;

// 分配缓冲区空间
wchar_t abs_path[1024];

// 将相对路径转换为绝对路径
if (_wfullpath(abs_path, rel_path, sizeof(abs_path) / sizeof(wchar_t)) != NULL)
{
wprintf(LAbsolute path: %ls\n, abs_path);
}
else
{
printf(Error converting relative path to absolute path.\n);
return 1;
}

return 0;
}

9.3 运行结果

在这里插入图片描述

10. _wmakepath

10.1 函数说明

函数声明 函数功能
void _wmakepath(wchar_t* path, const wchar_t* drive, const wchar_t* dir, const wchar_t* fname, const wchar_t* ext); 用于将文件路径的各个部分(驱动器、目录、文件名和扩展名)组合成完整路径

参数:

  • path : 指向存储生成的完整文件路径字符串的缓冲区的指针
  • drive : 指定驱动器的字符串,例如 "C:"。如果不需要指定驱动器,则将此参数设置为 NULL
  • dir : 指定目录路径的字符串,以反斜杠 (\) 结尾。可以使用正斜杠 (/) 作为替代字符。如果不需要指定目录路径,则将此参数设置为 NULL
  • fname : 指定文件名的字符串(不包括扩展名)。如果不需要指定文件名,则将此参数设置为 NULL
  • ext : 指定文件扩展名的字符串,包括点 (.)。如果不需要指定文件扩展名,则将此参数设置为 NULL

10.2 演示示例

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

#define MAX_PATH 300

int main()
{
// 定义文件路径各部分
const wchar_t* drive = LE:;
const wchar_t* dir = L\\Software\\C\\Demo\\C;
const wchar_t* fname = LTest2;
const wchar_t* ext = L.exe;

// 分配缓冲区空间并组合路径
wchar_t path[MAX_PATH];
// 将文件路径的各个部分(驱动器、目录、文件名和扩展名)组合成完整路径
_wmakepath(path, drive, dir, fname, ext);

// 输组合后的路径
wprintf(LPath: %ls\n, path);

return 0;
}

10.3 运行结果

在这里插入图片描述

举报

相关推荐

0 条评论