ini file

ini file

Loading settings.

Many applications read and write settings into an initialisation (ini) file. Typically the data in the file is structured into blocks identified with square brackets. Each setting has a name, followed by an equal sign, followed by the data. E.g. in the example below, port is a setting and the data is 143.

[modified by]

name=Dave Hillyard


[database]

; use IP address in case network name resolution is not working

server=192.0.2.62

port=143

Make


Write a program that reads the contents of a text file as shown above and outputs the data contained within it in the format:

Last modified by: Dave Hillyard

IP address: 192.0.2.62

Port number: 143

Use this boilerplate code as a starting point:

Success Criteria

Remember to add a comment before a subprogram, selection or iteration statement to explain its purpose.

Complete the subprogram called `input_settings` that:

  1. Attempts to open a file called `settings.ini`. If the file is not found the message, `"settings.ini file not found."` is output and no further processing takes place. Note the file is included in the Trinket above for you to use as source data.
  2. Reads in each line of data in the file.
  3. Separates the line of data into two parts. The part before an equal sign (the attribute) and a part after an equal sign (data). If the line is not a data attribute, it will only have one part and is ignored.
  4. Stores the values for modified by, server/ip address and port settings in three variables and output their values.

Note the data attributes can be anywhere in the file, not necessarily in the order presented above.

Complete the `main program` so that:

  1. The `input_settings` function is called.

Typical inputs and outputs from the program would be:

File contents for settings.ini:

[modified by]

name=Dave Hillyard


[database]

; use IP address in case network name resolution is not working

server=192.0.2.62

port=143

Output:

Last modified by: Dave Hillyard

IP address: 192.0.2.62

Port number: 143

Knowledge Organiser

Use these resources as a reference to help you meet the success criteria.

Programming guide:

Evaluate


Run the unit tests below to check that your program has met the success criteria.

Last modified by: Dave Hillyard

IP address: 192.0.2.62

Port number: 143

Check that you have:

  • Used comments within the code to describe the purpose of subprograms, conditions and iterations.
  • Used meaningful identifier names. That means the names of subprograms and variables indicate what they are for.

Registered in England and Wales: 10442992

VAT Number: 290 9845 58

Telephone: 01452 947500

Email: admin@craigndave.co.uk

Craig'n'Dave logo

Craig 'n' Dave

Bett Awards 2024 Finalist