w05-g04-read-input-function
Read input file with function
Basics
Before starting with your assignment, please go through the following basic exercises:
- w05-bsc-functions-1 #10 (closed)
- w05-bsc-functions-2 #11 (closed)
Objective
Adapt the code developed in the previous week read_input.cpp
, such that the reading of the lines is done in a function in a separate file and that this returns a container of trimmed strings with the lines of a specific section in the input file. This function will be used in two different files.
Implementation
-
In the
/tehpc/basics/
directory create a new file namedinput_file.hpp
. Inside this file, implement the function nameread_input_section
which should, based on a file name and a section name, read the appropriate part of the file and return it as a container filled with strings. Use the code from previous week as starting point. -
Modify
read_input_test.cpp
to use the function implemented ininput_files.hpp
. Without modifying the testing part inread_input_test.cpp
, run it to test if your function works. If not, correctinput_files.hpp
until the test passes. -
Modify
read_input_function.cpp
to use the above-mentioned function to read the input from the defined input file. Make sure that this example runs correctly. -
Make sure that your code is well structured and commented.
Notes:
Compile and run your code by:
- going into the
tehpc/build/
folder - (if you have never done before) copy the file
read_input_fixed.inp
into thebuild
folder. - (if you have never done before) copy the file
read_input_test.inp
into thebuild
folder. - compile with the following command:
g++ -Werror -Wpedantic -g -o read_input_test ../basics/read_input_test.cpp
- run code with
./read_input_test
- compile with the following command:
g++ -Werror -Wpedantic -g -o read_input_function ../basics/read_input_function.cpp
- run code with
./read_input_function