在.txt文件中的每一行的末尾删除^ M。 [英] Remove ^M at the end of each line in .txt file.

查看:169
本文介绍了在.txt文件中的每一行的末尾删除^ M。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下

^XA^M
^SZ2^JMA^M
^MCY^PMN^M
^PW822~JSN^M
^JZY^M
^LH0,0^LRN^M
^XZ^M
^XA^M
^FO350,95^M
^BY4^BCB,220,N,N,N,A^FD12345^FS
^FT605,700^M
^A0B,40,40^M
^FB600,1,0,R,0^M
^FD12345^FS
^FT282,1160^M
^A0B,28,39^FDBilka Tilst afdelingen^FS
^FT320,1160^M
^A0B,28,39^FDAgerøvej 7^FS
^FT358,1160^M
^A0B,28,39^FDPort 39^FS
^FT396,1160^M
^A0B,28,39^FDTilst^FS
^FT434,1160^M
^A0B,28,39^FDFLD_COUNTY_FLD^FS^M
^FT472,1160^M
^A0B,28,39^FD8381^FS
^FT510,1160^M
^A0B,28,39^FDFLD_COUNTRY_FLD^FS^M
^FT548,1160^M
^A0B,28,39^FDFLD_ORDERTEL_FLD^FS^M
^FO660,100^M
^GB0,720,2^FS^M
^FO750,100^M
^GB0,720,2^FS^M
^FO660,820^M
^GB92,0,2^FS^M
^FO660,100^M
^GB92,0,2^FS^M
^FT680,810^M
^A0B,18,23^FDDELIVERY INSTRUCTIONS:^FS^M
^FT700,810^M
^A0B,18,25^FDFLD_SPECIALINST1_FLD^FS^M
^FT720,810^M
^A0B,18,25^FDFLD_SPECIALINST2_FLD^FS^M
^FT200,1160^M
^A0B,80,90^FB1160,2,0,L,0^FDFLD_SERVICETEXT_FLD^FS^M
^FT780,1160^M
^A0B,18,20^FDSender's address: ^FS^M

我试图消除^ M在每行的结尾,并在C以下

I am trying to remove ^M at the end of each line, and have the following in C

GLOBAL BOOLEAN CARRIER_CONSIGNOR_SIM_ReplaceZPL(CARRIER_CONSIGNOR_SIM_ORDER_TYPE* Header)
{
  #define MAX_FILE_LEN 30

  char filename[MAX_FILE_LEN];
  snprintf(filename, MAX_FILE_LEN, "%s.zpl", Header->OrderNumber);

  FILE *file;
  FILE *write;
  char line  [256];
  char linec [256];

  file = fopen( filename, "r");
  write = fopen( filename, "r+");

  if( file != NULL )
  {
    while ( fgets ( line, sizeof line, file ) != NULL ) /* read a line */
    {
      strcpy(linec, line);
      fprintf (write , linec);
    }
    /* Close both files */
    fclose (write);
    fclose (file);
    printf("REPLACED LABEL in %s\n", filename);
    return ( TRUE );
  }

 return ( FALSE );
}/*CARRIER_CONSIGNOR_SIM_ReplaceZPL*/

我曾尝试做以下为RB +,R + B和Rb来写= FOPEN但无论这将消除^ M什么是解决这个问题的最好办法。

I have tried doing the following to "rb+", "r+b and rb to the write = fopen but neither of this will remove ^M. What is the best way around this.

推荐答案

您只需通过删除AY \\ r \\ n缩短字符串结尾,例如:人物变化:

You can just shorten the string by deleting ay \r or \n characters at the end, e.g. change:

  ...
  strcpy(linec, line);
  ...

  int len;
  ...
  strcpy(linec, line);
  len = strlen(linec);                   // get length of string
  while (len > 0)                        // while string not empty
  {                                      // if last char is \r or \n
      if (linec[len - 1] == '\r' || linec[len - 1] == '\n')
      {
          linec[len - 1] = '\0';         // delete it
          len--;
      }
      else                               // otherwise we found the last "real" character
          break;
  }
  ...

请注意,当你打印字符串,您将需要添加一个换行符,例如用

Note that when you print the string you will need to add a line feed, e.g. use

fprintf(write, "%s\n", linec);

这篇关于在.txt文件中的每一行的末尾删除^ M。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆