69,370
社区成员
发帖
与我相关
我的任务
分享
#include <stdio.h>
#include <string.h>
#define READ_BUF_SIZE (100)
void main()
{
FILE *fp, *fp2;
unsigned char c,c1,c2;
char hexTab[]={'0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F'};
char bufRead[READ_BUF_SIZE + 1] = {0};
char bufWrite[READ_BUF_SIZE*2 + 1] ={0};
int i=0,j=0;
int iReadCnt =0;
fp=fopen("1.txt","rb");
fp2=fopen("11.txt","w+");
fseek(fp,0,SEEK_END);
int len = ftell(fp);
printf("\n len=%d\n",len);
//getchar();
rewind(fp);
while(!feof(fp))
{
memset(bufRead,0,READ_BUF_SIZE + 1);
if(len >= READ_BUF_SIZE)
{
iReadCnt = fread(bufRead, 1, READ_BUF_SIZE, fp);
len -= READ_BUF_SIZE;
}
else
{
iReadCnt = fread(bufRead, 1, len, fp);
}
for(i=0;i< iReadCnt;i++)
{
c=bufRead[i];
bufWrite[i*2]=hexTab[c>>4];
bufWrite[i*2+1]=hexTab[c & 0x0f];
//c1=hexTab[c>>4];
//fputc(c1,fp2);
//c1=hexTab[c & 0x0f];
//fputc(c1,fp2);
}
printf("\n readCnt=%d,",iReadCnt);
iReadCnt=fputs(bufWrite,fp2);
printf(" WriteCnt=%d\n\n",iReadCnt);
fputc('\n',fp2);
printf("\n %d:%s\n", j, bufWrite);
memset(bufWrite,0,READ_BUF_SIZE*2 + 1);
j++;
}
fclose(fp);
fclose(fp2);
printf("\ndone,j=%d!\n",j);
}
/* feof example: byte counter */
#include <stdio.h>
int main ()
{
FILE * pFile;
long n = 0;
pFile = fopen ("myfile.txt","rb");
if (pFile==NULL) perror ("Error opening file");
else
{
while (!feof(pFile)) {
fgetc (pFile);
n++;
}
fclose (pFile);
printf ("Total number of bytes: %d\n", n-1);
}
return 0;
}