- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我目前正在编写一个程序,打算从 url 中读取并打印出有关天气的信息。省略实际与 url 建立连接的代码行,这里是循环遍历给定 url 上显示的信息以及相关变量的循环:
FILE *f = fdopen(sockfd, "r+");
char line[1000];
int count = 0;
int i;
char otime[200];
float temp;
int hum;
char dir[10];
float speed;
int fcastTempHi;
int fcastTempLo;
char wkday1[40];
char wkday2[20];
char wkday3[20];
char wkday4[20];
while(fgets(line, 1000, f) != NULL)
{
sscanf(line, "\t\t\"observation_time\":\"%[^\"]\",", otime);
sscanf(line, "\t\t\"temp_f\":%f\",", &temp);
sscanf(line, "\t\t\"relative_humidity\":\"%d\",", &hum);
sscanf(line, "\t\t\"wind_dir\":\"%[^\"]\",", dir);
sscanf(line, "\t\t\"wind_mph\":%f\",", &speed);
sscanf(line, "\t\t\"period\":1,");
}
printf("Current Conditions\n");
printf("Observation time: %s\n", otime);
printf("Temperature: %f F\n", temp);
printf("Humidity: %d%%\n", hum);
printf("Wind: %s %f mph\n\n", dir, speed);
printf("Forecast\n");
//printf("%s:\n", wkday1);
fclose(f);
假设我扫描的所有值都存在于显示的 url 内容中,但我将只包括我现在感兴趣的内容;阅读自
"period":1,
"high": {
"fahrenheit":"69",
"celsius":"20"
},
"low": {
"fahrenheit":"49",
"celsius":"9"
},
"weekday_short":"Sat",
"weekday":"Saturday",
我正在尝试定位“period”:1 行,如果找到,我想继续定位“weekday”:“string”,稍后打印该字符串。不幸的是,我正在扫描的另一行看起来像
"period":1,
"icon":"nt_partlycloudy",
并且这些行比我正在寻找的周期 1 出现得早得多。我特别寻找“期间”:1 而不仅仅是扫描“工作日”的原因是因为工作日显示多次,因为这是 10 天的天气预报。所以我想要包含我需要的工作日的特定时间段(时间段:1)而不是其他时间段(时间段:2、3、4、5、6 ...等),因为这些时间段包含接下来的工作日。
我曾尝试使用 if 语句,但这只能找到两次出现的“period:1”,之后我无法扫描任何内容。
一旦发现“period”:1 的第一个实例,我就尝试了 fork,但这没有用(很可能是因为我缺乏经验和对 fork 的普遍无知)。
为了使帖子简短,我将排除我尝试过的其他方法,但我会说我已经让程序至少识别出有两行是“period”:1,虽然我不是确定如何解决这个问题,以便我可以扫描第二个“period”实例之后的行:1,。预先感谢您的任何回复。
来源更多:
{
"response": {
"version":"0.1",
"termsofService":"REDACTED",
"features": {
"conditions": 1
,
"forecast10day": 1
}
}
, "current_observation": {
"image": {
"url":"REDACTED",
"title":"REDACTED",
"link":"REDACTED"
},
"display_location": {
"full":"REDACTED",
"city":"REDACTED",
"state":"REDACTED",
"state_name":"REDACTED",
"country":"US",
"country_iso3166":"US",
"zip":"REDACTED",
"magic":"1",
"wmo":"99999",
"latitude":"REDACTED",
"longitude":"REDACTED",
"elevation":"REDACTED"
},
"observation_location": {
"full":"REDACTED",
"city":"REDACTED",
"state":"REDACTED",
"country":"US",
"country_iso3166":"US",
"latitude":"REDACTED",
"longitude":"REDACTED",
"elevation":"705 ft"
},
"estimated": {
},
"station_id":"REDACTED",
"observation_time":"Last Updated on May 15, 4:17 PM PDT",
"observation_time_rfc822":"Fri, 15 May 2015 16:17:57 -0700",
"observation_epoch":"1431731877",
"local_time_rfc822":"Fri, 15 May 2015 16:18:17 -0700",
"local_epoch":"1431731897",
"local_tz_short":"PDT",
"local_tz_long":"REDACTED",
"local_tz_offset":"-0700",
"weather":"Partly Cloudy",
"temperature_string":"68.3 F (20.2 C)",
"temp_f":68.3,
"temp_c":20.2,
"relative_humidity":"53%",
"wind_string":"From the South at 3.2 MPH Gusting to 6.5 MPH",
"wind_dir":"South",
"wind_degrees":185,
"wind_mph":3.2,
"wind_gust_mph":"6.5",
"wind_kph":5.1,
"wind_gust_kph":"10.5",
"pressure_mb":"1011",
"pressure_in":"29.87",
"pressure_trend":"-",
"dewpoint_string":"51 F (11 C)",
"dewpoint_f":51,
"dewpoint_c":11,
"heat_index_string":"NA",
"heat_index_f":"NA",
"heat_index_c":"NA",
"windchill_string":"NA",
"windchill_f":"NA",
"windchill_c":"NA",
"feelslike_string":"68.3 F (20.2 C)",
"feelslike_f":"68.3",
"feelslike_c":"20.2",
"visibility_mi":"10.0",
"visibility_km":"16.1",
"solarradiation":"--",
"UV":"7","precip_1hr_string":"0.00 in ( 0 mm)",
"precip_1hr_in":"0.00",
"precip_1hr_metric":" 0",
"precip_today_string":"-999.00 in (-25375 mm)",
"precip_today_in":"-999.00",
"precip_today_metric":"--",
"icon":"partlycloudy",
"icon_url":"REDACTED",
"forecast_url":"REDACTED",
"history_url":"REDACTED",
"ob_url":"REDACTED",
"nowcast":""
}
,
"forecast":{
"txt_forecast": {
"date":"2:52 PM PDT",
"forecastday": [
{
"period":0,
"icon":"partlycloudy",
"icon_url":"REDACTED",
"title":"Friday",
"fcttext":"Partly cloudy. Lows overnight in the upper 40s.",
"fcttext_metric":"Partly cloudy. Low 9C.",
"pop":"0"
}
,
{
"period":1,
"icon":"nt_partlycloudy",
"icon_url":"REDACTED",
"title":"Friday Night",
"fcttext":"Some passing clouds. Low 49F. Winds SSE at 5 to 10 mph.",
"fcttext_metric":"Partly cloudy. Low 9C. Winds SSE at 10 to 15 km/h.",
"pop":"0"
}
,
{
"period":2,
"icon":"partlycloudy",
"icon_url":"REDACTED",
"title":"Saturday",
"fcttext":"Sunshine and clouds mixed. High 76F. Winds SSW at 5 to 10 mph.",
"fcttext_metric":"Sunshine and clouds mixed. High 24C. Winds SSW at 10 to 15 km/h.",
"pop":"0"
}
,
{
"period":3,
"icon":"nt_partlycloudy",
"icon_url":"REDACTED",
"title":"Saturday Night",
"fcttext":"A few clouds from time to time. Low 49F. Winds S at 5 to 10 mph.",
"fcttext_metric":"Partly cloudy skies. Low 9C. Winds S at 10 to 15 km/h.",
"pop":"10"
}
,
{
"period":4,
"icon":"mostlycloudy",
"icon_url":"REDACTED",
"title":"Sunday",
"fcttext":"More clouds than sun. High 74F. Winds SSW at 5 to 10 mph.",
"fcttext_metric":"Mainly cloudy. High 23C. Winds SSW at 10 to 15 km/h.",
"pop":"10"
}
,
{
"period":5,
"icon":"nt_partlycloudy",
"icon_url":"REDACTED",
"title":"Sunday Night",
"fcttext":"Mostly cloudy skies early will become partly cloudy late. Low around 50F. Winds S at 5 to 10 mph.",
"fcttext_metric":"Mostly cloudy skies early, then partly cloudy after midnight. Low near 10C. Winds S at 10 to 15 km/h.",
"pop":"10"
}
,
{
"period":6,
"icon":"clear",
"icon_url":"REDACTED",
"title":"Monday",
"fcttext":"A few passing clouds, otherwise generally sunny. High 74F. Winds SSW at 10 to 15 mph.",
"fcttext_metric":"A few clouds early, otherwise mostly sunny. High 24C. Winds SSW at 15 to 25 km/h.",
"pop":"10"
}
,
{
"period":7,
"icon":"nt_partlycloudy",
"icon_url":"REDACTED",
"title":"Monday Night",
"fcttext":"Partly cloudy skies. Low around 50F. Winds S at 10 to 15 mph.",
"fcttext_metric":"A few clouds from time to time. Low near 10C. Winds S at 15 to 25 km/h.",
"pop":"10"
}
,
{
"period":8,
"icon":"partlycloudy",
"icon_url":"REDACTED",
"title":"Tuesday",
"fcttext":"Partly cloudy skies. High 74F. Winds S at 10 to 15 mph.",
"fcttext_metric":"Some clouds in the morning will give way to mainly sunny skies for the afternoon. High 24C. Winds S at 15 to 25 km/h.",
"pop":"10"
}
,
{
"period":9,
"icon":"nt_clear",
"icon_url":"REDACTED",
"title":"Tuesday Night",
"fcttext":"Clear. Low near 50F. Winds S at 10 to 15 mph.",
"fcttext_metric":"A mostly clear sky. Low near 10C. Winds S at 15 to 25 km/h.",
"pop":"10"
}
,
{
"period":10,
"icon":"clear",
"icon_url":"REDACTED",
"title":"Wednesday",
"fcttext":"Sun and a few passing clouds. High 77F. Winds SSW at 5 to 10 mph.",
"fcttext_metric":"Sunshine along with some cloudy intervals. High near 25C. Winds SSW at 10 to 15 km/h.",
"pop":"10"
}
,
{
"period":11,
"icon":"nt_clear",
"icon_url":"REDACTED",
"title":"Wednesday Night",
"fcttext":"Mainly clear skies. Low 51F. Winds S at 5 to 10 mph.",
"fcttext_metric":"Mainly clear skies. Low 11C. Winds S at 10 to 15 km/h.",
"pop":"20"
}
,
{
"period":12,
"icon":"partlycloudy",
"icon_url":"REDACTED",
"title":"Thursday",
"fcttext":"Sunshine and clouds mixed. High 77F. Winds SSW at 5 to 10 mph.",
"fcttext_metric":"Intervals of clouds and sunshine. High near 25C. Winds SSW at 10 to 15 km/h.",
"pop":"10"
}
,
{
"period":13,
"icon":"nt_chancetstorms",
"icon_url":"REDACTED",
"title":"Thursday Night",
"fcttext":"Isolated thunderstorms during the evening, then partly cloudy overnight. Low 53F. Winds S at 5 to 10 mph. Chance of rain 30%.",
"fcttext_metric":"Widely scattered showers or a thunderstorm early. Then partly cloudy. Low 12C. Winds S at 10 to 15 km/h. Chance of rain 30%.",
"pop":"30"
}
{
"period":14,
"icon":"clear",
"icon_url":"REDACTED",
"title":"Friday",
"fcttext":"Mostly sunny skies. High 78F. Winds SSW at 5 to 10 mph.",
"fcttext_metric":"Sunshine. High around 25C. Winds SSW at 10 to 15 km/h.",
"pop":"10"
}
,
{
"period":15,
"icon":"nt_clear",
"icon_url":"REDACTED",
"title":"Friday Night",
"fcttext":"A mostly clear sky. Low 54F. Winds S at 5 to 10 mph.",
"fcttext_metric":"Clear skies. Low 12C. Winds S at 10 to 15 km/h.",
"pop":"10"
}
,
{
"period":16,
"icon":"clear",
"icon_url":"REDACTED",
"title":"Saturday",
"fcttext":"Mainly sunny. High 81F. Winds SSW at 5 to 10 mph.",
"fcttext_metric":"Sunny skies. High 27C. Winds SSW at 10 to 15 km/h.",
"pop":"10"
}
,
{
"period":17,
"icon":"nt_clear",
"icon_url":"REDACTED",
"title":"Saturday Night",
"fcttext":"A few clouds from time to time. Low 53F. Winds S at 5 to 10 mph.",
"fcttext_metric":"Mostly clear skies. Low 12C. Winds S at 10 to 15 km/h.",
"pop":"10"
}
,
{
"period":18,
"icon":"clear",
"icon_url":"REDACTED",
"title":"Sunday",
"fcttext":"Mostly sunny skies. High 82F. Winds SSW at 5 to 10 mph.",
"fcttext_metric":"A few clouds from time to time. High 28C. Winds SSW at 10 to 15 km/h.",
"pop":"10"
}
,
{
"period":19,
"icon":"nt_clear",
"icon_url":"REDACTED",
"title":"Sunday Night",
"fcttext":"Mostly clear. Low 53F. Winds SSE at 5 to 10 mph.",
"fcttext_metric":"A mostly clear sky. Low 12C. Winds SSE at 10 to 15 km/h.",
"pop":"10"
}
]
},
"simpleforecast": {
"forecastday": [
{"date":{
"epoch":"1431741600",
"pretty":"7:00 PM PDT on May 15, 2015",
"day":15,
"month":5,
"year":2015,
"yday":134,
"hour":19,
"min":"00",
"sec":0,
"isdst":"1",
"monthname":"May",
"monthname_short":"May",
"weekday_short":"Fri",
"weekday":"Friday",
"ampm":"PM",
"tz_short":"PDT",
"tz_long":"REDACTED"
},
"period":1,
"high": {
"fahrenheit":"69",
"celsius":"20"
},
"low": {
"fahrenheit":"49",
"celsius":"9"
},
"conditions":"Partly Cloudy",
"icon":"partlycloudy",
"icon_url":"REDACTED",
"skyicon":"",
"pop":0,
"qpf_allday": {
"in": 0.00,
"mm": 0
},
"qpf_day": {
"in": null,
"mm": null
},
"qpf_night": {
"in": 0.00,
"mm": 0
},
"snow_allday": {
"in": 0.0,
"cm": 0.0
},
"snow_day": {
"in": null,
"cm": null
},
"snow_night": {
"in": 0.0,
"cm": 0.0
},
"maxwind": {
"mph": 8,
"kph": 13,
"dir": "",
"degrees": 0
},
"avewind": {
"mph": 1,
"kph": 2,
"dir": "South",
"degrees": 186
},
"avehumidity": 73,
"maxhumidity": 0,
"minhumidity": 0
}
,
{"date":{
"epoch":"1431828000",
"pretty":"7:00 PM PDT on May 16, 2015",
"day":16,
"month":5,
"year":2015,
"yday":135,
"hour":19,
"min":"00",
"sec":0,
"isdst":"1",
"monthname":"May",
"monthname_short":"May",
"weekday_short":"Sat",
"weekday":"Saturday",
"ampm":"PM",
"tz_short":"PDT",
"tz_long":"REDACTED"
},
"period":2,
"high": {
"fahrenheit":"76",
"celsius":"24"
},
"low": {
"fahrenheit":"49",
"celsius":"9"
},
"conditions":"Partly Cloudy",
"icon":"partlycloudy",
"icon_url":"REDACTED",
"skyicon":"",
"pop":0,
"qpf_allday": {
"in": 0.00,
"mm": 0
},
"qpf_day": {
"in": 0.00,
"mm": 0
},
"qpf_night": {
"in": 0.00,
"mm": 0
},
"snow_allday": {
"in": 0.0,
"cm": 0.0
},
"snow_day": {
"in": 0.0,
"cm": 0.0
},
"snow_night": {
"in": 0.0,
"cm": 0.0
},
"maxwind": {
"mph": 10,
"kph": 16,
"dir": "SSW",
"degrees": 196
},
"avewind": {
"mph": 8,
"kph": 13,
"dir": "SSW",
"degrees": 196
},
"avehumidity": 54,
"maxhumidity": 0,
"minhumidity": 0
}
,
{"date":{
"epoch":"1431914400",
"pretty":"7:00 PM PDT on May 17, 2015",
"day":17,
"month":5,
"year":2015,
"yday":136,
"hour":19,
"min":"00",
"sec":0,
"isdst":"1",
"monthname":"May",
"monthname_short":"May",
"weekday_short":"Sun",
"weekday":"Sunday",
"ampm":"PM",
"tz_short":"PDT",
"tz_long":"REDACTED"
},
"period":3,
"high": {
"fahrenheit":"74",
"celsius":"23"
},
"low": {
"fahrenheit":"50",
"celsius":"10"
},
"conditions":"Mostly Cloudy",
"icon":"mostlycloudy",
"icon_url":"REDACTED",
"skyicon":"",
"pop":10,
"qpf_allday": {
"in": 0.00,
"mm": 0
},
"qpf_day": {
"in": 0.00,
"mm": 0
},
"qpf_night": {
"in": 0.00,
"mm": 0
},
"snow_allday": {
"in": 0.0,
"cm": 0.0
},
"snow_day": {
"in": 0.0,
"cm": 0.0
},
"snow_night": {
"in": 0.0,
"cm": 0.0
},
"maxwind": {
"mph": 10,
"kph": 16,
"dir": "SSW",
"degrees": 192
},
"avewind": {
"mph": 9,
"kph": 14,
"dir": "SSW",
"degrees": 192
},
"avehumidity": 54,
"maxhumidity": 0,
"minhumidity": 0
}
,
{"date":{
"epoch":"1432000800",
"pretty":"7:00 PM PDT on May 18, 2015",
"day":18,
"month":5,
"year":2015,
"yday":137,
"hour":19,
"min":"00",
"sec":0,
"isdst":"1",
"monthname":"May",
"monthname_short":"May",
"weekday_short":"Mon",
"weekday":"Monday",
"ampm":"PM",
"tz_short":"PDT",
"tz_long":"REDACTE"
},
"period":4,
"high": {
"fahrenheit":"74",
"celsius":"23"
},
"low": {
"fahrenheit":"50",
"celsius":"10"
},
"conditions":"Clear",
"icon":"clear",
"icon_url":"REDACTED",
"skyicon":"",
"pop":10,
"qpf_allday": {
"in": 0.00,
"mm": 0
},
"qpf_day": {
"in": 0.00,
"mm": 0
},
"qpf_night": {
"in": 0.00,
"mm": 0
},
"snow_allday": {
"in": 0.0,
"cm": 0.0
},
"snow_day": {
"in": 0.0,
"cm": 0.0
},
"snow_night": {
"in": 0.0,
"cm": 0.0
},
"maxwind": {
"mph": 15,
"kph": 24,
"dir": "SSW",
"degrees": 209
},
"avewind": {
"mph": 10,
"kph": 16,
"dir": "SSW",
"degrees": 209
},
"avehumidity": 52,
"maxhumidity": 0,
"minhumidity": 0
}
,
{"date":{
"epoch":"1432087200",
"pretty":"7:00 PM PDT on May 19, 2015",
"day":19,
"month":5,
"year":2015,
"yday":138,
"hour":19,
"min":"00",
"sec":0,
"isdst":"1",
"monthname":"May",
"monthname_short":"May",
"weekday_short":"Tue",
"weekday":"Tuesday",
"ampm":"PM",
"tz_short":"PDT",
"tz_long":"REDACTED"
},
"period":5,
"high": {
"fahrenheit":"74",
"celsius":"23"
},
"low": {
"fahrenheit":"50",
"celsius":"10"
},
"conditions":"Partly Cloudy",
"icon":"partlycloudy",
"icon_url":"REDACTED",
"skyicon":"",
"pop":10,
"qpf_allday": {
"in": 0.00,
"mm": 0
},
"qpf_day": {
"in": 0.00,
"mm": 0
},
"qpf_night": {
"in": 0.00,
"mm": 0
},
"snow_allday": {
"in": 0.0,
"cm": 0.0
},
"snow_day": {
"in": 0.0,
"cm": 0.0
},
"snow_night": {
"in": 0.0,
"cm": 0.0
},
"maxwind": {
"mph": 15,
"kph": 24,
"dir": "S",
"degrees": 190
},
"avewind": {
"mph": 10,
"kph": 16,
"dir": "S",
"degrees": 190
},
"avehumidity": 55,
"maxhumidity": 0,
"minhumidity": 0
}
,
{"date":{
"epoch":"1432173600",
"pretty":"7:00 PM PDT on May 20, 2015",
"day":20,
"month":5,
"year":2015,
"yday":139,
"hour":19,
"min":"00",
"sec":0,
"isdst":"1",
"monthname":"May",
"monthname_short":"May",
"weekday_short":"Wed",
"weekday":"Wednesday",
"ampm":"PM",
"tz_short":"PDT",
"tz_long":"REDACTED"
},
"period":6,
"high": {
"fahrenheit":"77",
"celsius":"25"
},
"low": {
"fahrenheit":"51",
"celsius":"11"
},
"conditions":"Clear",
"icon":"clear",
"icon_url":"REDACTED",
"skyicon":"",
"pop":10,
"qpf_allday": {
"in": 0.00,
"mm": 0
},
"qpf_day": {
"in": 0.00,
"mm": 0
},
"qpf_night": {
"in": 0.00,
"mm": 0
},
"snow_allday": {
"in": 0.0,
"cm": 0.0
},
"snow_day": {
"in": 0.0,
"cm": 0.0
},
"snow_night": {
"in": 0.0,
"cm": 0.0
},
"maxwind": {
"mph": 10,
"kph": 16,
"dir": "SSW",
"degrees": 196
},
"avewind": {
"mph": 9,
"kph": 14,
"dir": "SSW",
"degrees": 196
},
"avehumidity": 54,
"maxhumidity": 0,
"minhumidity": 0
}
,
{"date":{
"epoch":"1432260000",
"pretty":"7:00 PM PDT on May 21, 2015",
"day":21,
"month":5,
"year":2015,
"yday":140,
"hour":19,
"min":"00",
"sec":0,
"isdst":"1",
"monthname":"May",
"monthname_short":"May",
"weekday_short":"Thu",
"weekday":"Thursday",
"ampm":"PM",
"tz_short":"PDT",
"tz_long":"REDACTED"
},
"period":7,
"high": {
"fahrenheit":"77",
"celsius":"25"
},
"low": {
"fahrenheit":"53",
"celsius":"12"
},
"conditions":"Partly Cloudy",
"icon":"partlycloudy",
"icon_url":"REDACTED",
"skyicon":"",
"pop":10,
"qpf_allday": {
"in": 0.00,
"mm": 0
},
"qpf_day": {
"in": 0.00,
"mm": 0
},
"qpf_night": {
"in": 0.00,
"mm": 0
},
"snow_allday": {
"in": 0.0,
"cm": 0.0
},
"snow_day": {
"in": 0.0,
"cm": 0.0
},
"snow_night": {
"in": 0.0,
"cm": 0.0
},
"maxwind": {
"mph": 10,
"kph": 16,
"dir": "SSW",
"degrees": 195
},
"avewind": {
"mph": 8,
"kph": 13,
"dir": "SSW",
"degrees": 195
},
"avehumidity": 53,
"maxhumidity": 0,
"minhumidity": 0
}
,
{"date":{
"epoch":"1432346400",
"pretty":"7:00 PM PDT on May 22, 2015",
"day":22,
"month":5,
"year":2015,
"yday":141,
"hour":19,
"min":"00",
"sec":0,
"isdst":"1",
"monthname":"May",
"monthname_short":"May",
"weekday_short":"Fri",
"weekday":"Friday",
"ampm":"PM",
"tz_short":"PDT",
"tz_long":"REDACTED"
},
"period":8,
"high": {
"fahrenheit":"78",
"celsius":"26"
},
"low": {
"fahrenheit":"54",
"celsius":"12"
},
"conditions":"Clear",
"icon":"clear",
"icon_url":"REDACTED",
"skyicon":"",
"pop":10,
"qpf_allday": {
"in": 0.00,
"mm": 0
},
"qpf_day": {
"in": 0.00,
"mm": 0
},
"qpf_night": {
"in": 0.00,
"mm": 0
},
"snow_allday": {
"in": 0.0,
"cm": 0.0
},
"snow_day": {
"in": 0.0,
"cm": 0.0
},
"snow_night": {
"in": 0.0,
"cm": 0.0
},
"maxwind": {
"mph": 10,
"kph": 16,
"dir": "SSW",
"degrees": 208
},
"avewind": {
"mph": 8,
"kph": 13,
"dir": "SSW",
"degrees": 208
},
"avehumidity": 59,
"maxhumidity": 0,
"minhumidity": 0
}
,
{"date":{
"epoch":"1432432800",
"pretty":"7:00 PM PDT on May 23, 2015",
"day":23,
"month":5,
"year":2015,
"yday":142,
"hour":19,
"min":"00",
"sec":0,
"isdst":"1",
"monthname":"May",
"monthname_short":"May",
"weekday_short":"Sat",
"weekday":"Saturday",
"ampm":"PM",
"tz_short":"PDT",
"tz_long":"REDACTED"
},
"period":9,
"high": {
"fahrenheit":"81",
"celsius":"27"
},
"low": {
"fahrenheit":"53",
"celsius":"12"
},
"conditions":"Clear",
"icon":"clear",
"icon_url":"REDACTED",
"skyicon":"",
"pop":10,
"qpf_allday": {
"in": 0.00,
"mm": 0
},
"qpf_day": {
"in": 0.00,
"mm": 0
},
"qpf_night": {
"in": 0.00,
"mm": 0
},
"snow_allday": {
"in": 0.0,
"cm": 0.0
},
"snow_day": {
"in": 0.0,
"cm": 0.0
},
"snow_night": {
"in": 0.0,
"cm": 0.0
},
"maxwind": {
"mph": 10,
"kph": 16,
"dir": "SSW",
"degrees": 202
},
"avewind": {
"mph": 9,
"kph": 14,
"dir": "SSW",
"degrees": 202
},
"avehumidity": 57,
"maxhumidity": 0,
"minhumidity": 0
}
,
{"date":{
"epoch":"1432519200",
"pretty":"7:00 PM PDT on May 24, 2015",
"day":24,
"month":5,
"year":2015,
"yday":143,
"hour":19,
"min":"00",
"sec":0,
"isdst":"1",
"monthname":"May",
"monthname_short":"May",
"weekday_short":"Sun",
"weekday":"Sunday",
"ampm":"PM",
"tz_short":"PDT",
"tz_long":"REDACTED"
},
"period":10,
"high": {
"fahrenheit":"82",
"celsius":"28"
},
"low": {
"fahrenheit":"53",
"celsius":"12"
},
"conditions":"Clear",
"icon":"clear",
"icon_url":"REDACTED",
"skyicon":"",
"pop":10,
"qpf_allday": {
"in": 0.00,
"mm": 0
},
"qpf_day": {
"in": 0.00,
"mm": 0
},
"qpf_night": {
"in": 0.00,
"mm": 0
},
"snow_allday": {
"in": 0.0,
"cm": 0.0
},
"snow_day": {
"in": 0.0,
"cm": 0.0
},
"snow_night": {
"in": 0.0,
"cm": 0.0
},
"maxwind": {
"mph": 10,
"kph": 16,
"dir": "SSW",
"degrees": 206
},
"avewind": {
"mph": 9,
"kph": 14,
"dir": "SSW",
"degrees": 206
},
"avehumidity": 55,
"maxhumidity": 0,
"minhumidity": 0
}
]
}
}
}
我已经更新了源中的所有数据。
最佳答案
据我所知,您正在尝试检查是否有两个 "period": 1
实例,如果有,请读取另一个值。在那种情况下,我相信这样的事情应该有效:
#include <stdio.h>
#include <stdbool.h>
int main()
{
// I stored the data in weather.txt, you'll have to change this
FILE *f = fopen("weather.txt", "r+");
char line[1000];
int count = 0;
int i;
char otime[200];
float temp;
int hum;
char dir[10];
float speed;
int fcastTempHi;
int fcastTempLo;
char wkday1[40];
char wkday2[20];
char wkday3[20];
char wkday4[20];
int period1 = 0; // Keep track of how many period: 1's we've seen
int value = 0; // Temp variable to store value
bool haswkday = false; // Bool for if we've gotten the value or not
while(fgets(line, 1000, f) != NULL)
{
sscanf(line, "\t\t\"observation_time\":\"%[^\"]\",", otime);
sscanf(line, "\t\t\"temp_f\":%f\",", &temp);
sscanf(line, "\t\t\"relative_humidity\":\"%d\",", &hum);
sscanf(line, "\t\t\"wind_dir\":\"%[^\"]\",", dir);
sscanf(line, "\t\t\"wind_mph\":%f\",", &speed);
// If period is scanned successfully and its value is 1
if (sscanf(line, "\t\t\"period\":%d,", &value) != 0 && value == 1)
{
period1++; // Increase how many times we've seen it
}
// If we've seen exactly 2 period: 1's, then also check for weekday
if (period1 == 2 && sscanf(line, "\t\t\"weekday\":\"%[^\"]\"", &wkday1) > 0)
{
haswkday = true; // Weekday value already stored, say that we've gotten it
}
}
printf("Current Conditions\n");
printf("Observation time: %s\n", otime);
printf("Temperature: %f F\n", temp);
printf("Humidity: %d%%\n", hum);
printf("Wind: %s %f mph\n\n", dir, speed);
printf("Forecast\n");
// If we got the value, then print it
if (haswkday)
{
printf("%s:\n", wkday1);
}
fclose(f);
}
关于C编程——sscanf读取相似行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30270764/
我正在尝试打印 timeval 类型的值。实际上我可以打印它,但我收到以下警告: 该行有多个标记 格式“%ld”需要“long int”类型,但参数 2 的类型为“struct timeval” 程序
我正在编写自己的 unix 终端,但在执行命令时遇到问题: 首先,我获取用户输入并将其存储到缓冲区中,然后我将单词分开并将它们存储到我的 argv[] 数组中。IE命令是“firefox”以启动存储在
我是 CUDA 的新手。我有一个关于一个简单程序的问题,希望有人能注意到我的错误。 __global__ void ADD(float* A, float* B, float* C) { con
我有一个关于 C 语言 CGI 编程的一般性问题。 我使用嵌入式 Web 服务器来处理 Web 界面。为此,我在服务器中存储了一个 HTML 文件。在此 HTML 文件中包含 JavaScript 和
**摘要:**在代码的世界中,是存在很多艺术般的写法,这可能也是部分程序员追求编程这项事业的内在动力。 本文分享自华为云社区《【云驻共创】用4种代码中的艺术试图唤回你对编程的兴趣》,作者: break
我有一个函数,它的任务是在父对象中创建一个变量。我想要的是让函数在调用它的级别创建变量。 createVariable testFunc() [1] "test" > testFunc2() [1]
以下代码用于将多个连续的空格替换为1个空格。虽然我设法做到了,但我对花括号的使用感到困惑。 这个实际上运行良好: #include #include int main() { int ch, la
我正在尝试将文件写入磁盘,然后自动重新编译。不幸的是,某事似乎不起作用,我收到一条我还不明白的错误消息(我是 C 初学者 :-)。如果我手动编译生成的 hello.c,一切正常吗?! #include
如何将指针值传递给结构数组; 例如,在 txt 上我有这个: John Doe;xxxx@hotmail.com;214425532; 我的代码: typedef struct Person{
我尝试编写一些代码来检索 objectID,结果是 2B-06-01-04-01-82-31-01-03-01-01 . 这个值不正确吗? // Send a SysObjectId SNMP req
您好,提前感谢您的帮助, (请注意评论部分以获得更多见解:即,以下示例中的成本列已添加到此问题中;西蒙提供了一个很好的答案,但成本列本身并未出现在他的数据响应中,尽管他提供的功能与成本列一起使用) 我
我想知道是否有人能够提出一些解决非线性优化问题的软件包的方法,而非线性优化问题可以为优化解决方案提供整数变量?问题是使具有相等约束的函数最小化,该函数受某些上下边界约束的约束。 我已经在R中使用了'n
我是 R 编程的初学者,正在尝试向具有 50 列的矩阵添加一个额外的列。这个新列将是该行中前 10 个值的平均值。 randomMatrix <- generateMatrix(1,5000,100,
我在《K&R II C 编程 ANSI C》一书中读到,“>>”和“0; nwords--) sum += *buf++; sum = (sum >>
当下拉列表的选择发生变化时,我想: 1) 通过 div 在整个网站上显示一些 GUI 阻止覆盖 2)然后处理一些代码 3) 然后隐藏叠加层。 问题是,当我在事件监听器函数中编写此逻辑时,将执行 onC
我正在使用 Clojure 和 RESTEasy 设计 JAX-RS REST 服务器. 据我了解,用 Lisp 系列语言编写的应用程序比用“传统”命令式语言编写的应用程序更多地构建为“特定于领域的语
我目前正在研究一种替代出勤监控系统作为一项举措。目前,我设计的用户表单如下所示: Time Stamp Userform 它的工作原理如下: 员工将选择他/她将使用的时间戳类型:开始时间、超时、第一次
我是一名学生,试图自学编程,从在线资源和像您这样的人那里获得帮助。我在网上找到了一个练习来创建一个小程序来执行此操作: 编写一个程序,读取数字 a 和 b(长整型)并列出 a 和 b 之间有多少个数字
我正在尝试编写一个 shell 程序,给定一个参数,打印程序的名称和参数中的每个奇数词(即,不是偶数词)。但是,我没有得到预期的结果。在跟踪我的程序时,我注意到,尽管奇数词(例如,第 5 个词,5 %
只是想知道是否有任何 Java API 可以让您控制台式机/笔记本电脑外壳上的 LED? 或者,如果不可能,是否有可能? 最佳答案 如果你说的是前面的 LED 指示电源状态和 HDD 繁忙状态,恐怕没
我是一名优秀的程序员,十分优秀!