简体   繁体   English

带C的字符和字符串

[英]Characters and Strings with C

I'm working on restful client written in C. It is very simple, I just have to send by post a person name (John, Sam, Muhammad, Whatever...) which I write on terminal. 我正在使用C语言编写的宁静客户端。这很简单,我只需要邮寄一个我在终端上写的人名(John,Sam,Muhammad,Whatever ...)。

My whole code is working fine but I have some issues with conversion char or strings, to send by post. 我的整个代码工作正常,但我在通过邮寄发送转换字符或字符串时遇到一些问题。

My code: 我的代码:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <curl/curl.h>

int main(void) {

CURL *curl;
CURLcode res;

int x = 1;
unsigned char m;

while (x != 0) {
    printf("Opcao 1 para incluir nova pessoa\n");
    printf("Opcao 2 para listar pessoas\n");
    printf("Opcao 0 para sair\n");
    printf("Selecione a opcao: ");
    scanf("%d",&x);

    if (x == 1) {
        printf("Nome da pessoa: ");
        scanf("%s",&m);

        curl = curl_easy_init();
        if(curl) {
            curl_easy_setopt(curl, CURLOPT_URL, "localhost/wsRest/index.php/novo/" );
            curl_easy_setopt(curl, CURLOPT_POSTFIELDS, "");
            res = curl_easy_perform(curl);
            if(res != CURLE_OK)
                fprintf(stderr, "curl_easy_perform() failed: %s\n",
                  curl_easy_strerror(res));
            curl_easy_cleanup(curl);
        }
    }

    printf("\n");
}

curl_global_cleanup();
return 0;

}

I need to find a way to put the name written on variable 'm' in curl_easy_setopt() function, concatenated with my URL, but I have no idea how to do it, and the examples that a I found cant even read the URL to another variable... 我需要找到一种方法来将写在变量'm'上的名称与我的URL连接在一起,但是我不知道该怎么做,而且我发现的示例甚至无法读取URL来把它写在curl_easy_setopt()函数中。另一个变量

How can I do it? 我该怎么做?

Thanks you everyone! 谢谢大家!

This won't work the way you expect: 这将无法按您期望的方式工作:

scanf("%s", &m);

m is an unsigned char , the scanf %s modifier will attempt to read a string, write it to the pointer you feed it, and null-terminate it. m是一个unsigned char ,scanf %s修饰符将尝试读取一个字符串,将其写入您提供的指针,并以null终止。 For any non-empty name, it will write to invalid memory (if you're lucky, this should crash). 对于任何非空名称,它将写入无效的内存中(如果幸运的话,这应该会崩溃)。

Indeed, you passed a pointer to a character, but there's only space for 1 character. 确实,您传递了一个指向字符的指针,但是只有一个字符的空间。

You should use an array instead, for example: 您应该改用数组,例如:

char m[512];
/* ... */
scanf("%s", m);

Note that this places an upper bound of 511 on the name length. 请注意,这会将名称长度的上限设置为511。 If you expect longer names, increase the buffer size. 如果希望使用更长的名称,请增加缓冲区大小。

UPDATE : 更新

You can prepend the URL by doing: 您可以通过以下操作在URL前面添加:

char m[512];
int idx = sprintf(m, "localhost/wsRest/index.php/novo/");
/* ... */
scanf("%s", &m[idx]);

Then pass m as the url. 然后将m作为网址传递。

This works by first storing the URL path in m , and then reading the input string into the rest of the buffer. 首先通过将URL路径存储在m ,然后将输入字符串读入缓冲区的其余部分来工作。 sprintf(3) returns the number of characters written, so idx is the offset of the first position after the URL path. sprintf(3)返回写入的字符数,因此idx是URL路径后第一个位置的偏移量。

If you want to append instead, then scanf("%s", m) and then use strcat(m, "localhost/wsRest/index.php/novo/") . 如果要附加,则使用scanf("%s", m) ,然后使用strcat(m, "localhost/wsRest/index.php/novo/")

Again, this assumes that the name + the URL size is less than 511. 同样,这假设名称+ URL的大小小于511。

First, you need an array to hold the name entered by the user. 首先,您需要一个数组来保存用户输入的名称。 Change this 改变这个

unsigned char m;

to this 对此

char m[1000];

Next, you need an array to hold the URL 接下来,您需要一个数组来保存URL

char url[1200];

Then you can use sprintf to append the name to the URL 然后,您可以使用sprintf将名称附加到URL

sprintf( url, "localhost/wsRest/index.php/novo/%s", m ); 

And finally pass the url to the setopt function 最后将url传递给setopt函数

curl_easy_setopt(curl, CURLOPT_URL, url);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM