#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#define HEAD ""
#define TAIL ""
char * shift(char *tmp, int size)
{
int a = size / 8;
int i,b,c = -1,sum = 0;
char *ptmp = tmp;
char ch;
char tmpp[6] = {'0','x'};
FILE *FDW = fopen("./hello.txt","a+");
for (i = 0, b = 8; i < 4; b /= 2,i++)
{ ch = ptmp[i];
c = atoi(&ch);
printf("=====%d\n",c);
sum += c * b;
}
printf("%d\n",sum);
if (sum < 10)
{
tmpp[2] = sum + '0';
} // sprintf(&tmpp[2],"%c",sum);
else
{
tmpp[2] = 'a' + (sum - 10);
}
sum = 0;
for (i = 4, b = 8; i < 8; b /= 2,i++)
{
ch = ptmp[i];
c = atoi(&ch);
printf("%d\n",c);
sum += c * b;
}
if (sum < 10)
tmpp[3] = sum + '0';
// sprintf(&tmpp[2],"%c",sum);
else
{
tmpp[3] = 'a' + (sum - 10);
}
tmpp[4] = ',';
char *hh = tmpp;
//strncpy(hh,tmpp,5);
printf("%s\n",hh);
fputs(hh,FDW);
fclose(FDW);
return hh;
}
int function_print(char *buf, int size)
{
char *tmp = NULL;
char *tmp2 = NULL;
char *pbuf = NULL;
char *string_buf = NULL;
pbuf = buf;
pbuf[strlen(pbuf) - 1] = '\0';
tmp = (char *)malloc(9);
char *tmp1 = NULL;
//tmp1 = (char *)malloc(9);
int count = 0;
FILE *FDW = fopen("./jian.txt","a+");
while (count < 3)
{
strncpy(tmp,pbuf + count * 8,8);
printf("%s,%d\n",tmp,strlen(tmp));
shift(tmp,size);
count++;
//pbuf += 8;
}
printf("=====================");
free(tmp);
fclose(FDW);
tmp = NULL;
return 1;
}
int tmpfile_write(FILE *FW)
{
char ch;
int count = 0;
FILE *fdr = fopen("./hello.txt","r");
// FILE *fdw = fdopen(FW,"a+");
FILE *fdw = freopen("./jian.txt","a+",FW);
while(EOF != (ch = fgetc(fdr)))
{
fputc(ch,fdw);
// fread(&ch,1,1,fdr);
if (',' == ch)
{
count++;
if (count == 3)
{
fputc('\n',fdw);
count = 0;
continue;
}
}
}
//fputc('\n',fdw);
fputc('}', fdw);
fclose(fdr);
//fclose(fdw);
return 0;
}
int main(void)
{
int count = -1;
int count1 = -1;
char ch;
char *buf = (char *)malloc(40);
memset(buf,0,40);
//char buf[40] = {0};
FILE *fdr = fopen("./huang.txt","r");
if (fdr == NULL)
{
perror("fopen");
return -1;
}
FILE *fdw = fopen("./jian.txt","w");
if (fdw == NULL)
{
perror("fopen");
return -2;
}
printf("====\n");
int flag = 0;
char *rec_string = NULL;
char *store_string = " ";
while(fgets(buf,40,fdr) != NULL)
{
// buf[strlen(buf) - 1] = '\0';
printf("%s",buf);
if (!strncmp(buf,HEAD,8) || !strncmp(buf,TAIL,8))
{
fputs("/*",fdw);
flag = 1;
continue;
}
if (flag == 1)
{
if (NULL == strchr(buf, '*'))
{
//count1 = strlen(buf);
fputs(buf,fdw);
printf("***\n");
if (1 != function_print(buf,24))
{
printf("function_print failed!\n");
return -1;
}
memset(buf,0,40);
}
else
{
buf[strlen(buf) - 1] = '\0';
strcat(buf,"/\n");
fputs(buf,fdw);
if (0 != tmpfile_write(fdw))
{
printf("read/write hello.txt failed!");
break;
}
flag = 0;
memset(buf,0,40);
}
}
}
/* while(EOF != (ch = fgetc(fdr))) //!feof(fdr))
{
printf("%c\n",ch);
fputc(ch,fdw);
}*/
fclose(fdr);
fclose(fdw);
free(buf);
buf = NULL;
return 0;
}