首页 技术 正文
技术 2022年11月16日
0 收藏 334 点赞 3,573 浏览 1917 个字

1.结构体写入文件,读取

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define max 5struct books{
char title[];
char author[];
int price;
};const char * fileName="books.txt";
int size=sizeof(struct books);void read(struct books bks[]);
void write(struct books bks[]);
void list(struct books bks[]);
void demo(struct books bks[]);void main(void)
{
struct books bks[max]; //write(bks);
read(bks);}void write(struct books bks[])
{
FILE * fp;
int i=;
int addList;
if((fp=fopen(fileName,"a+b")) == NULL)
{
printf("file open fail");
} puts("大侠,你想添加几条数据?");
scanf("%d",&addList); if(addList>)
{
do{
printf("please add new book title\n");
scanf("%s",&bks[i].title);
printf("please add new book author\n");
scanf("%s",&bks[i].author);
printf("please add new book price\n");
scanf("%d",&bks[i].price);
i++;
fwrite(&bks[i],size,,fp);
}while(i<addList);
}}void read(struct books bks[])
{
FILE * fp;
int i=;
if((fp=fopen(fileName,"r+")) == NULL)
{
printf("file open fail");
} rewind(fp); while( i<max && fread(&bks[i],size,,fp) == )
{
printf("title is %s,author is %s,price is %d\n",bks[i].title,bks[i].author,bks[i].price);
i++;
}}

2. 获取指定字符在字符串的最后的位置

 #include <string.h>
#include <stdio.h>
#include <stdlib.h> int main(int argc,char *argv[])
{
if(argc<)
{
puts("params error");
exit();
} char *p;
char c = argv[][]; //传入来的都是字符串,得到需要的字符 ,去掉最后的\0
int weizhi; p = strrchr(argv[],c);//得到字符c在字符串argv[1]中的地址,返回的是字符指针 if(p)
weizhi = p - argv[]; //两个指针相减,得到位置距离
printf("%d",weizhi);
else
puts("not found"); return ;
}

3.字符串查找

 #include <string.h>
#include <stdio.h>
#include <stdlib.h>
#define TRUE 1
#define FALSE 0
#define ERROR 0 int strStart(char *,char *); int main(int argc,char *argv[])
{
char message[]="hello,welcome to China";
char find[]="Ch";
int start = strStart(message,find);
printf("%d",start);
} int strStart(char * String,char * find)
{
int start=-,i=,j=; //得到字符串长度
int StringLen = strlen(String);
int findLen = strlen(find);
//判断是否为空
if(StringLen<= || findLen<=)
{
return FALSE;
}
//判断子串的长度是否大于母串的长度
if(StringLen < findLen)
{
return FALSE;
}
//开始查找
for(i=;i<=StringLen;i++)
{
if(String[i]==find[j])
{
if(start<)
start = i; j++;
}else{
if(j<findLen)
start=-;
}
}
return start;
}
相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,088
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,565
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,413
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,186
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,822
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,905