字符串引用函数调用没有更新的C ++ [英] String reference not updating in function call in C++

查看:357
本文介绍了字符串引用函数调用没有更新的C ++的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我写一个Arduino库张贴在网页的http请求。

I am writing an arduino library to post http request on web.

我使用的是String类从 http://arduino.cc/en/Tutorial/TextString

I am using the String class from http://arduino.cc/en/Tutorial/TextString

我的code为出现异常行为时,我是一个函数调用后指的是我定义的字符串对象。

My code is behaving strangely when I am referring to my defined string objects after a function call.

在这里,其实我试图让我的GET请求的主体,并从HTTP GET请求的响应删除HTTP标头。

Here actually I am trying to get the body of my GET request and removing the http headers from the http GET request's response.

以下是说明:

方法调用:

  String body;  
  if(pinger.Get(host,path,&body))
  {
    Serial.println("Modified String Outside :");
    Serial.println(body);
    Serial.println();
    Serial.println("Modified String Outside Address");
    Serial.println((int)&body);
  }

输出

Modified String Outside :
HTTP/1.1 200 OK
Server: Apache-Coyote/1.1
Content-Type: text/html
Content-Length: 113
Date: Wed, 13 Jan 2010 14:36:28 GMT

<html>
<head>
<title>Ashish Sharma
</title>
</head>
<body>
Wed Jan 13 20:06:28 IST 2010
</body>
</html>


Modified String Outside Address
2273

方法说明:

bool Pinger::Get(String host, String path, String *response) {
    bool connection = false;
    bool status = false;

    String post1 = "GET ";
    post1 = post1.append(path);
    post1 = post1.append(" HTTP/1.1");

    String host1 = "Host: ";
    host1 = host1.append(host);

    for (int i = 0; i < 10; i++) {
        if (client.connect()) {
            client.println(post1);
            client.println(host1);
            client.println();
            connection = true;
            break;
        }
    }

    int nlCnt = 0;
    while (connection) {
        if (client.available()) {
            int c = client.read();
            response->append((char) c);
            if (c == 0x000A && nlCnt == 0) {
                nlCnt++;
                if (response->contains("200")) {
                        status = true;
                        continue;
                    } else {
                        client.stop();
                        client.flush();
                        break;
                    }   
            }
        }
        if (!client.connected()) {
            client.stop();
            connection = false;
        }
    }           
    response = &response->substring(response->indexOf("\n\r\n"),response->length());    
    Serial.println("Modified String: ");
    Serial.println(*response);  

    Serial.println();
    Serial.print("Modified String Address: ");
    Serial.println((int)&response);
    return status;
}

输出:

Modified String: 
Ø
<html>
<head>
<title>Ashish Sharma
</title>
</head>
<body>
Wed Jan 13 20:06:28 IST 2010
</body>
</html>

Modified String Address: 2259

由于可以从字符串参考对象是给我的Get方法里面的正确的字符串,但的字符串内容的参考例子可以看出改变时,get方法返回。

As can be seen from the example the string reference object is giving me the correct string inside the Get method but the reference of the string contents change when the Get method returns.

推荐答案

如果我正确理解你的code,你可能会想要做这样的事情:

If I understood correctly your code, you probably would want to do something like this:

*response = response->substring(response->indexOf("\n\r\n"),response->length());

而不是

response = &response->substring(response->indexOf("\n\r\n"),response->length());

大概也没有必要在一个指针传递(参考很可能使code看起来更漂亮)。

Also there's probably no need to pass in a pointer ( reference would probably make the code look much nicer ).

这篇关于字符串引用函数调用没有更新的C ++的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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