zoukankan      html  css  js  c++  java
  • 2017-2018-1 20155212 《信息安全系统设计基础》第4周学习总结

    2017-2018-1 20155212 《信息安全系统设计基础》第4周学习总结

    MyOD

    1. 参考教材第十章内容
    2. 用Linux IO相关系统调用编写myod.c 用myod XXX实现Linux下od -tx -tc XXX的功能,注意XXX是文件名,通过命令行传入,不要让用户输入文件名
    3. 不要把代码都写入main函数中
    4. 要分模块,不要把代码都写入一个.c中
    5. 提交测试代码和运行结果截图, 提交调试过程截图,要全屏,包含自己的学号信息
    • 解题步骤
      • 增加头文件
    #include <sys/types.h>
    #include <sys/stat.h>
    #include <fcntl.h>
    #include <unistd.h>
    
    - 修改```File *fp```为 ```int od```
    - 修改```fopen()```为```open()```
    - 修改```fget()```为```read()```
    
    • main.c
    #include <stdio.h>
    #include <string.h>
    #include <sys/types.h>
    #include <sys/stat.h>
    #include <fcntl.h>
    #include <unistd.h>
    #define MAXDATA 1024
    
    int main(int args, char **argv)
    {
        int od;
    	int reclen=1;
        long chnum;
        if((od=open(argv[1], O_RDONLY))==-1)
    	{
    		perror(argv[1]);
    		exit(1);
    	}
    
        char data[MAXDATA];
        int i=0;
        char ch;
    
        while(read(od, &ch,reclen)==reclen)
        {
            if(i<MAXDATA)
            {
                data[i++]=(char)ch;
            }
        }
        chnum=i;
        odfunction(chnum, data);
    	close(od);
    	return 0;
    }
    
    
    • odfunction.c
    void odfunction(long num, char data[])
    {
    	long i, j;
    	for(i=0; i<1024; i=i+4)
        {
            if(i%16==0)
            {
                printf("
    %07o		", i);
            }
            printf(" %02x%02x%02x%02x	", data[i+3], data[i+2], data[i+1], data[i]);
            if((i+4)%16==0)
            {
                printf("
    	  ");
                for (j = i - 12; j < i+4 ; j++)
                {
                    if ((int)data[j] == 10)
                    {
                        printf(" \");
                        printf("n ");
                    }
                    else
                    {
                        printf("  %c ", data[j]);
                    }
                }
            }
            if (data[i+4] ==0) 
            {
             	printf("
    	  ");
                for (int j = i-i%16; data[j-3] != 0; j++) 
                {
                     if ((int) data[j] == 10) 
                     {
                     	printf(" \");
                        printf("n ");
                     } 
                     else
                   	 {
                        printf("  %c ", data[j]);
                   	 }
                }
                     break;
            }
        }
        printf("
    %07o
    ", num);
    }
    
    • 问题与解决

      • Ubuntu虚拟机无法正常启动,启动后黑屏,提示“硬件不匹配”
        • 刚开始以为是Ubuntu系统出了问题,于是恢复快照,回到之前的备份(当时部分代码忘记git,很尴尬)。恢复完发现还是不行,最后发现是因为我将VMWare Station升级到了14版本,与12版本中创建的虚拟机系统不兼容。
      • 调试一直正常,没想到突然make后出现如下情况
        • 在网上找了很多情况,试了都没有用。最后发现错误原因在于我不小心删了函数名一个字母。
    • 运行结果截图

    MyHead

    • head命令的分析
    1. 命令格式: head [参数]... [文件]...
    2. 命令功能:head 用来显示档案的开头至标准输出中,默认head命令打印其相应文件的开头10行。
    • 伪代码
    char ch;
    while(能读取到一个字符给ch && 换行记数<10)
    {
        if(ch!=换行符)
        {
            打印ch;
        }
        else
        {
            换行计数++;
            打印ch;
        }
    }
    
    • 产品代码
    #include <stdio.h>
    #include <string.h>
    #include <sys/types.h>
    #include <sys/stat.h>
    #include <fcntl.h>
    #include <unistd.h>
    #define MAXDATA 1024
    
    int main(int args, char **argv)
    {
        int file;
        int flag=0;
        if((file=open(argv[1], O_RDONLY))==-1)
    	{
    		perror(argv[1]);
    		exit(1);
    	}
    
        char data[MAXDATA];
        int i=0;
        char ch;
    
        while(read(file, &ch, 1)==1 && flag<10)
        {
        	if(ch!='
    ')
            {
              	printf("%c", ch);
            }
            else
            {
            	flag++;
            	printf("%c", ch);
            }
        }
    	close(file);
    	return 0;
    }
    
    • 测试代码
    1
    12
    123
    1234
    12345
    123456
    1234567
    12345678
    123456789
    1234567890
    12345678901
    123456789012
    1234567890123
    12345678901234
    123456789012345
    1234567890123456
    
    1
    12
    123
    1234
    12345
    123456
    1234567
    12345678
    
    • 运行结果截图

    MyTail

    • tail命令分析
    1. 命令格式:tail[必要参数][选择参数][文件]
    2. 命令功能:用于显示指定文件末尾内容,不指定文件时,作为输入信息进行处理。常用查看日志文件。
    • 伪代码
    char 数据存储[MAX][MAX];
    char ch;
    int 行计数=0, 行字符计数=0;
    while(能读取到一个字符给ch && 行记数<MAX)
    {
        if(ch!=换行符 && 行字符计数<MAX)
        {
            数据存储[行计数][行字符计数++]=ch;
        }
        else
        {
            数据存储[i][j]='';
            行计数++;
            行字符计数置0;
        }
    }
    for(int i=行计数-10; i>=0 && i<行计数; i++)
    {
        打印数据存储[i];
    }
    if(行计数<10)
    {
        逐行打印;
    }
    
    • 产品代码
    #include <stdio.h>
    #include <string.h>
    #include <sys/types.h>
    #include <sys/stat.h>
    #include <fcntl.h>
    #include <unistd.h>
    #define MAXDATA 1024
    
    int main(int args, char **argv)
    {
        int file;
        if((file=open(argv[1], O_RDONLY))==-1)
    	{
    		perror(argv[1]);
    		exit(1);
    	}
    
        char data[MAXDATA][MAXDATA];
        int i=0, j=0;
        char ch;
    
        while(read(file, &ch, 1)==1 && i<MAXDATA)
        {
        	if(ch!='
    ')
            {
              	data[i][j++]=(char)ch;
            }
            else
            {
            	data[i][j]='';
    			i++;
    			j=0;
            }
        }
    
    	int k;
    	for(k=i-10; k>=0&&k<i; k++)
    	{
    		puts(data[k]);
    	}
    	if(i<10)
    	{
    		for(k=0;k<i;k++)
    		{
    			puts(data[k]);
    		}
    	}
    	close(file);
    	return 0;
    }
    
    • 测试代码
    01234567890123456789
    0123456789012345678
    012345678901234567
    01234567890123456
    0123456789012345
    012345678901234
    01234567890123
    0123456789012
    012345678901
    01234567890
    0123456789
    012345678
    01234567
    0123456
    012345
    01234
    0123
    012
    01
    0
    
    012345678
    01234567
    0123456
    012345
    01234
    0123
    012
    01
    0
    
    • 运行结果截图
  • 相关阅读:
    python面试的100题(2)
    面试题目和地址
    python面试的100题(1)
    no module named系列问题解决
    ubuntu16.04无法打开终端
    Reinforcement Learning,微信公众号:DRL学习
    Java中的I/O操作File
    Java中的Date时间转换【SimpleDateFormat (parse和format)】和Calendar日历表
    重写equals方法
    Java中栈,堆,常量池的简单理解
  • 原文地址:https://www.cnblogs.com/dky20155212/p/7675135.html
Copyright © 2011-2022 走看看