(1)说明
//把输入文件名为a.txt,(可以新建一个空文件,命名为b.txt)输出文件为b.txt
//在linux下把a.txt ,b.txt ,test三个文件放在一起,在linux环境下运行test即可。
(2)Makefile编写如下:
test:test.o
gcc -o test test.o
test.o:
gcc -c test.c
clean:
rm test.o
(3)test.c相关的源码如下:
#include
#include
int main()
{
FILE *pf = fopen("a.txt", "r");
if(pf == NULL) {
printf("open a.txt file failed!\n");
exit(0);
}
FILE *pf2 = fopen("b.txt", "w");
if(pf2 == NULL) {
printf("open b.txt file failed!\n");
fclose(pf);
exit(0);
}
unsigned char ch;
while(!feof(pf)) {
ch = fgetc(pf);
if(ch != NULL)
fprintf(pf2,"0x%02x,",ch);
}
fclose(pf2);
fclose(pf);
return 0;
}