Reading multiple file

I created a program to read a single file and output. Everything is working as intended(read and output), now, I've been trying to open 2 or more of these files(different month but similar format) but to no avail. I need to store the S variable of the file for further calculation, and the date and time.

Example of asd.csv opened in notepad. First line is the header of the file which i ignored. The Line1, line2 and line3 is just to illustrate which line it is in when i open it in notepad

Line 0:WAST,DP,Dta,Dts,EV,QFE,QFF,QNH,RF,RH,S,SR,ST1,ST2,ST3,ST4,Sx,T
Line 1:02/03/2014 22:00,17.6,176,0,0,1010.2,1013.6,1013.8,0,81.2,1,23,34.6,30.5,29.3,28,4,21
Line 2:02/03/2014 22:10,17.8,176,0,0,1010.1,1013.5,1013.7,0,83.6,0,17,34.5,30.6,29.3,28,1,20.74
Line 3:02/03/2014 22:20,17.9,176,0,0,1010.1,1013.5,1013.7,0,85.2,0,4,34.4,30.6,29.3,28,0,20.51

Below is the reading of file. (Part of the code)

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
58
        int main()
        {
            string filename;
            ifstream input;
            Vector<WindLogType> windlog;
            filename = "asd.csv";

            input.open(filename.c_str());
            input.ignore(500,'\n');

            if(input.is_open())
        {
            cout<< "Loading Data\n"<<endl;
        }


            string line,line2, readDay, readMonth, readYear, readHour, readMinute;
            float sp;


        while(!input.eof())
        {

            getline(input,readDay,'/');
            getline(input,readMonth,'/');
            getline(input,readYear,' ');

            getline(input,readHour,':');
            getline(input,readMinute,',');


            int day1 =atoi(readDay.c_str());
            int month1=atoi(readMonth.c_str());
            int year1=atoi(readYear.c_str());
            int hour1=atoi(readHour.c_str());
            int minute1=atoi(readMinute.c_str());

            float s1;

            for(int i = 0;i<10;i++)
            {
                input>>s1;
                input.ignore(100,',');
            }

            WindLogType T1;//create a record
            T1.d.setDate(day1,month1,year1);
            T1.t.setTime(hour1,minute1);
            T1.speed = s1;

            windlog.push_back(T1);//push inside vector


            getline(input,line2,'\n');
        }


        windlog.print();


_____________________________________________________

Example output:

2/3/2014 22:00
Speed: 0
2/3/2014 22:10
Speed: 0
2/3/2014 22:20
Speed: 0
Last edited on
If you can set the files' names yourself then give them sequential names and loop through the files one-by-one. Here's a program I uses a few months back to read 2 files of std::string:
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
#include <iostream>
#include <fstream>
#include <sstream>
#include<vector>
#include<string>

auto constexpr file_num = 2;

int main()
{
    std::vector<std::string> words;
    int i{};

    while (i < file_num)
    {
        std::stringstream filename;
        filename << "F:\\test" << i << ".txt";
        std::ifstream File(filename.str());

        if(File)
        {
            std::string temp;
            while(File >> temp)
            {
                if(File)
                {
                    words.emplace_back(temp);
                }
            }
        File.close();
        File.clear();
        }
        ++i;
    }
}

if, for some reason, you are not able to control/change the name of the files then you can use std::ifstream pointers to point to the required file, do stuff with it and then re-assign the pointer to the next file and so on

here's a link on another forum that might be of interest:
https://cboard.cprogramming.com/cplusplus-programming/171503-using-fstream-pointers.html
Topic archived. No new replies allowed.