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

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

问题描述

我正在编写一个 arduino 库来在网络上发布 http 请求.

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

我正在使用 http://arduino.cc/en/Tutorial/TextString

当我在函数调用后引用我定义的字符串对象时,我的代码表现得很奇怪.

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.

推荐答案

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

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());

此外,可能不需要传入指针(引用可能会使代码看起来更好).

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

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

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