在转换之前首先得了解什么是文件IO,文件IO可分为系统IO和标准IO,本文具体以系统IO实现该功能。
Linux“系统IO”的使用
1.打开文件open
2.操作文件
想什么呢?自己去百度!直接上代码
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
int main()
{
int fd = open(const char* pathname,int flags); //文件名 打开方式
if(-1 == fd) //反过来写的作用不用多说了吧 不懂百度
{
perror("open fail");
return -1;
}
printf("open success!\n");
int fd_dest = open("文件名",O_CREAT|O_EXCL|O_RDWR,0777);
if(-1 == fd_dest)
{
perror("open failed");
return -1;
}
while(1)
{
unsigned char c;
unsigned char d = 0;
for(int i=0;i<2;i++)
{
if(read(fd,&c,1)==0) //每次读写一个字节
{
close(fd);
close(fd_dest);
return 0;
}
if(c == ' ' || c== '\n' ||c == '\r' ) // c==0xa || c==0xd
{
i--;
continue;
}
else if(c >= '0' && c<='9')
{
c = c - '0';
}
else if(c >= 'a' && c <= 'f')
{
c = c - 'a';
c = c + 10;
}
else if(c >= 'A' && c <= 'F')
{
c = c - 'A';
c = c + 10;
}
else //文件中有不符和的字符直接退出
{
printf("error char;\n");
return 0;
}
d <<= 4;
d |= c;
}
printf("%x ",d);//可加打印调试
write(fd_dest,&d,1);
}
close(fd);
close(fd_dest);
return 0;
}
新手写代码,请多改正