I'm guessing "
each set of data is separated by 3 file" may be a typo?
I've assumed it means
three lines? I've also assumed it means three
blank lines.
For testing purposes I used an input file which looks like this:
00:00:04:00
10121
00:00:59:29
00:00:00:00
10238
00:00:29:28
00:00:00:00
10351
00:00:30:00
|
I'd approach this gradually, start simple and gradually refine what you have until it does what you need. One of the factors in this approach is that there will be learning as you go along too.
For a first attempt, I treated everything as a string. Also, I assumed that there are no spaces within the string, which means reading from the file is straightforward.
Of course if my assumptions are wrong this approach will need to be modified accordingly.
First version.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37
|
#include <iostream>
#include <string>
#include <fstream>
#include <vector>
#include <iomanip>
using namespace std;
int main()
{
std::vector<std::string> cue_time;
std::vector<std::string> filename;
std::vector<std::string> duration;
std::ifstream fin("myvidmaker_input.txt");
for (std::string cue, fil, dur; fin >> cue >> fil >> dur; )
{
cue_time.push_back(cue);
filename.push_back(fil);
duration.push_back(dur);
}
std::cout << std::setw(15) << "Cue Time"
<< std::setw(15) << "Filename"
<< std::setw(15) << "Duration" << '\n';
for (size_t i = 0; i<cue_time.size(); ++i)
{
std::cout << std::setw(15) << cue_time[i]
<< std::setw(15) << filename[i]
<< std::setw(15) << duration[i] << '\n';
}
}
|
Output:
Cue Time Filename Duration
00:00:04:00 10121 00:00:59:29
00:00:00:00 10238 00:00:29:28
00:00:00:00 10351 00:00:30:00
|
For a second attempt I used a class to contain the data for each group. This looks longer and more complex but the important thing is that the code in
main()
is itself much simpler.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57
|
#include <iostream>
#include <string>
#include <fstream>
#include <vector>
#include <iomanip>
class Timing {
private:
std::string cue_time;
std::string filename;
std::string duration;
public:
static void heading();
friend std::istream& operator >> (std::istream& is, Timing & t);
friend std::ostream& operator << (std::ostream& os, const Timing & t);
};
void Timing::heading()
{
std::cout << std::setw(15) << "Cue Time"
<< std::setw(15) << "Filename"
<< std::setw(15) << "Duration" << '\n';
}
std::istream& operator >> (std::istream& is, Timing & t)
{
is >> t.cue_time >> t.filename >> t.duration;
return is;
}
std::ostream& operator << (std::ostream& os, const Timing & t)
{
os << std::setw(15) << t.cue_time
<< std::setw(15) << t.filename
<< std::setw(15) << t.duration;
return os;
}
int main()
{
std::vector<Timing> timings;
std::ifstream fin("myvidmaker_input.txt");
for (Timing tim; fin >> tim; )
timings.push_back(tim);
Timing::heading();
for (const auto & tim : timings)
std::cout << tim << '\n';
}
|
The next two steps would be to consider how the data is to be sorted. The code for sorting is straightforward. But the program needs to be told how to compare the Timing objects.
A step directly related to that is to define another class to contain the timing data (such as "00:00:04:00"), rather than just using a string. This would allow more control over the sorting of data.