#Linux中的GCC编程# 第一次作业

LSD 培训作业

2017年8月17号

1、Demo1 带参主函数,参数打印输出

#include<stdio.h>
#include<stdlib.h>
#include<unistd.h>

void delay(int time)
{
	usleep(time*10000);
}
int main(int argc,char* argv[])
{
#if 1
	if(argc<2)
	{
		printf("缺少\n");
		return -1;
	}
	/*
	if(argc!=2)
	{
		char* p="参数不够";
		fprintf(stderr,"%s\n",p);

		//或者		
		perror("参数不够\n");
		return -1;
	}
	*/
	int i=0;
	while(argv[1][i])
	{
	    printf("%c ",argv[1][i]);
	    fflush(stdout);
	    delay(10);
	    i++;
	}
	printf("\n");
#else
	if(argc<2)
	{
		fprintf();
	}

#endif

	return 0;
}

/*
运行结果:
kshine@kshine-virtual-machine:/usr/Kshine/lsd/0817$ ./D1 Kshine Love China !
K s h i n e
*/

2、带参主函数,传入文件名,写入我喜欢的数字23,再读出数值

//标准IO库函数
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>

//系统io
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <errno.h>

int main(int argc, char** argv)
{
	if(argc<2)
	{
		printf("输入出错\t");
		fprintf(stderr,"%s\n",strerror(errno));
		return 0;
	}

	int fd = open(argv[1],O_RDWR | O_TRUNC |O_CREAT ,0664);
	
	//-------------
	int value=23;
	if(write(fd,&value,sizeof(int))==-1)
	{
		printf("写 出错\t");
		fprintf(stderr,"%s\n",strerror(errno));
		return 0;
	}
	printf("write success!\n");
	//------------
	int value2=0;
	//lseek(fd,-2,SEEK_CUR);
	lseek(fd,0,SEEK_SET);
	printf("offset success!\n");
	if(read(fd,&value2,sizeof(int))==-1)
	{
		printf("读错误\t");
		fprintf(stderr,"%s\n",strerror(errno));
		return 0;
	}
	printf("read success!\n");
	printf("the value is : %d\n",value2);	
	close(fd);
	return 0;
}


运行结果:
kshine@kshine-virtual-machine:/usr/Kshine/lsd/0817$ ./D2 Kshine Love China
write success!
offset success!
read success!
the value is : 23

3、作业1:带参主函数,传入IP和PORT的等式,并进行分解输出

/*
2017年08月17日
第一题:
IP = 192.168.1.1
PORT = 8080
要求通过 系统IO 
分解出192.168.1.1存入 第一个字符数组中,
分解出8080 存入第二个字符数组中。
最后打印输出。
*/

//标准库
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
//系统库
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <errno.h>

int main(int argc,char* argv[])
{
	//程序执行命令,无后接的字符串
	if(argc>=2)
	{
		printf("执行命令错误\n");
		//fprintf(stderr,"%s\n",strerror(errno));
		return 0;
	}

	//用户输入
	int i=0;
	char str1[30]="";
	char str2[30]="";
	printf("请输入字符串1\n");//行缓存,\n刷新输出
	//scanf("%s%s",str1,str2);
	gets(str1);
	printf("请输入字符串2\n");
	gets(str2);
	//系统IO的操作----------------------------
	
	//1、打开文件
	//int open(const char *pathname, int flags, mode_t mode);
	int fd = open("hw1",O_RDWR | O_CREAT | O_TRUNC,0664); //通过文件符打开
	//权限 S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH
	printf("open success!\n");//行缓存 通过\n 刷新输出
	
	//2、写入数据		
	if(write(fd,str1,sizeof(char)*30) == -1 || \
	   write(fd,str2,sizeof(char)*30) == -1 )
	{
		printf("写出错!\n");
		fprintf(stderr,"%s\n",strerror(errno));
		return 0;
	}
	printf("write success!\n");
	//3、偏移文件操作指针的位置
	
	//lseek(fd,0,SEEK_SET);
	FILE* fp =fdopen(fd,"r+");
				//r+和w+都是读写,但是w+会清空已有文件
	fseek(fp,0,SEEK_SET);
	//4、读取数据
	#if 1
	i=0;
	char ch=0;
	int flag=0,count=0;//用于切换存储的数组
	for(;;)
	{
		//读取单个字符
		if(read(fd,&ch,sizeof(char)) == 0 )
		{
			break;
		}
		
		if((ch <= '9'&& ch >= '0')|| ch== '.')//如果是数字或者.
		{
			if(ch == '.' && count>=0  )//记录‘.’的个数
			{
				count++;
			}
			if(flag==0)//第一个字符串存储
			{
				str1[i]=ch;
				i++;
				continue;
			}
			if(flag >= 1)//1 第二个字符开始 2
			{
				flag=2;
				str2[i]=ch;
				i++;
				continue;
			}
			
			continue;
			
		}
		//已经经过3个. 到这里还不是数字
		else if(count==3)
		{
			count=-10;//结束计数
			str1[i]='\0';
			flag=1;//切换到字符串2
			i=0;		
		}
		
		if(flag==2)//已经在str2中写了数字了,这里不是数字,则端口的数值结束
		{
			str2[i]='\0';
			break;
		}
	}
	printf("read success!\n");
	#endif
	//5、关闭文件
	close(fd);
	printf("close success!\n");

	//6、输出
	printf("%s\n",str1);
	printf("%s\n",str2);
	return 0;
}


运行结果:

kshine@kshine-virtual-machine:/usr/Kshine/lsd/0817$ ./H1
请输入字符串1
Kshine
请输入字符串2
Love
open success!
write success!
read success!
close success!
Kshine
Love

kshine@kshine-virtual-machine:/usr/Kshine/lsd/0817$ ./H1
请输入字符串1
ip = 192.168.1.1
请输入字符串2
port = 8080
open success!
write success!
read success!
close success!
192.168.1.1
8080


4、带参主函数,实现拷贝copy功能

/*
2017年08月17日
第二题
利用系统IO函数 
实现 cp 功能,
要求文件名1,
文件名2 
从main函数传入。
*/

//标准库
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
//系统库
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <errno.h>

int main(int argc,char* argv[])
{
        //程序执行命令
        if(argc<3)
        {
                printf("执行参数不够\n");
                //fprintf(stderr,"%s\n",strerror(errno));
                return 0;
        }

        
        //系统IO的操作---------------------------------------

	//1、打开文件
        //int open(const char *pathname, int flags, mode_t mode);

	int fd1 = open(argv[1],O_RDONLY,0664);//文件1,打开 读取 
	printf("open file1 success!\n");
	
        int fd2 = open(argv[2],O_WRONLY | O_CREAT | O_TRUNC,0664); //通过文件符打开

        //权限 S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH
        printf("open file2 success!\n");//行缓存 通过\n 刷新输出
                

	
	//2、单字符搬运
	char ch=0;
	while(1)
	{
		//读取单个字符
                if(read(fd1,&ch,sizeof(char)) == 0 )
                {
                        break;
                }
		write(fd2,&ch,sizeof(char));
	}

        printf("copy complete!\n");
	
	//3、关闭
	close(fd1);
	close(fd2);
        
        return 0;
}


运行结果:
kshine@kshine-virtual-machine:/usr/Kshine/lsd/0817$ gcc homework2.c -o H2 -Wall
kshine@kshine-virtual-machine:/usr/Kshine/lsd/0817$ ./H2 text newText
open file1 success!
open file2 success!
copy complete!


5、带参主函数,传入文件名,将九九乘法表的打印输出 重定向 到该文件中。

/*
2017年08月17日
第三题
通过重定向FILE *freopen(const char *path, const char *mode, FILE *stream);
把99乘法表打印输出到文件,
输出完成后复原重定向
在屏幕上打印write OK
*/

//标准IO库函数
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>

//系统io
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <errno.h>

int main(int argc ,char** argv)
{
	if(argc!=2)
	{
		fprintf(stderr,"命令参数个数错误\n");
		return 0;
	}
	//int creat(const char *pathname, mode_t mode);
	//创建文件
	int fd=creat(argv[1],0664);
	#if 1
	//重定向
	if(freopen(argv[1],"w",stdout)==NULL)
	{
		printf("重定向失败\n");
		return 0;
	}	
	#endif
	#if 0
	/* 重定向 */  
        if (-1 == dup2(fd,STDOUT_FILENO) ) {  
                printf("can't redirect fd error\n");  
                exit(1);  
        }  
	#endif

	//输出
	int i=1,j=1;
	printf("九九乘法表:\n");
	for(i=1;i<=9;i++)
	{
		for(j=1;j<=9;j++)
		{
			printf("%dx%d=%d\t",i,j,i*j);
		}
		printf("\n");
	}
	
	//还原重定向
	#if 1
	if(freopen("/dev/tty","w",stdout)==NULL)
        {
                printf("重定向失败\n");
                return 0;
        }

	#endif
	#if 0
	/* 恢复stdout */  
        if (-1 != dup2(fd,STDOUT_FILENO) ) {  
                printf("recover fd ok \n");  
  
                /* 恢复后,写入stdout应该向屏幕输出 */  
                write(STDOUT_FILENO,"stdout\n",7);  
        }  
	#endif

	//再次输出
	printf("write ok \n");
	//关闭
	close(fd);

	return 0;
}

kshine@kshine-virtual-machine:/usr/Kshine/lsd/0817$ gcc homework3.c -o H3 -Wall
kshine@kshine-virtual-machine:/usr/Kshine/lsd/0817$ ./H3 KKK3
write ok 

kshine@kshine-virtual-machine:/usr/Kshine/lsd/0817$ cat KKK3 
九九乘法表:
1x1=1	1x2=2	1x3=3	1x4=4	1x5=5	1x6=6	1x7=7	1x8=8	1x9=9	
2x1=2	2x2=4	2x3=6	2x4=8	2x5=10	2x6=12	2x7=14	2x8=16	2x9=18	
3x1=3	3x2=6	3x3=9	3x4=12	3x5=15	3x6=18	3x7=21	3x8=24	3x9=27	
4x1=4	4x2=8	4x3=12	4x4=16	4x5=20	4x6=24	4x7=28	4x8=32	4x9=36	
5x1=5	5x2=10	5x3=15	5x4=20	5x5=25	5x6=30	5x7=35	5x8=40	5x9=45	
6x1=6	6x2=12	6x3=18	6x4=24	6x5=30	6x6=36	6x7=42	6x8=48	6x9=54	
7x1=7	7x2=14	7x3=21	7x4=28	7x5=35	7x6=42	7x7=49	7x8=56	7x9=63	
8x1=8	8x2=16	8x3=24	8x4=32	8x5=40	8x6=48	8x7=56	8x8=64	8x9=72	
9x1=9	9x2=18	9x3=27	9x4=36	9x5=45	9x6=54	9x7=63	9x8=72	9x9=81

猜你喜欢

转载自blog.csdn.net/Kshine2017/article/details/83896359