四:strstr
char *strstr(const char *haystack, const char *needle);
strstr() 函数用于在字符串 haystack 中查找子字符串 needle 的第一次出现位置。如果找到,则返回指向该位置的指针;如果未找到,则返回 NULL。
参数
haystack -- 要搜索的主字符串。
needle -- 要查找的子字符串。
返回值
返回指向 haystack 中第一次出现 needle 的位置的指针。如果 needle 未在 haystack 中找到,则返回 NULL。
if (argc != 2) {
printf("Usage: %s<filename>\n", argv[0]);
return -1;
}
FILE *fp = fopen(argv[1], "r");
if (NULL == fp) {
fprintf(stderr, "open error\n");
return 1;
}
char buf[1024];
int line_count = 0;
char test[8] = "#define";
while (fgets(buf, sizeof(buf), fp)) {
line_count++;
char *ret = strstr(buf, test);
if (ret != NULL) {
buf[strlen(buf)-1] = '\0';
//want_word[strlen(want_word) - 1] = '\0'
printf("%s\t%s\tline %d\n",buf,argv[1],line_count);
}
}
fclose(fp);
五:symlink(软链接)
链接文件: file.txt -> hello.c
软链接文件、符号链接文件(-s,没有限制)
硬链接文件
命令行:ln -s 123(文件名) softlink 快捷方式
ln -s /home/linux/20250623_cd/02file/01stdio/2.png ~/Desktop/
int symlink(const char *oldpath, const char *newpath);
功能:创建一个链接向oldpath文件的新符号链接文件
参数:oldpath:被链接向的文件的路径
newpath:新符号链接文件
返回值: 成功返回0
失败返回-1
#include <stdio.h>
#include <unistd.h>//linux下的系统库
int main(int argc, const char *argv[])
{
int ret = symlink("/home/linux/01-show_pic/0.bmp","/home/linux/Desktop/0.bmp");//vi编程下就不~,识别不出
if(-1 == ret)
{
printf("stmlink error\n");
return -1;
}
return 0;
}
六:remove(类似rm)
int remove(const char *pathname);
功能: 删除一个文件(不能删目录)
参数: pathname:删除文件的路径
返回值: 成功返回0
失败返回-1
int ret = remove("/home/linux/Desktop/0.bmp");
if(-1 == ret)
{
printf("remove error\n");
return -1;
}
七:rename//mv rename
int rename(const char *oldpath, const char *newpath);
功能: 将一个老的路径名改为新的路径
参数: oldpath:老路径名
newpath:新路径名
返回值: 成功返回0
失败返回-1
int ret = rename("./hw.c","./hw1.c");
if(-1 == ret)
{
printf("rename error\n");
return -1;
}
八:link ln 1 2 (硬链接)
link ln 1.txt 2.txt
硬链接只能指文件,也不能跨文件介质
eg:硬链接:如果2号文件链接1号文件,如果一号文件删除,2号文件不受影响
软连接:1号文件删除,2号文件不可访问
int link(const char *oldpath, const char *newpath);
功能: 创建一个硬链接文件
参数: oldpath:要链接向的文件
newpath:创建的新硬链接文件
返回值: 成功返回0
失败返回-1
#include <stdio.h>
#include <unistd.h>
int main(int argc, char *argv[])
{
int ret = link("./1.txt","./2.txt");//2.txt必须不存才可以硬链接
if(-1 == ret)
{
printf("link error\n");
return 1;
}
return 0;
}
九:truncate
功能:把文件变大或者变小
#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>//基本系统数据类型.Unix/Linux系统的基本系统数据类型的头文件
int main(int argc, char *argv[])
{
//264
int ret = truncate("./1.txt",264);
if(-1 == ret)
{
printf("truncate error\n");
return 1;
}
return 0;
十:练习(文件/内核链表,小功能集成)
主要是save和load这两个函数本质上就是链表和二进制文件的相互读取和写入
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "list.h"
typedef struct
{
int id;
char name[50];
char phone[15];
char addr[100];
struct list_head node;
} PER;
int printMenu()
{
printf("1.showall\n");
printf("2.add per\n");
printf("3.del per\n");
printf("4.modify per\n");
printf("5.search per\n");
printf("6.quit\n");
printf("pls choose :");
char buf[5] = {0};
fgets(buf, sizeof(buf), stdin);
int choose = atoi(buf);
return choose;
}
int addper(struct list_head* head)
{
PER* tmp = malloc(sizeof(PER));
if (NULL == tmp)
{
printf("addper malloc error\n");
return 1;
}
char buf[100] = {0};
printf("pls input id:");
fgets(buf, sizeof(buf), stdin);
tmp->id = atoi(buf);
printf("pls input name:");
bzero(buf, sizeof(buf));
fgets(buf, sizeof(buf), stdin); // zhangsan\n
buf[strlen(buf) - 1] = '\0';
strcpy(tmp->name, buf);
printf("pls input phone:");
bzero(buf, sizeof(buf));
fgets(buf, sizeof(buf), stdin); // zhangsan\n
buf[strlen(buf) - 1] = '\0';
strcpy(tmp->phone, buf);
printf("pls input addr:");
bzero(buf, sizeof(buf));
fgets(buf, sizeof(buf), stdin); // zhangsan\n
buf[strlen(buf) - 1] = '\0';
strcpy(tmp->addr, buf);
list_add(&tmp->node, head);
return 0;
}
int showall(struct list_head* head)
{
PER *tmp, *next;
list_for_each_entry_safe(tmp, next, head, node)
{
printf("id:%d name:%s phone:%s addr:%s\n", tmp->id, tmp->name, tmp->phone,
tmp->addr);
}
return 0;
}
int delper(struct list_head* head)
{
printf("pls input id ,to del per");
char buf[5] = {0};
fgets(buf, sizeof(buf), stdin);
int id = atoi(buf);
PER *tmp, *next;
list_for_each_entry_safe(tmp, next, head, node)
{
if (tmp->id == id)
{
list_del(&tmp->node);
free(tmp);
return 0;
}
}
return 1;
}
int modifyper(struct list_head* head)
{
printf("pls input id , to modify per");
char buf[5] = {0};
fgets(buf, sizeof(buf), stdin);
int id = atoi(buf);
PER *tmp, *next;
list_for_each_entry_safe(tmp, next, head, node)
{
if (tmp->id == id)
{
char buf[100] = {0};
printf("pls input id:");
fgets(buf, sizeof(buf), stdin);
tmp->id = atoi(buf);
printf("pls input name:");
bzero(buf, sizeof(buf));
fgets(buf, sizeof(buf), stdin); // zhangsan\n
buf[strlen(buf) - 1] = '\0';
strcpy(tmp->name, buf);
printf("pls input phone:");
bzero(buf, sizeof(buf));
fgets(buf, sizeof(buf), stdin); // zhangsan\n
buf[strlen(buf) - 1] = '\0';
strcpy(tmp->phone, buf);
printf("pls input addr:");
bzero(buf, sizeof(buf));
fgets(buf, sizeof(buf), stdin); // zhangsan\n
buf[strlen(buf) - 1] = '\0';
strcpy(tmp->addr, buf);
return 0;
}
}
return 1;
}
int searchper(struct list_head* head)
{
PER *tmp, *next;
char buf[5] = {0};
printf("pls id,to find per:");
fgets(buf, sizeof(buf), stdin);
int id = atoi(buf);
list_for_each_entry_safe(tmp, next, head, node)
{
if (tmp->id == id)
{
printf("id:%d name:%s phone:%s addr:%s\n", tmp->id, tmp->name, tmp->phone,
tmp->addr);
return 0;
}
}
return 1;
}
int loadfile(struct list_head* head)
{
FILE* fp = fopen("data", "r");
if (NULL == fp)
{
printf("fopen error\n");
return 1;
}
while (1)
{
PER per = {0};
int ret = fread(&per, sizeof(PER), 1, fp);
if (0 == ret)
{
break;
}
PER* tmp = malloc(sizeof(PER));
if (NULL == tmp)
{
printf("fread malloc error\n");
return 1;
}
tmp->id = per.id;
strcpy(tmp->name, per.name);
strcpy(tmp->phone, per.phone);
strcpy(tmp->addr, per.addr);
list_add(&tmp->node, head);
}
fclose(fp);
return 0;
}
int savefile(struct list_head* head)
{
FILE* fp = fopen("data", "w");
if (NULL == fp)
{
printf("fopen error\n");
return 1;
}
PER *tmp, *next;
list_for_each_entry_safe(tmp, next, head, node)
{
fwrite(tmp, sizeof(PER), 1, fp);
}
fclose(fp);
return 0;
}
int main(int argc, char** argv)
{
struct list_head head;
INIT_LIST_HEAD(&head);
loadfile(&head);
while (1)
{
int choose = printMenu();
if (6 == choose)
{
break;
}
switch (choose)
{
case 1:
showall(&head);
break;
case 2:
addper(&head);
break;
case 3:
delper(&head);
break;
case 4:
modifyper(&head);
break;
case 5:
searchper(&head);
break;
}
}
savefile(&head);
// system("pause");
return 0;
}
//自己写的,save/load没写出来,测试能用
//直接写的,缺save和load
#include "list.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct {
int id;
char name[20];
char phone[20];
char addr[100];
struct list_head node;
} PACK;
int add_per(struct list_head *head) {
PACK *tmp = malloc(sizeof(PACK));
if (NULL == tmp) {
fprintf(stderr, "add_word malloc\n");
return 1;
}
printf("Enter ID: ");
scanf("%d", &tmp->id);
printf("Enter Name: ");
scanf("%s", tmp->name);
printf("Enter Phone: ");
scanf("%s", tmp->phone);
printf("Enter Address: ");
scanf("%s", tmp->addr);
list_add(&tmp->node, head); // list_add_tail(&tmp->node, head);
return 0;
}
int show_per(struct list_head *head) {
PACK *tmp, *next; //创立临时节点和其下一个节点
list_for_each_entry_safe(
tmp, next, head,
node) // for()
//借用系统数据库.h的函数,本质上,让tmp接入旧数据中,起到循环作用
//可以理解数组的下标用来遍历数组的元素
{
printf("id:%d name:%s phone:%s addr:%s\n", tmp->id, tmp->name, tmp->phone,
tmp->addr);
}
return 0;
}
PACK *find_Per(struct list_head *head) {
PACK *tmp, *next;
printf("Enter Name: ");
char name[20] = {0};
scanf("%s", name);
list_for_each_entry_safe(tmp, next, head, node) {
if (0 == strcmp(tmp->name, name)) {
printf("find it\n");
printf("id:%d name:%s phone:%s addr:%s\n", tmp->id, tmp->name, tmp->phone,
tmp->addr);
return tmp;
}
}
printf("not find\n");
return NULL;
}
PACK *find_Per_Mod(struct list_head *head) {
PACK *tmp, *next;
int id = 0;
printf("input id\n");
scanf("%d", &id);
list_for_each_entry_safe(tmp, next, head, node) {
if (tmp->id == id) {
return tmp;
}
}
return NULL;
}
int modfiy_PerID(struct list_head *head) {
PACK *found = find_Per_Mod(head);
if (found == NULL) {
printf("ID not found!\n");
return 1;
}
printf("Enter ID: ");
scanf("%d", &found->id);
printf("Enter Name: ");
scanf("%s", found->name);
printf("Enter Phone: ");
scanf("%s", found->phone);
printf("Enter Address: ");
scanf("%s", found->addr);
return 0;
}
int Del_PerID(struct list_head *head) {
int id;
scanf("%d", &id);
PACK *tmp, *next;
list_for_each_entry_safe(tmp, next, head, node) // for()
{
if (tmp->id == id) {
list_del(&tmp->node); //把当前节点从 list 中删掉。
free(tmp); //释放tmp
printf("Del successed\n");
}
}
return 0;
}
void menu(struct list_head *head) {
int n;
do {
printf("-----------------------------------------------------------\n");
printf("1:show_all\n");
printf("2:new_add Per\n");
printf("3:modfiy Per_id\n");
printf("4:Del Per\n");
printf("5:Find Per\n");
printf("6:quit\n");
printf("-----------------------------------------------------------\n");
scanf("%d", &n);
switch (n) {
case 1:
show_per(head);
break;
case 2:
add_per(head);
break;
case 3:
modfiy_PerID(head);
break;
case 4:
Del_PerID(head);
break;
case 5:
find_Per(head);
break;
case 6:
break;
default:
printf("Invalid choice! Try again.\n");
n = 6;
break;
}
} while (n != 6);
}
int loadfile(struct list_head* head)
{
FILE* fp = fopen("data", "r");
if (NULL == fp)
{
printf("fopen error\n");
return 1;
}
while (1)
{
PACK per = {0};
int ret = fread(&per, sizeof(PACK), 1, fp);
if (0 == ret)
{
break;
}
PACK* tmp = malloc(sizeof(PACK));
if (NULL == tmp)
{
printf("fread malloc error\n");
return 1;
}
tmp->id = per.id;
strcpy(tmp->name, per.name);
strcpy(tmp->phone, per.phone);
strcpy(tmp->addr, per.addr);
list_add(&tmp->node, head);
}
fclose(fp);
return 0;
}
int savefile(struct list_head* head)
{
FILE* fp = fopen("data", "w");
if (NULL == fp)
{
printf("fopen error\n");
return 1;
}
PACK *tmp, *next;
list_for_each_entry_safe(tmp, next, head, node)
{
fwrite(tmp, sizeof(PACK), 1, fp);
}
fclose(fp);
return 0;
}
int main(int argc, char **argv) {
struct list_head head;
INIT_LIST_HEAD(&head);
loadfile(&head);
int n;
menu(&head);
savefile(&head);
// system("pause");
return 0;
}
1381

被折叠的 条评论
为什么被折叠?



