用c语言自己实现linux里的memcoy()
时间: 2024-05-01 21:16:58 浏览: 6
```c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
void *my_memcpy(void *dest, const void *src, size_t n)
{
char *d = dest;
const char *s = src;
while (n--)
*d++ = *s++;
return dest;
}
int main()
{
char str1[] = "Hello, World!";
char str2[20];
my_memcpy(str2, str1, strlen(str1) + 1);
printf("Copied string: %s\n", str2);
return 0;
}
```
相关问题
c语言编程实现linux cp命令
c语言编程实现Linux cp命令可以参考下面的代码:
```c
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char *argv[]) {
FILE *source, *destination;
char ch;
if (argc < 3) {
printf("未传入源文件路径或者目标文件路径\n");
exit(1);
}
source = fopen(argv[1], "r");
if (source == NULL) {
printf("打开源文件失败!\n");
exit(1);
}
destination = fopen(argv[2], "w");
if (destination == NULL) {
printf("打开目标文件失败!\n");
exit(1);
}
while ((ch = fgetc(source)) != EOF) {
fputc(ch, destination);
}
printf("文件复制成功!\n");
fclose(source);
fclose(destination);
return 0;
}
```
linux c语言自己实现16进制转10进制
可以使用C语言中的 strtol 函数将16进制数转换为10进制数。示例代码如下:
```
#include <stdio.h>
#include <stdlib.h>
int main() {
char hex_num[] = "0x3A";
int dec_num = strtol(hex_num, NULL, 16);
printf("16进制数 %s 转换为10进制数为 %d\n", hex_num, dec_num);
return 0;
}
```
输出结果为:
```
16进制数 0x3A 转换为10进制数为 58
```
希望能够帮到你。