Linux系统编程-IO操作实现简易QQ登录设计

前言

Linux系统编程使用open,read,write,close等文件IO操作函数,实现简易QQ登录设计。


一、文件IO操作函数

1.1.open函数

	   #include <sys/types.h>
       #include <sys/stat.h>
       #include <fcntl.h>
       int open(const char *pathname, int flags);
       int open(const char *pathname, int flags, mode_t mode);
       int creat(const char *pathname, mode_t mode);

1.2.read函数

	   #include <unistd.h>
       ssize_t read(int fd, void *buf, size_t count);

1.3.write函数

	   #include <unistd.h>
       ssize_t write(int fd, const void *buf, size_t count);

1.4.close函数

       #include <unistd.h>
       int close(int fd);

二、实现过程

2.1 流程图

在这里插入图片描述

2.2 核心代码

2.2.1读取操作,获得qq号

		read(fd,(char *)buf,32);
		char *qq_id_start = strstr(buf, "qq_id=");
    	if (qq_id_start != NULL) 
		{
	        qq_id_start += strlen("qq_id="); // 移动到"qq_id="后面的位置
	        char *qq_id_end = strchr(qq_id_start, '\n'); 
	        if (qq_id_end != NULL) 
			{
	            // 提取qq_id数据
	            strncpy(qq_id, qq_id_start, qq_id_end - qq_id_start);
	            qq_id[qq_id_end - qq_id_start] = '\0';         
	            printf("qq_id: %s\n", qq_id);
        	}
    	}

2.2.2保存操作

	fd = open("./user.txt",O_CREAT | O_WRONLY | O_TRUNC,0777);
	if(fd < 0)
	{
		printf("open user.txt error\n");
		return -1;
	}
	//write qq to user.txt file
	write(fd,"qq_id=",strlen("qq_id="));
	write(fd,qq_id,strlen(qq_id));
	write(fd,"\n",1);
	write(fd,"qq_pwd=",strlen("qq_pwd="));
	write(fd,qq_pwd,strlen(qq_pwd));


三、运行结果

在这里插入图片描述

四、总结

练习使用Linux系统IO文件操作。
视频讲解详见哔哩哔哩:【Linux系统编程-IO操作实现简易QQ登录设计-哔哩哔哩】 https://b23.tv/qQ907HS

  • 9
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值