首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Google translate with c

Google translate with c
EN

Stack Overflow用户
提问于 2011-01-05 12:48:49
回答 2查看 679关注 0票数 2

任何人都知道如何在c++中使用Google translate API。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2011-01-05 12:53:46

here中有一个REST API可用。您应该能够从C很容易地访问它。

票数 5
EN

Stack Overflow用户

发布于 2011-01-08 07:27:48

多亏了pwc,我使用了他的资源并用管道创建了它,下面是它的源代码

代码语言:javascript
复制
  char  chrptr_GoogleResponse [0x1000];
  char* chrptr_pos2 = NULL;
  char* translate_text = search_str;           
  char* lang_pairs = "&langpair=es%7Cen'";     // language pairs
  bool boNoError = true;

    strcpy(chrarray_GoogleCommand, "curl -s -e http://www.my-ajax-site.com \
            'https://ajax.googleapis.com/ajax/services/language/translate?v=1.0&q=");

    strcat(chrarray_GoogleCommand, translate_text);
    strcat(chrarray_GoogleCommand, lang_pairs);

    popen(chrarray_GoogleCommand, "r");

    FILE* fileptrFile = popen(chrarray_GoogleCommand, "r");

 if (fileptrFile == NULL)
    {
        printf("Error on opening pipe\n.");
        exit(EXIT_FAILURE);
    }

 while ( !feof (fileptrFile) )
    {
        fgets (chrptr_GoogleResponse , 0x1000 , fileptrFile) ;

        chrptr_pos1 = strstr(chrptr_GoogleResponse, "{\"translatedText\":\"") ;
        if ( chrptr_pos1 )
        {
            chrptr_pos1 = chrptr_pos1 + strlen("{\"translatedText\":\"") ;
            chrptr_pos2 = strstr(chrptr_GoogleResponse, "\"}, \"responseDetails\": null, \"responseStatus\": 200}") ;
            if ( chrptr_pos2 )
            {
                memcpy(chrptr_temp, chrptr_pos1, chrptr_pos2 - chrptr_pos1);
                memset((void*) ((unsigned long) chrptr_temp + (unsigned long) chrptr_pos2 - (unsigned long) chrptr_pos1), 0, 1);
            }
            else
                boNoError = false ;
        }
        else
            boNoError = false ;

        if (feof (fileptrFile))
            break;
    }
    pclose(fileptrFile);

    if (boNoError)
        strcpy(search_str, chrptr_temp); //copy translated text.
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/4600913

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档