C语言使用记录
水过硕士3年,非科班转码,几年来对所有的知识都是浅尝辄止,导致毕业进入一家资金链短缺发不起工资的芯片小公司,有幸部门同事一个有多个机器人大赛经验,一个有大厂工作经验通晓标准流程。常被pua,但挨打要立正。今日2023-06-18起,花1个小时记录所学技术,所遇问题。
函数的使用
为了将字符串“2023-06-18 14:15:16”分别提出2023 ,6,18,14,15,16这6个整型
2023/6/18
strtok(字符,分隔符)进行分割字符串
(1)每次调用strtok函数只能获得一个分割单位。
(2)要获得所有的分割单元必须反复调用strtok函数。
#include <string.h>
#include <stdio.h>
//有
int a[10];
int i = 0;
char *token = strtok("192.168.11.12", ".");/* 获取第一个子字符串 */
/* 继续获取其他的子字符串 */
while (token != NULL)
{
a[i] = atoi(token);
i++;
token = strtok(NULL, ".");
}
//a=[192,168,11,12]知
int main () {
char str[80] = "“2023-06-18 14:15:16";
const char c[2] = " ";
char *token = strtok(str, c);//先将前后分开获得第一个子字符串“2023-06-18”
/* 继续获取其他的子字符串 */
while( token != NULL ) {
/*
得到两组不同“-”“:”的字符串
*/
token = strtok(NULL, c);
}
return(0);
}
char string[] = "A darkone\tof *-,tokens\nand other tokens";
char seps[] = " ,-\t\n";//包含所有需要过滤的字符
char *token;
token = strtok( string, seps );
while(token !=NULL)
{
printf("%s\n",token);
token=strtok(NULL,seps);
}
//返回 A darkone of tokens and other tokens 7个字符串
isdigit()判断字符或字符串是否是数字
当字符为0-9时返回非零
char str[]="123@#FDsP[e?";
for( int i=0;str[i]!=0;i++)
{
if( isdigit (str[i]) )
printf( "%c is an digit character\n",str[i] );}
//1,2,3
atoi() 函数
把字符串转换成整型数。
默认使用十进制转换
char *ptr = "1234";
char *ptr = "0123";
char *ptr = "12.33";
char *ptr = "-1234";
char *ptr = "+21";
char *ptr = "0";
char *ptr = "s3241";
char *ptr = "0x1234"
a = atoi(ptr);
/*1234,
123,
12,
-1234,
21,
0,
0.
0
*/
sscanf()将字符串中数字拿出
2023-06-19
从一个字符串中读进与指定格式相符的数据
char str[100];
//用法一:取指定长度的字符串
sscanf("12345","%4s",str);
//1234
//用法二:格式化时间
int year,month,day,hour,minute,second;
sscanf("2013/02/13 14:55:34","%d/%d/%d %d:%d:%d",&year,&month,&day,&hour,&minute,&second);
//2013,2,13,14,55,34
//用法三:读入字符串
sscanf("12345","%s",str);
//12345
//用法四:%*d 和 %*s 加了星号 (*) 表示跳过此数据不读入. (也就是不把此数据读入参数中)
sscanf("12345acc","%*d%s",str);
//acc
//用法五:取到指定字符为止的字符串。如在下例中,取遇到'+'为止字符串。
sscanf("12345+acc","%[^+]",str);
//12345
//用法六:取到指定字符集为止的字符串。如在下例中,取遇到小写字母为止的字符串。
sscanf("12345+acc121","%[^a-z]",str);
//12345+
strtol()
char buffer[20]="103cend$3";
char *stop;
printf("%d\n",strtol(buffer, &stop, 8));
printf("%s\n", stop);
if(*stop=='\0')
printf("%ld\n",long_num);
else
printf("转换失败\n");
//67 8位103 ->2位001 000 011 ->10w位 2^6+3=64+3
//cend$3
位移计算
除2: a>>1
int address_int = 8;
printf("Address: %d\n", address_int >> 1);
//4
奇数:? a&1==0
2023-6-25
sprintf()
函数将整数 num 转换为字符串,并将结果写入 buf 缓冲区中。
int num = 123;
char buf[16];
// 将 num 转换为字符串
sprintf(buf, "%d", num);
char *itoa( int value, char *string,int radix);
原型说明:
value:欲转换的数据。
string:目标字符串的地址。
radix:转换后的进制数,可以是10进制、16进制等。
int num = 123;
char buf[16];
// 将 num 转换为字符串
itoa(num, buf, 10);
获取文件信息
#define SEEK_END 2
#define SEEK_SET 0
FILE *file = fopen(file_path, "rb");
size_t image_size=0;
if (!file)
{
ESP_LOGE(TAG, "无法打开文件: %s", file_path);
continue;
}
// 获取图像文件的大小
fseek(file, 0, SEEK_END);//作用就是将文件指针所指向的文件的读写位置指针移动到特定的位置
//fseek(fp, 8, SEEK_SET); 代表开头位置往右边移动8个字节。
image_size = ftell(file);//这个函数可以用来获取文件内部指针(位置指针)距离文件开头的字节数
rewind(file);//将读写位置指针重置到文件开头
}
fread( void *buffer, size_t size, size_t count, FILE *stream)
从文件中读取若干字节数据到内存缓冲区中
FILE *p = fopen("D:\\a.txt", "rb");
// 用于接收读取数据的缓冲区
char buffer[4] = {0};
while(!feof(p)){
memset(buffer, 0, sizeof(buffer));
// buffer : 将文件读取到内存的位置
// sizeof(char) : 读取的基本单元字节长度
// sizeof(buffer) : 读取的基本单元个数,
// 读取字节个数是 sizeof(buffer) * sizeof(char)
// p : 文件指针
fread(buffer, sizeof(char), sizeof(buffer), p);
// 打印读取的内容
printf("buffer = %s\n", buffer);
}
##获取字符串的长度
int arr[] = {1, 2, 3, 4, 5};
int *p = arr;
size_t len = sizeof(arr) / sizeof(arr[0]);
char *str[] = {"hello", "world", "how", "are", "you"};
char **p = str;
size_t len = strlen(*p);//返回第一个字符串的长度
size_t len = 0;
for (int i = 0; i < sizeof(str) / sizeof(str[0]); i++) {
len += strlen(str[i]);//返回所有数组长度
}