printf结果是意外的

Abh*_*ngh 2 c linux gcc

我有以下代码:

char *buff = (char *)malloc(sizeof(char)*120);
char *ts_t = (char *)malloc(sizeof(char)*80);

/*Generating invalid test case for timestamp.*/
printf("Out side of ts.\n");

 while(fgets (ts_t, 80, fp_ts)!=NULL) 
 {
      printf("Inside ts.\n");
      //ts_t[strlen(ts_t)-1] = '\0';
      memset(buff,0,120);       
      memcpy(buff,ts_t,strlen(ts_t)-1);

      printf("The ts is :%s",buff);

      fprintf(fp_test_case,"%s\t%s\t%s%d\t%s\t%s\t%s%04d\n",buff,ver,txn,digit_generate(num),aspid,uid,"Test",count_testCase);

      print_description( fp_description,ts_t,count_testCase,"TimeStamp");
      count_testCase++;
    }

printf("Invalid Time stamp case generated.\n");
Run Code Online (Sandbox Code Playgroud)

预期的是:

在外面.
里面的ts.
ts是:05-26-2015T13:53:33.509
内部ts.
ts是:05-26-2015T13:53:33.509

但它打印为:

在ts
里面.
里面的ts.
Inside ts.:05-26-2015T13:53:33.509
Inside ts.:2015-26-05T13:53:33.509

我在这做错了什么?谢谢.

Ign*_*ams 8

您的文件具有DOS样式的换行符("\ r \n"),但您在使用*nix样式换行符("\n")的系统上运行代码.额外的CR是将终端光标移回第一列,然后打印另一行.从行中剥离CR或以可处理备用换行样式的模式打开文件.