0

I have a question regarding this type of problem. There is a .txt file containing an unknown number of lines in a format like this:

Cityname(max 30 char)temperature(float)wind_speed(float)air_pressure(float).

Example of a line in a file: Paris-80.81010 where Paris is a city name, -8 is the temperature(float), 0.8(float) is the wind speed and 1010(float) is the air pressure.

It is needed to read all lines, and store each line in a structure with specific data types. The result needs to be a list of structs.

How can I read a line like this and store all the different data types properly without losing information? I have tried everything I could possibly think of, and I have no idea how to do this.

Cœur
  • 37,241
  • 25
  • 195
  • 267
CptWhoWent
  • 33
  • 1
  • 4
  • Also, a city name can have a number with it as well, because it can be a number of a weather station in that city. For an example Paris02, Amsterdam21, Berlin03 etc. – CptWhoWent Aug 17 '17 at 15:14
  • 1
    And there are no delimiters? If so, this is impossible to solve. – Iharob Al Asimi Aug 17 '17 at 15:15
  • Iharob Al Asimi, thank you but I need an idea. :) – CptWhoWent Aug 17 '17 at 15:15
  • 3
    You need a lot more than that. – Iharob Al Asimi Aug 17 '17 at 15:16
  • I consulted some of senior students. They say that even doe it is not specified in the problem, the line of the file is written in a way that it has blanks (spaces) between the data in the line. So the point was for a student to conclude that any other way is impossible to solve. With this information, the problem would be easily solved with a: `while(feof(FILE*)==0) { fscanf(FILE*, "%s %f %f %f", char* somestring, float1, float2, float3); /* Then store it in a struct */ }` As a part of a code. All this with a reasonable names for variables. Thanks a lot for your help guys! – CptWhoWent Aug 18 '17 at 08:27
  • Wrong, starting with `while (feof(FILE *) == 0)` which is always one line off. – Iharob Al Asimi Aug 18 '17 at 15:39
  • How can I check then if the file is at the end? – CptWhoWent Aug 19 '17 at 17:14
  • 2
    If the file is at the end, `fscanf()` would fail and then you can check if `feof()`. Because only after that would it return true. – Iharob Al Asimi Aug 19 '17 at 18:02
  • Thank you Iharob, you are very kind. – CptWhoWent Aug 19 '17 at 19:21

2 Answers2

2

This is a question that depends on the way that the information is structured.

If, for example, the city name always ends with a letter, you can know for sure the next digit is the start of the temperature. That's easy.

But it's not always simple. in your example, how would you know that the temperature is not -80.81, the wind speed is 0 and the air pressure is 10?

Without more information about the possible structure and values of the different variables, you have no way to know.

Noam Ohana
  • 186
  • 14
2

Read the line. Then process it in reverse, using the following assumptions:

  • Air pressure is an integer between 9xx and 10xx hecto Pascal, so any number at the end meeting this criterium is the air pressure (unless you are on Mars);

  • Wind speed is between 0.0 and 80 knots, unless there is a hurricane. Any number meeting this criterium is the wind speed.

  • Temperature is between -40 and +50, unless you are on Mars, Venus or the North or South pole. Any number meeting this criterium is the temperature in Celsius.

  • The remainder is the city name.

Without delimiters you need these assumptions to be able to process the line. Any errors in the parsing are caused by the data format. Communicate these assumptions to the data provider to let him challenge the assumptions.

Paul Ogilvie
  • 25,048
  • 4
  • 23
  • 41
  • I consulted some of senior students. They say that even doe it is not specified in the problem, the line of the file is written in a way that it has blanks (spaces) between the data in the line. So the point was for a student to conclude that any other way is impossible to solve. With this information, the problem would be easily solved with a: `while(feof(FILE*)==0) { fscanf(FILE*, "%s %f %f %f", char* somestring, float1, float2, float3); /* Then store it in a struct */ }` As a part of a code. All this with a reasonable names for variables. Thanks a lot for your help guys! – CptWhoWent Aug 18 '17 at 08:20