fileconv.cpp

来自「关于TPM的一些应用」· C++ 代码 · 共 44 行

CPP
44
字号
#include <stdio.h>
#include <string.h>
#include <stdlib.h>

int main(int argc,char *argv[])
{
	int idx = 0,si;
	char c, val[3], sOut[100];
	FILE *pInFile,*pOutFile;

	if(argc != 2)
	{
		printf("Wrong number of args %d\n",argc-1);
		return -1;
	}
	pInFile = fopen(argv[1],"rb");
	strcpy(sOut,argv[1]);
	strcat(sOut,".txt");
	pOutFile = fopen(sOut,"w");
	if (pInFile == NULL || pOutFile == NULL)
	{
		printf("Cannot open file\n");
		return -1;
	}
	
	fputc('{',pOutFile);
	c = fgetc(pInFile);
	while(1)
	{
      itoa((unsigned char)c,val,10);
	  for(si=0;si<strlen(val);si++)
		fputc(val[si],pOutFile);
	  c = fgetc(pInFile);
	  //since it's a binary file all values including EOF are valid
	  if(c == EOF && feof(pInFile)) break;
	  fputc(',',pOutFile);
	  idx++;
    }
	fputc('}',pOutFile);
	fclose(pInFile);
	fclose(pOutFile);
	return 0;
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?