-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathweatherReport.c
62 lines (57 loc) · 1.72 KB
/
weatherReport.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
// Temperature of given city
#include <stdio.h>
#include <string.h>
#define CITY_NAME_LENGTH 50
#define COMMAND_LENGTH 500
int main()
{
char cityName[CITY_NAME_LENGTH], *pieceOfReport;
char command[COMMAND_LENGTH], report[COMMAND_LENGTH];
printf("Enter City Name:");
scanf("%s", cityName);
fflush(stdin);
sprintf(command, "curl -k \"https://api.openweathermap.org/data/2.5/weather?q=%s&units=metric&appid=201ac1b1456985494f5e9f7c1c258d45\">weather.txt", cityName);
system(command);
FILE *fpWeather = fopen("weather.txt", "r");
fgets(report, COMMAND_LENGTH, fpWeather);
fclose(fpWeather);
pieceOfReport = strtok(report, ":");
while (pieceOfReport != 0)
{
if (strcmp(pieceOfReport, "{\"temp\"") == 0)
{
pieceOfReport = strtok('\0', ",");
system("cls");
printf("Temperature in %s: %s degree Celsius", cityName, pieceOfReport);
break;
}
pieceOfReport = strtok('\0', ":");
}
}
/*// tTemperature of given city
#include <stdio.h>
#include <string.h>
#define CITY_NAME_LENGTH 50
#define COMMAND_LENGTH 500
int main()
{
int counter;
char cityName[CITY_NAME_LENGTH], *pieceOfReport;
char command[COMMAND_LENGTH], report[COMMAND_LENGTH];
printf("Enter City Name:");
scanf("%s", cityName);
fflush(stdin);
sprintf(command, "curl -k \"https://api.openweathermap.org/data/2.5/weather?q=%s&units=metric&appid=201ac1b1456985494f5e9f7c1c258d45\">weather.txt", cityName);
system(command);
FILE *fpWeather = fopen("weather.txt", "r");
fgets(report, COMMAND_LENGTH, fpWeather);
fclose(fpWeather);
pieceOfReport = strtok(report, ":");
for (counter = 0; counter < 10; counter++)
{
pieceOfReport = strtok('\0', ":");
}
pieceOfReport = strtok('\0', ",");
system("cls");
printf("Temperature in %s: %s", cityName, pieceOfReport);
}*/