My test file

 

#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;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值