2013-02-13 56 views
0

我写此代码为POS机结果没有读成字符串

我从网页链接设置返回的数据为“坏”,这样我可以测试是否是真正的工作。 但是当我比较结果与字符串“坏”它总是说他们不相等。 购买时,我打印结果来屏幕显示两个结果都不好。

请我需要你的帮助。 代码下面

void checklogin(void) { 
    CURL *curl; 
    CURLcode res; 
     long timeout = 30; 
     char buffer[50000]; 
    //Initializing the CURL module 
    curl = curl_easy_init(); 

    if(curl){ 
    //Tell libcurl the URL 
    curl_easy_setopt(curl,CURLOPT_URL, "http://website.org/login.php"); 
    curl_easy_setopt(curl, CURLOPT_POSTFIELDS, "username=su&password=ch"); 
     curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, myfunc); 
     curl_easy_setopt(curl, CURLOPT_WRITEDATA, buffer); 
     curl_easy_setopt(curl, CURLOPT_TIMEOUT, timeout); 
     curl_easy_setopt(curl, CURLOPT_VERBOSE, 1L); //tell curl to output its progress 

      res = curl_easy_perform(curl); 

      CTOS_LCDTClearDisplay(); 

      char mai[30]; 
      char mai2[30]; 

      char *serverresponse = "bad"; 
      sprintf(mai2, "%s" , serverresponse); 
      sprintf(mai, "%s" , buffer); 

      if(mai2 == mai){ 
      CTOS_LCDTPrint("Invalid username"); 
      CTOS_KBDGet(&key); 
      }else{ 
       //loginname = buffer; 
       //mainusername = username; 
       CTOS_LCDTPrintXY(1, 1, "Login Success"); 
       CTOS_LCDTPrintXY(1, 2, "Welcome"); 
       CTOS_KBDGet(&key); 
       } 


    } 
} 
+1

谷歌比较C中的字符串。 – 2013-02-13 07:32:26

回答

0

mai2和脉是指针到炭阵列的第一个字段。由于这些是不同的阵列,它们的地址当然是不同的。你要的内容进行比较,因此你需要使用strcmp

#include <string.h> 
strcmp("hello", "hello"); //0 

结果将是0,如果两个字符数组包含相同的内容。

if (strcmp(mai, mai2) == 0) { 
    ... 
} 

-Hannes

+0

这应该可能是string.h - 他似乎在使用C,尽管标记为C++。 – Cubic 2013-02-13 07:38:58

+0

谢谢。但#include 无法与POS一起工作 – 2013-02-13 07:43:31

+0

您是对的,Cubic。谢谢。编辑:string.h是标准的C99,所以它应该用任何标准的编译器进行编译。 – 2013-02-13 09:51:39

1

你对C做的工作在C++,但是比较,你需要使用strcmp()比较字符串。如果作为参数提供的两个字符串包含相同的内容,则返回值将为0。另外请记住,您可以在需要时使用其他strcmp()函数,例如当您要进行不区分大小写的比较时使用stricmp()