The second week of the election do (myod)

02. The second week myod (OPTIONAL)

Experimental requirements:

  • C review of the file processing

  • Write myod.c achieved under Linux od -tx -tc XXX function with myod XXX

  • main separate from other, making static and dynamic libraries

  • Writing the Makefile

  • Submit the results of the test code and run shots, shots submit debugging process, to full-screen, contains its own student number information

  • Published a blog in blog Park, problems encountered and solutions focus on writing process

Linux od Detailed command:

  • 1. Function
    • od command is used to specify the contents of the file are usually used to display or viewing files can not be displayed directly in the terminal's character in octal, decimal, hexadecimal display, floating point format or ASCII character encoding mode. od command system default display is octal name from Octal Dump. Common file as a text file and binary files. od command is mainly used to view the value stored in a binary file, according to the data format specified file and interpret the output, whether it is IEEE754 floating point format or ASCII code, the od command can output their values ​​on demand.
  • 2. Command Format
    • od [<option> <parameter>] [<filename>]
  • 3. Command Options
1. A<RADIX>,--address-radix=RADIX:选择以何种基数表示地址偏移;
2. -j<BYTES>,--skip-bytes=BYTES:跳过指定数目的字节;
3. -N,--read-bytes=BYTES:输出指定字节数;
-S<BYTES>, --strings[=BYTES]:输出长度不小于指定字节数的字符串;
4. -v,--output-duplicates:输出时不省略重复的数据; 
5. -w<BYTES>,--width=<BYTES>:设置每行显示的字节数,od默认每行显示16字节。如果选项--width不跟数字,默认显示32字节;
6. -t<TYPE>,--format=TYPE:指定输出格式,格式包括a、c、d、f、o、u和x,各含义如下:
 a:具名字符;
 c:ASCII字符或者反斜杠;
 d[SIZE]:十进制,正负数都包含,SIZE字节组成一个十进制整数;
 f[SIZE]:浮点,SIZE字节组成一个浮点数;
 o[SIZE]:八进制,SIZE字节组成一个八进制数;
 u[SIZE]:无符号十进制,只包含正数,SIZE字节组成一个无符号十进制整数;
 x[SIZE]:十六进制,SIZE字节为单位以十六进制输出,即输出时一列包含SIZE字节。
--help:在线帮助; 
--version:显示版本信息。

Experimental Procedure:

1. The code section:

  • ascii.c (ASCII characters):
#include "head.h"
#include <stdio.h>
void ascii(char *name)
{
  FILE *fp;
  char ch;
    fp=fopen(name,"r");
    ch=fgetc(fp);
  printf("output the ascii:\n");

  while(ch!=EOF)
  {
     if(ch=='\n')
        printf("\n");
     else
        printf("%4d",ch);
        ch=fgetc(fp);
  }
  fclose(fp);

}
  • hex.c (hexadecimal display output):
#include "head.h"
#include <stdio.h>
void hex(char *name)
{
  FILE *fp;
  char ch;
  printf("output the hex:\n");
  fp=fopen(name,"r");
  ch=fgetc(fp);
  while(ch!=EOF)
  {
    if(ch=='\n')
    printf("\n");
    else
    printf("%4x",ch);
    ch=fgetc(fp);
   }
   fclose(fp);
}
  • MYoD.c:
#include "head.h"
#include <stdio.h>
void main()
{ 
  char name[50];
  printf("please input the txtname:");
  scanf("%s",name);
  ascii(name);
  hex(name);
}

2. Process section:

Achieved under Linux od -tx -tc XXX with myod XXX function code run shot:

a static library implementation process and screenshots:

ar cr MYoD.a hex.o ascii.o
gcc -o MYoD MYoD.c ./MYoD.a


DLL implementation process and screenshots:

gcc -shared -fpic -o MYoD.so ascii.c hex.c
gcc -o MYoD2 MYoD.c ./MYoD.so

manufacture and running the makefile screenshots:

testmymath:MYoD.o hex.o ascii.o
     gcc MYoD.o hex.o ascii.o -o testmymath
MYoD.o:MYoD.c head.h
    gcc -c MYoD.c
hex.o:hex.c head.h
    gcc -c hex.c
ascii.o:ascii.c head.h
    gcc -c ascii.c

3. When the problem:

An error occurred while gcc link library.

By examining the code formatting problems, been to modifications.

gcc -shared -fpic -o MYoD.so ascii.c hex.c

Guess you like

Origin www.cnblogs.com/20199304lbs/p/11923367.html