reading the year value of a file

Hello guys,

I'm working on a code which I have insert a file with the name of xxx-2017.csv

How can I make the program return the value of the year value only?

For example, I made a function to save this file into an array and I want it to return the value of the year ( 2017 in this case).

any hints?
Last edited on
closed account (SECMoG1T)
hello, are you passing the file name to the function... i might not have gotten that clearly
but if that what you meant the you can learn from this example

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
#include <iostream>
#include <string>
#include <vector>


std::size_t extract_year(const std::string& filename);

int main()
{
  std::vector<std::string> filenames{"xxx-2006.csv","xxx-2007.csv","xxx-2008.csv",\
                                     "xxx-2009.csv","xxx-2010.csv","xxx-2011.csv",\
                                     "xxx-2012.csv","xxx-2013.csv","xxx-2014.csv",\
                                     "xxx-2015.csv","xxx-2016.csv","xxx-2017.csv"
                                    };

  std::cout<<"filename\tYear\n";
  std::cout<<"--------------------\n";
  for(auto name : filenames)
   {
       std::cout<<name<<"\t"<<extract_year(name)<<std::endl;
   }
}

///returns an unsigned year value
std::size_t extract_year(const std::string& filename)
{
    auto last_pos  = filename.rfind(".");
    auto first_pos = filename.find_last_not_of("0123456789",last_pos-1) + 1;

    std::string temp(filename.begin()+first_pos,filename.begin()+last_pos);

    return std::stoi(temp);
}


output
--------


filename        Year
--------------------
xxx-2006.csv    2006
xxx-2007.csv    2007
xxx-2008.csv    2008
xxx-2009.csv    2009
xxx-2010.csv    2010
xxx-2011.csv    2011
xxx-2012.csv    2012
xxx-2013.csv    2013
xxx-2014.csv    2014
xxx-2015.csv    2015
xxx-2016.csv    2016
xxx-2017.csv    2017
Hello pir4t3x,

This is part of what I have used in the past. It is old C style, but works. This code is part of a larger header file I use, so I think it will show you everything that will get you started. I have not tested this smaller portion, so if it is a problem let me know.

1
2
3
4
5
6
7
8
9
10
#include <ctime>  // <--- Header file for using "time_t" and "tm"

time_t rawtime;
struct tm Tm;

rawtime = time(0);
localtime_s(&Tm, &rawtime);


constexpr int currentYear = Tm.tm_year + 1900;  // <--- Change to "const" if a problem. 


Hope that helps,

Andy
It's a little bit unclear what you are asking.

If you trying to extract the integer year from a filename then try:
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
#include <iostream>
#include <string>
using namespace std;

//==========

int getYear( string filename, string suffix )
{
   int pos = filename.find( suffix );

   if ( pos == string::npos || pos < 4 )
   {
      cout << "Invalid filename\n";
      return 0;
   }
   
   string yearString = filename.substr( pos - 4, 4 );
   return stoi( yearString );
}

//==========

int main()
{
   string filename = "xxx-2017.csv";
   string suffix   = ".csv";

   cout << "Year is " << getYear( filename, suffix ) << '\n';
}

//==========
Year is 2017




If you are after the current year to give to the name of a file (@Handy Andy's interpretation) then:
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
#include <iostream>
#include <string>
#include <ctime>
using namespace std;

//======================================================================


tm getTm()
{
   time_t tt;
   time( &tt );
   return *localtime( &tt );
}


//======================================================================


void splitTm( const tm &TM, int &year, int &month, int &day, int &hour, int &mins, int &secs, int &weekDay )
{
   year    = TM.tm_year + 1900;
   month   = TM.tm_mon ;
   day     = TM.tm_mday;
   hour    = TM.tm_hour;
   mins    = TM.tm_min ;
   secs    = TM.tm_sec ;
   weekDay = TM.tm_wday ;
}


//======================================================================


int main()
{
   int year, month, day, hour, mins, secs, weekDay;

   tm TM = getTm();
   splitTm( TM, year, month, day, hour, mins, secs, weekDay );
   cout << "Year = " << year << '\n';

   string prefix = "xxx-";
   string suffix = ".csv";
   string filename = prefix + to_string( year ) + suffix;
   cout << "Filename is " << filename << '\n';
}


//======================================================================
Year = 2017
Filename is xxx-2017.csv
Topic archived. No new replies allowed.