watermelon
watermelon

Reputation: 13

Why is my file not being created using std::ofstream file{fileName};

I am creating a routine manager app. I have taken the system time and changed to DD/MM/YY format and want to create a file 'routineDD/MM/YY' for a specific time.

I have tested my conversion to the format DD/MM/YY and it works. I just cant understand why my file is not being created. Could someone please help me out

Here is the code

#include <string>
#include <bits/stdc++.h>
#include <iostream>
#include <fstream>
#include <conio.h>
#include <sstream>
class task
{
    std::string name;
    int stime, etime;
};
//Time - Sun May 02 09:17:33 2021
int calcMon(std::string month)
{
    int fmonth;
    switch (month[0])
    {
    case 'F':
        fmonth = 02;
        break;
    case 'S':
        fmonth = 9;
        break;
    case 'O':
        fmonth = 10;
        break;
    case 'N':
        fmonth = 11;
        break;
    case 'D':
        fmonth = 12;
        break;
    default:
        break;
    }
    if (month[0] == 'A' && month[1] == 'p')
    {
        fmonth = 04;
    }
    if (month[0] == 'A' && month[1] == 'u')
    {
        fmonth = 07;
    }
    if (month[0] == 'J')
    {
        if (month[1] == 'a')
        {
            fmonth = 01;
        }
        if (month[1] == 'u' && month[2] == 'n')
        {
            fmonth = 06;
        }
        if (month[1] == 'u' && month[2] == 'l')
        {
            fmonth = 07;
        }
    }
    if (month[0] == 'M' && month[1] == 'a' && month[2] == 'r')
    {
        fmonth = 03;
    }
    if (month[0] == 'M' && month[1] == 'a' && month[2] == 'y')
    {
        fmonth = 05;
    }

    return fmonth;
}
std::string genFname(std::string time)
{
    std::cout << time << std::endl;
    std::stringstream time4, time5, time6, time8, time9, time22, time24, time23;
    time4 << time[4];
    time5 << time[5];
    time6 << time[6];
    time9 << time[9];
    time24 << time[24];
    time23 << time[23];
    time22 << time[22];
    time8 << time[8];
    std::string t4, t5, t6, t8, t9, t24, t23, t22;
    time4 >> t4;
    time5 >> t5;
    time6 >> t6;
    time8 >> t8;
    time23 >> t23;
    time24 >> t24;
    time22 >> t22;
    time9 >> t9;
    std::string month, day, year, timeIns;
    month = t4 + t5 + t6;
    day = t8 + t9;
    year = t22 + t23 + t24;
    month = std::to_string(calcMon(month));
    timeIns = t8 + t9 + "/" + month + "/" + t22 + t23 + t24;
    std::string finalFname = "routine" + timeIns+".txt";
    std::cout<<finalFname<<std::endl;
    return finalFname;
}
// sigh*
void setData(std::string s1)
{
    std::cout << "Utho" << std::endl;
    std::cout << s1 << std::endl;
    std::cout << "So jao";
}
void inp(void)
{
    std::string task1;
    std::cout << "Enter task: ";
    std::cin >> task1;
    setData(task1);
}
int main()
{
    time_t tt;
    struct tm *ti;
    time(&tt);
    ti = localtime(&tt);
    std::string time = asctime(ti);
    std::string finalFname = genFname(time);
    std::ofstream file{finalFname};
    // inp();
    getch();
}

I use a 64 bit computer and vs code as my code editor.

Thanks in advance

Upvotes: 1

Views: 59

Answers (1)

dabo42
dabo42

Reputation: 486

A slash (/) is the directory separator on many platforms. You can't use it in a file name. Your code tries to create a file YY inside directory routineDD/MM/, which will likely fail because there is no such directory. Use something other than '/' as separator, e.g. '.'.

Upvotes: 3

Related Questions