Newbie needs help with a project

Hey all,
I am new to programming. Please be kind to me I am a 56 years old veteran who decided to go back to school to fulfill his wish.
Now with that away,
I have written a small script that collects data and write to a text file.
What I would like to do is write an application that can read that file and open the data to a table
For example:

00:00:04:00 Cue Time
10121 Filename
00:00:59:29 Duration
line1
line2
line3
00:00:00:00
10238
00:00:29:28



00:00:00:00
10351
00:00:30:00

each set of data is separated by 3 file.
Data should appear on the table as follow:

Cue Time Filename Duration
00:00:04:00 10121 00:00:59:29

I would like to be able to sort the table.

A pointer on how to get started will be greatly appreciated.

I haven't really touched databases with c++ yet but you could store all this information in a struct or class and have a function that prints it out in a table like format

next you can use ifstream to read the contents of that file if you have it in a file I'm assuming you are first writing to a file manually,create a while loop and take input from the file and add it to a temporary struct or class add this object to let's say an array of structs or even use a vector or map from the STL,

after this create another loop to output the data,of course you could do it in the same loop but would probably be more clear

what you will need to study

structs,classes

arrays

basic input/output

maybe STL containers such as vectors

hope I'm of some help

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.

Topic archived. No new replies allowed.