截取、拼接字符串,memcpy
字符串 截取 拼接 memcpy
2023-09-11 14:14:44 时间
1. 截取字符串
#include<string.h>
int main(
{
char* s="GoldenGlobalView";
char d[20];
memcpy(d,s+12,4); //从第13个字符(V)开始复制,连续复制4个字符(View)
d[4]='\0'; //memcpy(d,s+14*sizeof(char),4*sizeof(char));也可
printf("%s",d);
getchar();
return 0;
}
输出结果:
View
2. 填充字符串
/*取得当前目录下的文件个数*/
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <sys/wait.h>
#include <string.h>
#define MAXLINE 1024
#define RES_MAX 10240
int main()
{
char result_buf[MAXLINE], command[MAXLINE];
char result[RES_MAX] = {0};
int rc = 0; // 用于接收命令返回值
FILE *fp;
/*将要执行的命令写入buf*/
snprintf(command, sizeof(command), "ls ./");
/*执行预先设定的命令,并读出该命令的标准输出*/
fp = popen(command, "r");
if(NULL == fp)
{
perror("popen执行失败!");
exit(1);
}
while(fgets(result_buf, sizeof(result_buf), fp) != NULL)
{
/*为了下面输出好看些,把命令返回的换行符去掉*/
if('\n' == result_buf[strlen(result_buf)-1])
{
result_buf[strlen(result_buf)-1] = '\0';
}
// printf("命令【%s】 输出【%s】\r\n", command, result_buf);
memcpy(result + strlen(result), result_buf, strlen(result_buf));
result[strlen(result)] = ',';
}
printf ("result = %s\n", result);
/*等待命令执行完毕并关闭管道及文件指针*/
rc = pclose(fp);
if(-1 == rc)
{
perror("关闭文件指针失败");
exit(1);
}
else
{
printf("命令【%s】子进程结束状态【%d】命令返回值【%d】\r\n", command, rc, WEXITSTATUS(rc));
}
return 0;
}
相关文章
- oc 之中的 汉字字符串转化成为拼音 汉字字符串的排序
- 如何将字符串写入文件?
- JavaScript 13. 字符串
- 【数据库】sql中字符串截取、拼接、字符串与数字之间相互转换
- 第六节:详细讲解Java中的装箱与拆箱及其字符串
- .bat批处理(十):从路径字符串中截取盘符、文件名、后缀名等信息
- c# 将数组或集合 转换成以逗号分隔的字符串
- js加密字符串
- 字符串截取
- 《Python参考手册(第4版•修订版)》——1.5 字符串
- iOS字符串的各种用法(字符串插入、字符串覆盖、字符串截取、分割字符串)
- 操作字符串--实现跑马灯效果--截取头部与尾部,拼接
- java中截取字符串的方式
- JQuery判断数组中是否包含某个元素,字母大小写转换函数,判断字符串中是否包含特定字符,
- [LeetCode] Multiply Strings 字符串相乘
- c#截取两个指定字符串中间的字符串(转载)
- 字符串截取
- Excel 字符串截取和查找 提取收货人姓名