函数声明 | 函数功能 |
---|---|
| 用于将无符号长整型数转换成指定基数下的字符串表示 |
| 用于将字符推回输入流中 |
| 用于将字符推回输入流中 |
| 用于将文本文件的行末标志符从 |
| 用于将将文本文件的行末标志符从 |
| 用于删除指定文件 |
| 它不是标准 |
| 用于对文件进行解锁操作 |
函数声明 | 函数功能 |
---|---|
| 用于将无符号长整型数转换成指定基数下的字符串表示 |
参数:
函数 ultoa()
将参数 value
转换为以 base
进制表示的形式,并将结果存储在缓冲区 str
中。如果转换成功,则返回指向 str
的指针。
注意: 函数 ultoa()
不会检查缓冲区是否足够大,因此调用者需要确保缓冲区足够大以避免发生缓冲区溢出。
#include <stdio.h>
#include <stdlib.h>
int main()
{
unsigned long num = 0xDEADBEEF;
char str[20];
_ultoa(num, str, 16);
printf("The hexadecimal representation of %lu is %s\n", num, str);
return 0;
}
函数声明 | 函数功能 |
---|---|
| 用于将字符推回输入流中 |
参数:
#include <stdio.h>
//int ungetc(int c, FILE *stream);
int main()
{
int c;
FILE *fp = fopen("test.txt", "r");
if (fp == NULL)
{
printf("Failed to open file\n");
return 1;
}
// 读取一个字符
c = fgetc(fp);
if (c == EOF)
{
printf("Failed to read character\n");
return 1;
}
printf("Read character: %c\n", c);
// 推回字符到输入流中
if (ungetc(c, fp) == EOF)
{
printf("Failed to unget character\n");
return 1;
}
// 再次读取字符
c = fgetc(fp);
if (c == EOF)
{
printf("Failed to read character\n");
return 1;
}
printf("Read character again: %c\n", c);
fclose(fp);
return 0;
}
在上面的示例代码中,
test.txt
的文本文件;fgetc()
函数从中读取一个字符;ungetc()
函数将该字符推回输入流中;fgetc()
函数从输入流中读取字符;printf()
函数将两次读取的字符打印到标准输出流中。注意: 在使用 ungetc()
函数推回字符之前,必须先读取一个字符并检查其是否成功读取。否则,ungetc()
函数将无法确定将字符推回哪个位置。
函数声明 | 函数功能 |
---|---|
| 用于将字符推回输入流中 |
参数:
#include <stdio.h>
#include <ctype.h>
#include <conio.h>
int main()
{
int i=0;
char ch;
puts("Input an integer followed by a char:");
while((ch = getche()) != EOF && isdigit(ch))
i = 10 * i + ch - 48;
if (ch != EOF)
ungetch(ch);
printf("\n\ni = %d, next char in buffer = %c\n", i, getch());
return 0;
}
在上述的示例代码中,
"Input an integer followed by a char:"
getche()
函数从输入流中逐个读取字符,并检查它是否是数字字符。如果是数字字符,则将其转换为整数并存储在变量 i
中。ungetch()
函数将该字符推回输入流中,以保留它供后续使用。getch()
函数从输入流中读取一个字符,并打印出读取到的下一个字符和此时 i
的值。注意: getch()
和 ungetch()
函数通常只在 Windows
平台上可用,因此这段代码可能不可移植到其他操作系统或编译器中。
函数声明 | 函数功能 |
---|---|
| 用于将文本文件的行末标志符从 |
| 用于将将文本文件的行末标志符从 |
参数:
返回值:
0
;#include <stdio.h>
#include <stdlib.h>
int unix2dos(const char *src_file, const char *dst_file);
int dos2unix(const char *src_file, const char *dst_file);
int main()
{
int status;
// 将 Unix 格式的文件转换为 DOS 格式
status = unix2dos("input_unix.txt", "output_dos.txt");
if (status != 0)
{
printf("Failed to convert file: %d\n", status);
return 1;
}
// 将 DOS 格式的文件转换为 Unix 格式
status = dos2unix("input_dos.txt", "output_unix.txt");
if (status != 0)
{
printf("Failed to convert file: %d\n", status);
return 1;
}
printf("File conversion successful\n");
return 0;
}
int unix2dos(const char *src_file, const char *dst_file)
{
FILE *in = fopen(src_file, "r");
FILE *out = fopen(dst_file, "w");
if (in == NULL || out == NULL)
return -1;
int c;
while ((c = fgetc(in)) != EOF)
{
if (c == '\n')
fputc('\r', out);
fputc(c, out);
}
fclose(in);
fclose(out);
return 0;
}
int dos2unix(const char *src_file, const char *dst_file)
{
FILE *in = fopen(src_file, "r");
FILE *out = fopen(dst_file, "w");
if (in == NULL || out == NULL)
return -1;
int c;
int prev = -1;
while ((c = fgetc(in)) != EOF)
{
if (prev == '\r' && c == '\n')
{
// skip CR character
prev = c;
continue;
}
fputc(c, out);
prev = c;
}
fclose(in);
fclose(out);
return 0;
}
函数声明 | 函数功能 |
---|---|
| 用于删除指定文件 |
参数:
#include <stdio.h>
#include <stdlib.h>
int main()
{
int status;
// 删除指定文件
status = unlink("huazie.txt");
if (status != 0)
{
printf("Failed to delete file: %d\n", status);
return 1;
}
printf("File deletion successful\n");
return 0;
}
在上面的示例代码中,我们使用 unlink() 函数删除了当前目录下名为 huazie.txt
的文件。如果 unlink()
函数返回值不为 0
,则说明删除操作失败,可能是由于权限不足、文件不存在或其他原因导致的。如果删除操作成功,则会输出一条简短的提示信息 "File deletion successful"
。
注意: 由于删除操作无法撤销,并且被删除的文件内容将无法恢复,因此在使用 unlink()
函数删除文件时需要小心谨慎,建议在执行此类敏感操作之前进行备份或确认。
函数声明 | 函数功能 |
---|---|
| 它不是标准 |
参数:
offset
参数小于零,则将从文件末尾开始向前计算偏移量。#include <io.h>
#include <fcntl.h>
#include <sys\stat.h>
#include <process.h>
#include <share.h>
#include <stdio.h>
#include <sys\locking.h>
int main(void)
{
int handle, status;
long length;
// 打开名为 test.txt 的文件
handle = sopen("test.txt",O_RDONLY,SH_DENYNO,S_IREAD);
if (handle < 0)
{
printf("sopen failed\n");
exit(1);
}
// 获取文件长度
length = filelength(handle);
// 锁定上面打开的文件
status = lock(handle,0L,length/2);
// 检查锁定操作是否成功,返回0,表示成功,返回非0,则加锁失败
if (status == 0)
printf("lock succeeded\n");
else
printf("lock failed\n");
// 对上面锁定的文件进行解除锁定
status = unlock(handle,0L,length/2);
// 检查解除锁定操作是否成功,返回0,表示成功,返回非0,则解锁失败
if (status == 0)
printf("unlock succeeded\n");
else
printf("unlock failed\n");
// 关闭文件句柄
close(handle);
return 0;
}
函数声明 | 函数功能 |
---|---|
| 用于对文件进行解锁操作 |
参数:
4GB
,因此需要使用两个参数表示完整的偏移量#include <windows.h>
#include <stdio.h>
int main() {
HANDLE file_handle;
DWORD bytes_written;
OVERLAPPED overlapped = {0};
DWORD offset = 0;
DWORD length = 0;
BOOL status;
// 打开指定文件并获取文件句柄
file_handle = CreateFile("test.txt", GENERIC_WRITE, 0, NULL, CREATE_ALWAYS,
FILE_ATTRIBUTE_NORMAL, NULL);
if (file_handle == INVALID_HANDLE_VALUE)
{
printf("Failed to open file\n");
return 1;
}
// 将字符串写入文件
const char *data = "Hello, world!";
status = WriteFile(file_handle, data, strlen(data), &bytes_written, &overlapped);
if (!status)
{
printf("Failed to write to file\n");
CloseHandle(file_handle);
return 1;
}
// 锁定文件的前半部分
length = GetFileSize(file_handle, NULL) / 2;
status = LockFile(file_handle, offset, 0, length, 0);
if (!status)
{
printf("Failed to lock file\n");
CloseHandle(file_handle);
return 1;
}
printf("File locked successfully\n");
// 解锁文件的前半部分
status = UnlockFile(file_handle, offset, 0, length, 0);
if (!status)
printf("Failed to unlock file\n");
else
printf("File unlocked successfully\n");
// 关闭文件句柄并返回
CloseHandle(file_handle);
return 0;
}
在上面的示例代码中,
Windows API
中的 CreateFile()
函数打开名为 test.txt 的文件,并获取其文件句柄;WriteFile()
函数将字符串写入文件;LockFile()
函数对文件进行锁定操作,并使用 UnlockFile()
函数进行解锁操作;注意:在使用 UnlockFile()
函数时,需要确保已经使用 CreateFile()
或其他文件打开函数打开了文件,并获得了有效的文件句柄。
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。