2024/03/28作业

一、练习题

1、使用fgetc计算一个文件有多少行

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <errno.h>
int main(int argc, const char *argv[])
{
	FILE *fp = fopen(argv[1],"r");
	if(fp == NULL)
	{
		printf("打开文件失败\n");
		return 1;
	}
	int count=0;
	while(1)
	{
		int ret =fgetc(fp);
		if(ret=='\n')
			count++;
		if(ret<=0)
			break;
	}
	printf("该文件有%d行\n",count);
	fclose(fp);
	return 0;
}

运行结果: 

ubuntu@ubuntu:0328$ gcc 4.c
ubuntu@ubuntu:0328$ ./a.out demo2.text
该文件有2行
ubuntu@ubuntu:0328$ cat demo2.text
hello word
helol shanghai

2、编写一条学生链表,写一些能够像链表里边添加数据的函数

实现:将链表中的所有内容保存到文件中去 以及 读取文件中的所有内容,加载到链表里面

创建链表相关函数:

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define N 20
typedef struct node
{
	union
	{
		int len;
		char name[N];
	};
	struct node *next;
}node,*node_p;
//创建链表
node_p creat_node()
{
	node_p H = (node_p)malloc(sizeof(node));
	if(H==NULL)
	{
		printf("空间申请失败\n");
		return NULL;
	}
	H->len=0;
	H->next=NULL;
	return H;
}
//创建新结点
node_p creat_new_node()
{
	node_p new = (node_p)malloc(sizeof(node));
	if(new==NULL)
	{
		printf("空间申请失败\n");
		return NULL;
	}
	printf("请输入学生姓名:");
	scanf("%s",new->name);
	new->next=NULL;
	return new;
}
//按位置插入
void insert_pos(node_p H,int pos)
{
	node_p new=creat_new_node();
	if(pos>H->len+1||pos<1)
	{
		printf("位置不合理\n");
		return;
	}
	node_p p=H;
	for(int i=1;i<pos;i++)
	{
		p=p->next;
	}
	new->next=p->next;
	p->next=new;
	H->len++;
}
//输出
void show(node_p H)
{
	if(H==NULL)
	{
		printf("入参为空,请检查");
		return ;
	}
	node_p p=H;
	int i=1;
	while(p->next!=NULL)
	{
		p=p->next;
		printf("name%i=%s\n",i++,p->name);
	}
}
//清空链表中的数据
void clean_node(node_p H)
{
	if(H==NULL)
	{
		printf("入参为空,请检查\n");
		return;
	}
	while(H->next!=NULL)
	{
		node_p del = H->next;
		H->next=del->next;
		free(del);
		H->len--;
	}
}

 将链表中的所有内容保存到文件中去:

//链表中的数据保存到文件中
void serve_to_file(node_p H,char *string)
{
	if(H==NULL)
	{
		printf("入参为空,请检查\n");
		return;
	}
	FILE *fp = fopen(string,"w");
	if(fp==NULL)
	{
		printf("打开文件失败\n");
		return;
	}
	node_p p = H;
	char temp = 0;
	while(p->next!=NULL)
	{
		p=p->next;
		fprintf(fp,"%s\n",p->name);
	}
	fclose(fp);
}

 读取文件中的所有内容,加载到链表里面:

//文件中的数据存入链表
void serve_to_node(node_p H,char *string)
{
	if(H==NULL)
	{
		printf("入参为空,请检查\n");
		return;
	}
	FILE *fp = fopen(string,"r");
	if(fp==NULL)
	{
		printf("打开文件失败\n");
		return;
	}
	node_p p = H;
	while(p->next!=NULL)
	{
		p=p->next;
	}

	while(1)
	{
		node_p new = (node_p)malloc(sizeof(node));
		int ret = fscanf(fp,"%s",new->name);
		if(ret<=0)
			break;
		new->next=p->next;
		p->next=new;
		H->len++;
		p=p->next;
	}
	fclose(fp);
}

主函数:

//主函数部分
int main(int argc, const char *argv[])
{
	node_p H=creat_node();
	insert_pos(H,1);
	insert_pos(H,2);
	insert_pos(H,3);
	insert_pos(H,4);
	serve_to_file(H,"1.text");
	clean_node(H);
	serve_to_node(H,"2.text");
	show(H);
	return 0;
}

 

运行结果:

ubuntu@ubuntu:0328$ gcc 5.c
ubuntu@ubuntu:0328$ ./a.out
请输入学生姓名:zhangsan
请输入学生姓名:lisi
请输入学生姓名:wangwu
请输入学生姓名:zhaoliu
name1=zhangsan
name2=lisi
name3=wangwu
name4=zhaoliu
name5=qianqi
name6=zhuba
name7=huangjiu
ubuntu@ubuntu:0328$ cat 1.text
zhangsan
lisi
wangwu
zhaoliu
ubuntu@ubuntu:0328$ cat 2.text
zhangsan
lisi
wangwu
zhaoliu
qianqi
zhuba
huangjiu

二、思维导图

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值