一.上期回顾
上篇博客的链接如下:
二.strstr的模拟实现
cpp
#define _CRT_SECURE_NO_WARNINGS 1
#include <stdio.h>
#include <assert.h>
//暴力求解
char* my_strstr(const char* str1, const char* str2)
{
const char* cur = str1;
const char* s1 = NULL;
const char* s2 = NULL;
assert(str1 && str2);
if (*str2 == '\0')
{
return (char*)str1;
}
while (*cur)
{
s1 = cur;
s2 = str2;
while (*s1 && *s2 && *s1 == *s2)
{
s1++;
s2++;
}
if (*s2 == '\0')
{
return (char*)cur;
}
cur++;
}
return NULL;
}
int main()
{
char arr1[] = "abcdef";
char arr2[] = "abcdef";
char* ret = my_strstr(arr1, arr2);
if (ret != NULL)
printf("%s\n", ret);
else
printf("找不到\n");
return 0;
}

三.strtok函数的使用



cpp
#include <string.h>
int main()
{
char arr[] = "zhangsan@163.com#hehe";
char arr2[30] = {0}; //zhangsan\0163\0com
strcpy(arr2, arr);
const char* p = "@.#";
char* s = NULL;
// 初始化部分只执行一次
for (s = strtok(arr2, p); s != NULL; s=strtok(NULL, p))
{
printf("%s\n", s);
}
//char *s = strtok(arr2, p);
//printf("%s\n", s);
//s = strtok(NULL, p);
//printf("%s\n", s);
//s = strtok(NULL, p);
//printf("%s\n", s);
return 0;
}
四.strerror函数的使用

cpp
int main()
{
int i = 0;
for (i = 0; i < 10; i++)
{
printf("%d: %s\n",i, strerror(i));
}
return 0;
}

将对应的错误码,转换成错误信息
cpp
#include <stdio.h>
#include <string.h>
#include <errno.h>
int main()
{
FILE* pFile;
pFile = fopen("unexist.txt", "r");
if (pFile == NULL)
printf("Error opening file unexist.ent: %s\n", strerror(errno));
else
printf("打开文件成功\n");
return 0;
}
cpp
#include <stdio.h>
#include <string.h>
#include <errno.h>
int main()
{
FILE* pFile;
pFile = fopen("unexist.ent", "r");
if (pFile == NULL)
//printf("Error opening file unexist.ent: %s\n", strerror(errno));
perror("Error opening file unexist.ent");
return 0;
}

