I used the %Width[^]s format specifier, in which "Width" specifies the maximum number of characters to be read for the value of the associated variable. It does not appear to work properly or it is incorrect.
Code:
char inbuf[128] = "\0"; //input string just read from infile char obs_sta[32] = "\0"; //name of observation station char sky_wx[16] = "\0"; //sky and weather conditions char tmp[8] = "\0"; //dry bulb temperature (?F) char dp[8] = "\0"; //dew point temperature (?F) char rh[8] = "\0"; //relative humidity (%) char wind[16] = "\0"; //wind speed/direction/gust_speed char pres[16] = "\0"; //barometric pressure (in Hg) char *rise_fall = '\0'; //pressure rising (R) or falling (F) //indicatior char remarks[16] = "\0"; //read a record from text file fgets (inbuf, sizeof(inbuf), fp1); //Five examples of fixed-length fields record layout: //CITY SKY/WX TMP DP RH WIND PRES REMARKS //ELLINGTON FLD PTSUNNY 90 75 62 SW10G18 29.94F HAZE HX 100 //*ZAPATA SUNNY 99 61 28 SE13G20 29.74F HX 100 //FORT STOCKTON SUNNY 102 -17 1 SW24G30 29.78F HX 93 //GUYMON NOT AVBL //SANTA FE SUNNY 81 7 6 VRB6G23 30.02F SMOKE HX 75 //field lengths are as in the sscanf format statement below //The CITY field is 15 characters long, the SKY/WX field is 8 characters long; //and the REMARKS field contains those remaining characters to the newline. //Each of these fields can contain a string with embedded spaces. //inbuf correctly contains the entire record //parse the record into its components (all treated as non-numeric values) sscanf(inbuf, "%15[^]s%8[^]s%4s%4s%4s%10s%6s%c%[^]s", obs_sta, sky_wx, tmp, dp, rh, wind, pres, rise_fall, remarks); //print a parsed line printf("full line: [%15s][%8s][%4s][%4s][%4s][%10s][%6s][%c][%s]\n", obs_sta, sky_wx, tmp, dp, rh, wind, pres, rise_fall, remarks); //Five examples of parsed output: //[ELLINGTON FLD ][ ][ ][ ][ ][ ][ ][ ][] //[*ZAPATA ][ ][ ][ ][ ][ ][ ][ ][] //[FORT STOCKTON ][ ][ ][ ][ ][ ][ ][ ][] //[GUYMON ][ ][ ][ ][ ][ ][ ][ ][] //[SANTA FE ][ ][ ][ ][ ][ ][ ][ ][]
Comment