Skip to main content

Thread: C programming problems


hello, i'm trying write weather program school project. keep running errors , i'm not sure problem program is.

code:
//************************************************************  //              copyright 2012  //               jacob boline  //************************************************************    #include <stdio.h>  #include <curl/curl.h>  #include <string.h>  #include <stdlib.h>    void main()  {    char url[100] = "http://api.wunderground.com/api/59eb6f72cabe560e/"; /*declares inital url*/    char location[5];    char feature[12];    char temp[7] = "temp_f";    char tempf[5];    int lnth;    printf("please enter zip code:\n");                   /* asks location. */    scanf("%s", location);	                             /* stores location. */    printf("please enter information like:\n"); /* asks information. */    scanf("%s", feature);                                      /* stores information. */    lnth = strlen(feature);                                    /* finds length of string. */    strncat(url, feature, lnth);                               /* appends feature url */    strncat(url, "/q/", 3);    strncat(url, location, 5);                                 /* appends location url. */    strncat(url, ".json", 5);    curlf(url);                                                /* calls curl function. */    printf("file downloaded.\n");    tempf = search_in_file(temp);    printf("%s\n", tempf);  }    void curlf(const char* url, char *str)  {    curl *json;    file *wdata;    curlcode res;    json = curl_easy_init();                           /* start downloading process. */    curl_easy_setopt(json, curlopt_url, url);          /* tells curl url is. */    wdata = fopen( "wdata.json", "w");                 /* create file save data. */    curl_easy_setopt(json, curlopt_writedata, wdata);  /* tells curl save sata wdata. */    curl_easy_perform(json);                           /* performs download. */    curl_easy_cleanup(json);                           /* cleans up. */    fclose(wdata);                                     /* close file. */  }    char search_in_file(char *str) {    file *fp;    char temp[512];    char sep[] = ":,";    char *result = null;      fp = fopen("wdata.json", "r");      while(fgets(temp, 512, fp) != null)    {      if((strstr(temp, str)) != null)      {        result = strtok( temp, sep );        result = strtok( null, sep );        return result;      }    }     fclose(fp);  }
the program supposed download file, parse temp_f in order find temperature, return main function, , print it. keep getting these errors when attempt compile it:
code:
main.c: in function ‘main’:  main.c:30:9: error: incompatible types when assigning type ‘char[5]’ type ‘int’  main.c: @ top level:  main.c:34:6: warning: conflicting types ‘curlf’ [enabled default]  main.c:28:3: note: previous implicit declaration of ‘curlf’ here  main.c:48:6: error: conflicting types ‘search_in_file’  main.c:30:11: note: previous implicit declaration of ‘search_in_file’ here  main.c: in function ‘search_in_file’:  main.c:62:7: warning: return makes integer pointer without cast [enabled default]
i'm compiling "gcc main.c -l curl -o main". can me?

there couple of issues in code.

1. signature of functions must known before use
either place whole functions @ top of file or prototypes (= first line ; @ end)
code:
void curlf(const char* url, char *str);  int main()  {  curlf(bla);  }    void curlf(const char* url, char *str)  {  //  }
2. search_in_file has wrong return type, must return char* , variable receiving result must same. can't return arrays in c.

3. search_in_file returns pointer stack storage invalid when function ends, should strdup result place on heap, return pointer , make sure free again later.

4. happens when target search string gets cut in half 512 buffered read?

5. there may more issues overlooked.

parse json should prefer using library e.g. json-c


Forum The Ubuntu Forum Community Ubuntu Specialised Support Development & Programming Programming Talk C programming problems


Ubuntu

Comments

Popular posts from this blog

How to set the order of FAQs instead of alphabetical

Thread: Get UK Keyboard working

how do I change the e-mail address for my merchant account