Penrose5833
Penrose5833

Reputation: 107

Best way to read this file to manipulate later?

I am given a config file that looks like this for example:

Start Simulator Configuration File
Version/Phase: 2.0
File Path: Test_2e.mdf
CPU Scheduling Code: SJF
Processor cycle time (msec): 10
Monitor display time (msec): 20
Hard drive cycle time (msec): 15
Printer cycle time (msec): 25
Keyboard cycle time (msec): 50
Mouse cycle time (msec): 10
Speaker cycle time (msec): 15
Log: Log to Both
Log File Path: logfile_1.lgf
End Simulator Configuration File

I am supposed to be able to take this file, and output the cycle and cycle times to a log and/or monitor. I am then supposed to pull data from a meta-data file that will tell me how many cycles each of these run (among other things) and then im supposed to calculate and log the total time. for example 5 Hard drive cycles would be 75msec. The config and meta data files can come in any order.

I am thinking I will put each item in an array and then cycle through waiting for true when the strings match(This will also help detect file errors). The config file should always be the same size despite a different order. The metadata file can be any size so I figured i would do a similar thing but in a vector. Then I will multiply the cycle times from the config file by the number of cycles in the matching metadata file string. I think the best way to read the data from the vector is in a queue.

Does this sound like a good idea? I understand most of the concepts. But my data structures is shaky in terms of actually coding it. For example when reading from the files, should I read it line by line, or would it be best to separate the int's from the strings to calculate them later? I've never had to do this that from a file that can change before. If i separate them, would I have to use separate arrays/vectors?

Im using C++ btw

Upvotes: 2

Views: 81

Answers (1)

Burhan Khalid
Burhan Khalid

Reputation: 174622

Your logic should be:

  1. Create two std::map variables, one that maps a string to a string, and another that maps a string to a float.
  2. Read each line of the file
  3. If the line contains :, then, split the string into two parts:

    3a. Part A is the line starting from zero, and 1-minus the index of the :

    3b. Part B is the part of the line starting from 1+ the index of the :

  4. Use these two parts to store in your custom std::map types, based on the value type.

Now you have read the file properly. When you read the meta file, you will simply look up the key in the meta data file, use it to lookup the corresponding key in your configuration file data (to get the value), then do whatever mathematical operation is required.

Upvotes: 1

Related Questions