-------------------------------------------------------------------------------------------------
12.结构
C语言内在数据类型:
基本数据类型:float、double、char、int、long、short
unsigned int、 unsigned char
派生类型:数组、指针
指针:int* p char* p
数组:int arr[5] char arr[5]. (同类型的多个元素)
struct:关键字,定义用户自己的数据类型。

#if 1
#include <string.h>
/* 定义结构学生类型 */
struct Student{
int age;
float score;
char name[10];
};
/*传递参数 错误写法 */
void Scanf (struct Student stu){
printf("请输入学生姓名 年龄 分数\n");
scanf("%s",stu.name);
scanf("%d",&stu.age);
scanf("%f",&stu.score);
printf("%d\t%f\t%s\t\n",stu.age,stu.score,stu.name);
}
/*传递参数 地址 */
void ScanfAddress (struct Student *stu){
printf("请输入学生姓名 年龄 分数\n");
scanf("%s",stu->name);
scanf("%d",&(stu->age));
scanf("%f",&(stu->score));
printf("%d\t%f\t%s\t\n",stu->age,stu->score,stu->name);
}
int main(){
if(0)
{
struct Student stu;
Scanf(stu);
printf("%d\t%f\t%s\t\n",stu.age,stu.score,stu.name);
/* 错误 函数内部的值没有传递给外面的变量 */
ScanfAddress(&stu);
printf("%d\t%f\t%s\t\n",stu.age,stu.score,stu.name);
}else
{
struct Student stuArr[100];
struct Student *p=stuArr;
struct Student *q=0;
int n = 0;
while(1){
ScanfAddress(p);
printf("输入的数据为:序号:%d %d\t%f\t%s\t\n",n,p->age,p->score,p->name);
if(p->age<0) break;
n++;
p++;
}
printf("输入完成,开始遍历结果\n");
q=p;
for(p=stuArr;p<q;p++){
printf("%d\t%f\t%s\t\n",p->age,p->score,p->name);
}
}
return 0;
}
#endif13.动态内存分配
int arr[100] =>静态数组:空间不足或空间浪费
malloc函数 动态内存分配 malloc.h头文件中
int *p=(int *)malloc(3*sizeof(int))
强制类型转换(类型) eg:float a= (float)3/5;

if 1
#include<malloc.h> /*window下 */
#include<sys/malloc.h> /*mac下 */
#include<stdlib.h> /*mac下 */
int main(){
int *p=(int *)malloc(3*sizeof(int));
*p=20;
*(p+1)=30;
p[2]=40;
/* p[3]=50; 非法空间访问 */
for(int i=0;i<3;i++){
printf("%d\t",p[i]);
}
printf("\n");
/*重新分配 */
p=(int *)realloc(p,5*sizeof(int));
if(p == 0){
printf("扩充内存失败\n");
return 2;
}else{
printf("扩充内存成功\n");
}
printf("再次访问\n");
p[3]=50;
for(int i=0;i<5;i++){
printf("%d\t",p[i]);
}
/*释放内存 */
free(p);
return 0;
}
动态分配内存完成学生表:
if 1
#include <string.h>
#include <stdlib.h>
#include<sys/malloc.h> /*mac下 */
/* 动态内存分配 学生成绩表 */
struct Student {
char name[10];
int age;
float score;
};
void Scanf(struct Student *p){
printf("请输入学生姓名 年龄 分数\n");
scanf("%s",p->name);
scanf("%d",&(p->age));
scanf("%f",&(p->score));
printf("学生姓名:%s\t,年龄:%d\t,分数:%f\t\n",p->name,p->age,p->score);
};
int main(){
struct Student *stuArr=0;
struct Student *p=0;
struct Student *q=0;
int n=0,cap;
printf("请输入要分配的空间\n");
scanf("%d",&cap);
stuArr = (struct Student *)malloc(cap*sizeof(struct Student));
if(!stuArr){
printf("分配内存失败\n");
return 1;
}
p=stuArr;
while(1){
Scanf(p);
if(p->age<0)break;
p++;
n++;
if(n==cap){
cap*=2;
printf("分配内存已满,重新分配二倍内存:%d\n",cap);
stuArr=(struct Student *)realloc(stuArr,(cap)*sizeof(struct Student));
if(!stuArr){
printf("新增内存失败\n");
return 2;
}
printf("分配内存成功,请继续输入:\n");
p=stuArr+n; /* !!!重新指向新地址 */
}
}
printf("输入完成,开始遍历\n");
q=stuArr+n;
for(p=stuArr;p<q;p++){
printf("学生姓名:%s\t,年龄:%d\t,分数:%f\t\n",p->name,p->age,p->score);
}
return 0;
}
#endif14.文件读写
打开文件: FILE * fopen(“文件名”,”打开模式”);返回file结构类型的指针变量。
关闭文件:int fclose(FILE * 文件指针).

Eg:FILE *fp= fopen(“test.text”,”w”) //只写,创建新文件或覆盖已有文件。
FILE *fp= fopen(“test.text”,”r”) //只读,打开已有文件,文件不存在,报错。
FILE *fp= fopen(“test.text”,”a”) //追加,打开已有文件,在文件后写内容文件不存在,报错。
#if 1
/* 文件读写 */
int main(){
/* 打开或者创建文件 */
FILE *fp= fopen("test.text","w");
if(fp == 0){
printf("打开失败\n");
return 1;
}
/*写入数据 */
printf("开始写入数据\n");
fprintf(fp,"liping 78.9\n");
fprintf(fp,"%s %f","liming",99.7);
fclose(fp);
printf("写入数据完成,开始读取\n");
/* 读文件 */
char name[10];
float score;
fp= fopen("test.text","r");
fscanf(fp,"%s %f",name,&score);
printf("%s %f\n",name,score);
fscanf(fp,"%s %f",name,&score);
printf("%s %f\n",name,score);
fclose(fp);
return 0;
}
#elif 1
/* 文件读写 demo2*/
int main(){
struct Student{
char name[20];
float score;
};
struct Student s;
FILE *fr,*fw;
fr= fopen("test.text","r");
fw= fopen("test2.text","w");
while(fscanf(fr,"%s %f",s.name,&s.score) != EOF){
fprintf(fw,"%s %f",s.name,s.score);
fprintf(fw,"\n");
printf("%s %f\n",s.name,s.score);
}
fclose(fr);
fclose(fw);
return 0;
}
#endif单个字符写入 读取:
int getc(FILE *fp)//以整数int返回读入的字符,失败返回EOF
int putc(int character,FILE *fp)//将字符写入文件,成功返回这个字符,失败返回EOF
if 1
/* 文件读写 demo3*/
int main(){
char ch;
FILE *fp;
printf("请输入要写入的字符:\n");
fp=fopen("get_c.text","w");
/* getchar() 从键盘山读取字符 */
while((ch = getchar()) != EOF){
putc(ch,fp);
};
fclose(fp);
//mac ctrl-d 发送EOF window ctrl 结束输入
fp=fopen("get_c.text","r");
while((ch = getc(fp)) != EOF){
printf("%c",ch);
};
fclose(fp);
return 0;
}
#endif字符串行读取写入:
char *fgets(char *str,int num, FILE *fp)//从文件读n-1个字符到str,并添加换行符\0到最后,成功返回str,失败返回null(0)
int *fputs(char *str, FILE *fp)//将str字符串写入文件中,成功返回写入字符个数,失败返回EOF
#if 1
/* 文件读写 行字符读取写入 demo4*/
int main(){
char str[100];
FILE *fp;
printf("请输入要写入的字符:\n");
fp=fopen("fget_c.text","w");
/* gets() 从键盘山读取行 */
while( gets(str) != NULL){
fputs(str,fp);
};
fclose(fp);
fp=fopen("get_c.text","r");
while(fgets(str,100,fp) != NULL){
printf("%s",str);
};
fclose(fp);
return 0;
}
#endif二进制读写:
“wb” “rb” “ab”
数据块读写函数,fread() fwrite()
size_t fread(void *str,size_t size_of_element,size_t number_of_element, FILE *fp);
//成功返回读取元素的个数,应该等于number 否则失败
size_t fwrite(const void *str,size_t size_of_element,size_t number_of_element, FILE *fp);
//成功返回写入元素的个数,应该等于number 否则失败
#if 1
/* 文件读写 demo5 二进制读写*/
int main(){
struct Student{
char name[20];
int age;
float score;
};
struct Student s;
FILE *fp;
printf("请输入学生姓名 年龄 分数\n");
fp= fopen("student.bin","wb");
while(scanf("%s %d %f",s.name,&s.age,&s.score) != EOF){
fwrite(&s,sizeof(struct Student),1, fp);
}
fclose(fp);
fp= fopen("student.text","rb");
while(fread(&s,sizeof(struct Student),1, fp) == 1){
printf("%s %d %f\n",s.name,s.age,s.score);
}
fclose(fp);
return 0;
}
#endif文件指针定位
fseek(FILE *strem,long offset,int whence)
//指针当前位置whence 偏移 long offset whence(seek_set开头,seek_cur当前,seek_end结尾)
ftell()返回当前文件指针,相当于文件开头的位移量。
long filesize= ftell(*fp);
#if 1
#include <string.h>
/* 文件读写 demo6 指针定位*/
int main(){
struct Student{
char name[20];
int age;
float score;
};
FILE *fp;
long filesize;
struct Student s,s2;
strcpy(s.name,"lining");
s.age=12;
s.score=88.7;
/* wb+ 既可以读又可以写 */
fp=fopen("stu_test.text","wb+");
fwrite(&s,sizeof(struct Student),1,fp);
filesize=ftell(fp);
printf("文件当前位置是:%ld\n",filesize);
/* !!!写完之后必须重新定位到开头 才可以读取到数据 */
/* 重新定位指针到开头 */
printf("定位开头\n");
fseek(fp,0,SEEK_SET);
filesize=ftell(fp);
printf("文件当前位置是:%ld\n",filesize);
fread(&s2,sizeof(struct Student),1,fp);
printf("读取结果:%s %d %f%\n",s2.name,s2.age,s2.score);
return 0;
}
#endif(完成)
--------------------------------------------------------------------------------------------
看完点个赞吧,给别人点赞,会让自己觉得自己是一个善良的人。
关注一下,让自己觉得自己是一个友善的人,开放包容的人,人生会不经意间美妙一点点。
我的收获期待与你分享,希望一同成长!