none
关于字符串解析的小程序,总是不对,怎么回事? RRS feed

  • 问题

  • // Test.cpp: 主项目文件。
    #include "stdafx.h"
    #include <stdio.h>
    #include <string.h>
    #include <stdlib.h>
    using namespace System;
    int CheckPart_Equal_0_CheckSum_Equal_1 = -1; //标志数,属于CheckPart部分,则为0;属于CheckSum部分,则为1。初始值为-1
    char CheckPart;         //前面的CheckPart部分
    char CheckSum;         //后面的CheckSum部分
    int ProcessEveryByte (const char *ReceivedChar)
    {
     switch (*ReceivedChar)
     {
      case '\n':
       CheckPart_Equal_0_CheckSum_Equal_1 = -1; strcpy (&CheckPart, ""); strcpy (&CheckSum, "");  break;
      case '$':
       CheckPart_Equal_0_CheckSum_Equal_1 = 0;  strcpy (&CheckPart, ""); break;
      case '*':
       CheckPart_Equal_0_CheckSum_Equal_1 = 1;  strcpy (&CheckSum, "");  break;
      default://如果收到的字符,不是 \n $ *等等
       printf ("\nCheckPart_Equal_0_CheckSum_Equal_1 Is : %d\n", CheckPart_Equal_0_CheckSum_Equal_1);
       printf ("\nReceivedChar Is: \t%s", ReceivedChar);
       if (1 == CheckPart_Equal_0_CheckSum_Equal_1)
       {
        strcat (&CheckSum, ReceivedChar);
        printf ("\ndefault Part: CheckPart Is: \t%s", &CheckPart); printf ("\ndefault Part: CheckSum Is: \t%s", &CheckSum); break;
       }
       if (0 == CheckPart_Equal_0_CheckSum_Equal_1)
       {
        strcat (&CheckPart, ReceivedChar);
        printf ("\ndefault Part: CheckPart Is: \t%s", &CheckPart); printf ("\ndefault Part: CheckSum Is: \t%s", &CheckSum); break;
       }
     }
     return 1;
    }
    int main(array<System::String ^> ^args)
    {
     char ReceivedChar;
     ProcessEveryByte ("a"); ProcessEveryByte ("b"); ProcessEveryByte ("c"); ProcessEveryByte ("d");
     ProcessEveryByte ("$");
     ProcessEveryByte ("0"); ProcessEveryByte ("1"); ProcessEveryByte ("2"); ProcessEveryByte ("3"); ProcessEveryByte ("4");
     ProcessEveryByte ("5"); ProcessEveryByte ("6"); ProcessEveryByte ("7"); ProcessEveryByte ("8"); ProcessEveryByte ("9");
     ProcessEveryByte ("*");
     ProcessEveryByte ("A"); ProcessEveryByte ("B"); ProcessEveryByte ("C"); ProcessEveryByte ("D"); ProcessEveryByte ("E");
     ProcessEveryByte ("F"); ProcessEveryByte ("G"); ProcessEveryByte ("H"); ProcessEveryByte ("I"); ProcessEveryByte ("J");
     system("PAUSE");
     return 0;
    }


    什么?个性签名?哼!打死我也不签。既然你们都不觉得我聪明,那就叫我BenQ吧。好歹也是国际大品牌。

    2019年4月17日 1:34

答案

  • 你好,

    我在VS2017里面测过你的代码,如果你把CheckPart 和 CheckSum 定义成字符数组,问题就解决了。我的测试代码如下:

    #include "stdafx.h"
    #include <stdio.h>
    #include <string.h>
    #include <stdlib.h>
    
    using namespace System;
    int CheckPart_Equal_0_CheckSum_Equal_1 = -1; //
    char CheckPart[11] = "";         //
    char CheckSum[11] = "";         //
    
    int ProcessEveryByte(const char *ReceivedChar)
    {
    	switch (*ReceivedChar) 
    	{
    	case '\n':
    		CheckPart_Equal_0_CheckSum_Equal_1 = -1; strcpy_s(CheckPart, 1,""); 
    		strcpy_s(CheckSum, 1, "");  
    		break;
    	case '$':
    		CheckPart_Equal_0_CheckSum_Equal_1 = 0;  strcpy_s(CheckPart, 1,""); break;
    	case '*':
    		CheckPart_Equal_0_CheckSum_Equal_1 = 1;  strcpy_s(CheckSum, 1,"");  break;
    	default:// not \n $  etc
    		printf("\nCheckPart_Equal_0_CheckSum_Equal_1 Is : %d\n", CheckPart_Equal_0_CheckSum_Equal_1);
    		printf("\nReceivedChar Is: \t%s", ReceivedChar);
    		if (1 == CheckPart_Equal_0_CheckSum_Equal_1)
    		{
    			strcat(CheckSum, ReceivedChar);
    			printf("\ndefault Part: CheckPart Is: \t%s", CheckPart); printf("\ndefault Part: CheckSum Is: \t%s", CheckSum); break;
    		}
    		if (0 == CheckPart_Equal_0_CheckSum_Equal_1)
    		{
    			strcat(CheckPart, ReceivedChar);
    			printf("\ndefault Part: CheckPart Is: \t%s", CheckPart); printf("\ndefault Part: CheckSum Is: \t%s", CheckSum); break;
    		}
    	}
    	return 1;
    }
    
    int main(array<System::String ^> ^args)
    {
    	char ReceivedChar;
    	ProcessEveryByte("a"); ProcessEveryByte("b"); ProcessEveryByte("c"); ProcessEveryByte("d");
    	ProcessEveryByte("$");
    	ProcessEveryByte("0"); ProcessEveryByte("1"); ProcessEveryByte("2"); ProcessEveryByte("3"); ProcessEveryByte("4");
    	ProcessEveryByte("5"); ProcessEveryByte("6"); ProcessEveryByte("7"); ProcessEveryByte("8"); ProcessEveryByte("9");
    	ProcessEveryByte("*");
    	ProcessEveryByte("A"); ProcessEveryByte("B"); ProcessEveryByte("C"); ProcessEveryByte("D"); ProcessEveryByte("E");
    	ProcessEveryByte("F"); ProcessEveryByte("G"); ProcessEveryByte("H"); ProcessEveryByte("I"); ProcessEveryByte("J");
    	system("PAUSE");
    	return 0;
    }

    打印结果如下:

    ...
    
    ReceivedChar Is:        H
    default Part: CheckPart Is:     0123456789
    default Part: CheckSum Is:      ABCDEFGH
    CheckPart_Equal_0_CheckSum_Equal_1 Is : 1
    
    ReceivedChar Is:        I
    default Part: CheckPart Is:     0123456789
    default Part: CheckSum Is:      ABCDEFGHI
    CheckPart_Equal_0_CheckSum_Equal_1 Is : 1
    
    ReceivedChar Is:        J
    default Part: CheckPart Is:     0123456789
    default Part: CheckSum Is:      ABCDEFGHIJ

    Best Wishes,

    Jack Zhang


    MSDN Community Support Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.


    2019年4月17日 3:03
    版主
  • char类型为8bit数据,char CheckPart;char CheckSum; 占用了相邻的2个字节。

    strcat (&CheckPart, ReceivedChar)转换为字符串“0123456789”,被strcat (&CheckSum, ReceivedChar)转换的字符串“ABCDEFGHIJ”所覆盖,&CheckPart与&CheckSum差8bit(1个字节),

    因此CheckSum被显示为“ABCDEFGHIJ”,CheckPart被显示为“BCDEFGHIJ”。

    2019年4月19日 7:58

全部回复

  • 本程序非常简单,就是将一个字符串
    abcd$0123456789*ABCDEFGHIJ
    分成 0123456789 和 ABCDEFGHIJ 两个部分。
    不过,为什么显示的结果如下?
    default Part: CheckPart Is:     BCDEFGHIJ
    default Part: CheckSum Is:      ABCDEFGHIJ
    本段程序,在VC 2005 Express调试通过,可以直接复制过去查看显示结果


    什么?个性签名?哼!打死我也不签。既然你们都不觉得我聪明,那就叫我BenQ吧。好歹也是国际大品牌。

    2019年4月17日 1:35
  • 你好,

    我在VS2017里面测过你的代码,如果你把CheckPart 和 CheckSum 定义成字符数组,问题就解决了。我的测试代码如下:

    #include "stdafx.h"
    #include <stdio.h>
    #include <string.h>
    #include <stdlib.h>
    
    using namespace System;
    int CheckPart_Equal_0_CheckSum_Equal_1 = -1; //
    char CheckPart[11] = "";         //
    char CheckSum[11] = "";         //
    
    int ProcessEveryByte(const char *ReceivedChar)
    {
    	switch (*ReceivedChar) 
    	{
    	case '\n':
    		CheckPart_Equal_0_CheckSum_Equal_1 = -1; strcpy_s(CheckPart, 1,""); 
    		strcpy_s(CheckSum, 1, "");  
    		break;
    	case '$':
    		CheckPart_Equal_0_CheckSum_Equal_1 = 0;  strcpy_s(CheckPart, 1,""); break;
    	case '*':
    		CheckPart_Equal_0_CheckSum_Equal_1 = 1;  strcpy_s(CheckSum, 1,"");  break;
    	default:// not \n $  etc
    		printf("\nCheckPart_Equal_0_CheckSum_Equal_1 Is : %d\n", CheckPart_Equal_0_CheckSum_Equal_1);
    		printf("\nReceivedChar Is: \t%s", ReceivedChar);
    		if (1 == CheckPart_Equal_0_CheckSum_Equal_1)
    		{
    			strcat(CheckSum, ReceivedChar);
    			printf("\ndefault Part: CheckPart Is: \t%s", CheckPart); printf("\ndefault Part: CheckSum Is: \t%s", CheckSum); break;
    		}
    		if (0 == CheckPart_Equal_0_CheckSum_Equal_1)
    		{
    			strcat(CheckPart, ReceivedChar);
    			printf("\ndefault Part: CheckPart Is: \t%s", CheckPart); printf("\ndefault Part: CheckSum Is: \t%s", CheckSum); break;
    		}
    	}
    	return 1;
    }
    
    int main(array<System::String ^> ^args)
    {
    	char ReceivedChar;
    	ProcessEveryByte("a"); ProcessEveryByte("b"); ProcessEveryByte("c"); ProcessEveryByte("d");
    	ProcessEveryByte("$");
    	ProcessEveryByte("0"); ProcessEveryByte("1"); ProcessEveryByte("2"); ProcessEveryByte("3"); ProcessEveryByte("4");
    	ProcessEveryByte("5"); ProcessEveryByte("6"); ProcessEveryByte("7"); ProcessEveryByte("8"); ProcessEveryByte("9");
    	ProcessEveryByte("*");
    	ProcessEveryByte("A"); ProcessEveryByte("B"); ProcessEveryByte("C"); ProcessEveryByte("D"); ProcessEveryByte("E");
    	ProcessEveryByte("F"); ProcessEveryByte("G"); ProcessEveryByte("H"); ProcessEveryByte("I"); ProcessEveryByte("J");
    	system("PAUSE");
    	return 0;
    }

    打印结果如下:

    ...
    
    ReceivedChar Is:        H
    default Part: CheckPart Is:     0123456789
    default Part: CheckSum Is:      ABCDEFGH
    CheckPart_Equal_0_CheckSum_Equal_1 Is : 1
    
    ReceivedChar Is:        I
    default Part: CheckPart Is:     0123456789
    default Part: CheckSum Is:      ABCDEFGHI
    CheckPart_Equal_0_CheckSum_Equal_1 Is : 1
    
    ReceivedChar Is:        J
    default Part: CheckPart Is:     0123456789
    default Part: CheckSum Is:      ABCDEFGHIJ

    Best Wishes,

    Jack Zhang


    MSDN Community Support Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.


    2019年4月17日 3:03
    版主
  • char类型为8bit数据,char CheckPart;char CheckSum; 占用了相邻的2个字节。

    strcat (&CheckPart, ReceivedChar)转换为字符串“0123456789”,被strcat (&CheckSum, ReceivedChar)转换的字符串“ABCDEFGHIJ”所覆盖,&CheckPart与&CheckSum差8bit(1个字节),

    因此CheckSum被显示为“ABCDEFGHIJ”,CheckPart被显示为“BCDEFGHIJ”。

    2019年4月19日 7:58
  • 你好,

    我在VS2017里面测过你的代码,如果你把CheckPart 和 CheckSum 定义成字符数组,问题就解决了。我的测试代码如下:

    #include "stdafx.h"
    #include <stdio.h>
    #include <string.h>
    #include <stdlib.h>
    
    using namespace System;
    int CheckPart_Equal_0_CheckSum_Equal_1 = -1; //
    char CheckPart[11] = "";         //
    char CheckSum[11] = "";         //
    
    int ProcessEveryByte(const char *ReceivedChar)
    {
    	switch (*ReceivedChar) 
    	{
    	case '\n':
    		CheckPart_Equal_0_CheckSum_Equal_1 = -1; strcpy_s(CheckPart, 1,""); 
    		strcpy_s(CheckSum, 1, "");  
    		break;
    	case '$':
    		CheckPart_Equal_0_CheckSum_Equal_1 = 0;  strcpy_s(CheckPart, 1,""); break;
    	case '*':
    		CheckPart_Equal_0_CheckSum_Equal_1 = 1;  strcpy_s(CheckSum, 1,"");  break;
    	default:// not \n $  etc
    		printf("\nCheckPart_Equal_0_CheckSum_Equal_1 Is : %d\n", CheckPart_Equal_0_CheckSum_Equal_1);
    		printf("\nReceivedChar Is: \t%s", ReceivedChar);
    		if (1 == CheckPart_Equal_0_CheckSum_Equal_1)
    		{
    			strcat(CheckSum, ReceivedChar);
    			printf("\ndefault Part: CheckPart Is: \t%s", CheckPart); printf("\ndefault Part: CheckSum Is: \t%s", CheckSum); break;
    		}
    		if (0 == CheckPart_Equal_0_CheckSum_Equal_1)
    		{
    			strcat(CheckPart, ReceivedChar);
    			printf("\ndefault Part: CheckPart Is: \t%s", CheckPart); printf("\ndefault Part: CheckSum Is: \t%s", CheckSum); break;
    		}
    	}
    	return 1;
    }
    
    int main(array<System::String ^> ^args)
    {
    	char ReceivedChar;
    	ProcessEveryByte("a"); ProcessEveryByte("b"); ProcessEveryByte("c"); ProcessEveryByte("d");
    	ProcessEveryByte("$");
    	ProcessEveryByte("0"); ProcessEveryByte("1"); ProcessEveryByte("2"); ProcessEveryByte("3"); ProcessEveryByte("4");
    	ProcessEveryByte("5"); ProcessEveryByte("6"); ProcessEveryByte("7"); ProcessEveryByte("8"); ProcessEveryByte("9");
    	ProcessEveryByte("*");
    	ProcessEveryByte("A"); ProcessEveryByte("B"); ProcessEveryByte("C"); ProcessEveryByte("D"); ProcessEveryByte("E");
    	ProcessEveryByte("F"); ProcessEveryByte("G"); ProcessEveryByte("H"); ProcessEveryByte("I"); ProcessEveryByte("J");
    	system("PAUSE");
    	return 0;
    }

    打印结果如下:

    ...
    
    ReceivedChar Is:        H
    default Part: CheckPart Is:     0123456789
    default Part: CheckSum Is:      ABCDEFGH
    CheckPart_Equal_0_CheckSum_Equal_1 Is : 1
    
    ReceivedChar Is:        I
    default Part: CheckPart Is:     0123456789
    default Part: CheckSum Is:      ABCDEFGHI
    CheckPart_Equal_0_CheckSum_Equal_1 Is : 1
    
    ReceivedChar Is:        J
    default Part: CheckPart Is:     0123456789
    default Part: CheckSum Is:      ABCDEFGHIJ

    Best Wishes,

    Jack Zhang


    MSDN Community Support Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.



    感谢您的辛苦调试,解决了我的问题。
    我在这个坑里困扰好几天了,非常感谢。

    什么?个性签名?哼!打死我也不签。既然你们都不觉得我聪明,那就叫我BenQ吧。好歹也是国际大品牌。

    2019年4月20日 13:27
  • char类型为8bit数据,char CheckPart;char CheckSum; 占用了相邻的2个字节。

    strcat (&CheckPart, ReceivedChar)转换为字符串“0123456789”,被strcat (&CheckSum, ReceivedChar)转换的字符串“ABCDEFGHIJ”所覆盖,&CheckPart与&CheckSum差8bit(1个字节),

    因此CheckSum被显示为“ABCDEFGHIJ”,CheckPart被显示为“BCDEFGHIJ”。


    感谢您的解答,从本质上说明了问题的所在。
    不过,我还想向您继续请教一下。如果我坚持用指针的方式,而不是像上面的朋友,采用数组的方式,有没有更好的修改改程序的方法?
    没办法,求知欲爆发,想学习一下。
    有没有可能,让CheckPart和CheckSum这两个数据,离开的远一点?保证后面的数据被写入的时候,前面的数据不会丢失呢?

    什么?个性签名?哼!打死我也不签。既然你们都不觉得我聪明,那就叫我BenQ吧。好歹也是国际大品牌。

    2019年4月20日 13:33
  • CheckPart和CheckSum两个为全局变量,占用了静态的存储资源,

    编译器始终认为存储资源是紧缺的,不允许存储单元之间存在浪费资源的。

    即使人为地变更数据指针的偏移位置,那么偏移多少合适仍然是不确定的。

    2019年4月20日 15:14
  • 那么,假如说,我给这两个全局变量,最多存储128个字符的空间,应该如何表示呢?

    我是新手,查了一些资料,好像也没有这样写的。您这边有什么好方法么?

    谢谢了


    什么?个性签名?哼!打死我也不签。既然你们都不觉得我聪明,那就叫我BenQ吧。好歹也是国际大品牌。

    2019年4月22日 3:08
  • 定义数组类型,数组个数128。

    这种方法简单,规范。

    2019年4月22日 5:09