Hello all. I need some advice on how to compact my code. I have a file that my program will read several times. Each time a different test will check it for something specific. I can't check it all at once because I let the user specify what to check. I'm currently using an ifstream with a while loop to read until the end of file.
Here are three approaches I have in mind, feel free to suggest more:
1. I keep a static ifstream in a function and call it each time I want a new line to read. The function will "remember" what line is at and returns a new line each time (I suspect this can work but I'm not 100% sure). Each function can then be separated and call readline to get a new line back.
2. I make one giant while loop and make case statements that call the correct case and pass the string all the way down to the test function that is being run.
3. Write everything to some sort of array buffer. The file isn't terribly big about 1000 lines.
What should I do? Is the first approach even worth the effort to cut down a few lines of code? The second case doesn't seem to be much better as I cannot reuse anything.
When you want to make an optimization such as this, we need to know more about what you're doing before it can be decided which approach to use.
In general though, when you only open the file after user interaction, it doesn't generally make much difference whether or not you re-open, read, and close the file for each request or not, since the only bottleneck is the user giving your program input to proceed. If it were automated however, then you would indeed want to keep the file open.
So what I'm doing is not automated. There is a way that let's the user decide which args to run at the beginning of the program. After that no changing. They may run 1 or all. They must rerun the test program if they want to change it up. The goal of the program is to run tests on some file and tell the user if it is correct or not.
There's nothing that specifically concerns me. I've recently been programming in a functional language and tried to apply the modularity to stuff I'm working on. I realize that's it's a different paradigm and hence I'm asking for advice if what I'm doing is actually worth it.