为什么我的文件不应该被覆盖?
Why is my file being overwritten when it is not supposed to?
我正在用 C++ 制作例程管理器应用程序。我创建了一个启动函数,用于检查是否已经为该日期创建了一个文件,如果没有,它可以让您计划您的例程。如果已经创建了例程,我希望我的程序将其读出...但是我的 .txt
文件正在被覆盖,因此没有任何内容被打印出来,我想知道为什么
代码如下:
#include <string>
#include <bits/stdc++.h>
#include <iostream>
#include <fstream>
#include <conio.h>
#include <sstream>
class task
{
public:
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::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";
return finalFname;
}
void newRoutine(std::string fileName)
{
std::ofstream file{fileName};
task tasks[100];
tasks[0].name = "wake up";
std::cout << "I will ask you for your tasks in a moment.\nJust enter 'bedtime' when its your time to sleep";
std::cout << "\nWhen do you plan to wake up?: ";
std::cin >> tasks[0].stime;
while (true)
{
int n = 1;
std::string tName, strN;
std::stringstream sample;
sample << n;
sample >> strN;
std::cout << "Enter task " + strN + ": ";
std::cin >> tName;
tasks[n].name = tName;
if (tName[0] == 'b' && tName[1] == 'e' && tName[2] == 'd' && tName[3] == 't' && tName[4] == 'i' && tName[5] == 'm' && tName[6] == 'e')
break;
n++;
}
}
void oldRoutine(std::string fileName)
{
std::ifstream routine(fileName);
std::string dummy;
do
{
getline(routine, dummy);
std::cout << dummy << std::endl;
} while (!routine.eof()); //eof stands for end of file
std::cout << "Thats it";
routine.close();
}
void start(std::string fileName)
{
std::ofstream routine(fileName);
if (routine.fail())
{
std::cout<<"No routine found for today"<<std::endl;
newRoutine(fileName);
}
if (routine.good())
{
std::cout<<"A routine found for today"<<std::endl;
oldRoutine(fileName);
}
routine.close();
}
int main()
{
time_t tt;
struct tm *ti;
time(&tt);
ti = localtime(&tt);
std::string time = asctime(ti);
std::string finalFname = genFname(time);
start(finalFname);
// inp();
getch();
}
输出
A routine found for today
That's it
如果我没理解错的话,你想检查一个文件是否已经存在。在您的代码中,您创建了一个 std::ofstream
对象,它创建了一个新文件:
std::ofstream routine(fileName);
如果您想从现有文件中读取,请尝试 std::ifstream
(已解释 here)。
我正在用 C++ 制作例程管理器应用程序。我创建了一个启动函数,用于检查是否已经为该日期创建了一个文件,如果没有,它可以让您计划您的例程。如果已经创建了例程,我希望我的程序将其读出...但是我的 .txt
文件正在被覆盖,因此没有任何内容被打印出来,我想知道为什么
代码如下:
#include <string>
#include <bits/stdc++.h>
#include <iostream>
#include <fstream>
#include <conio.h>
#include <sstream>
class task
{
public:
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::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";
return finalFname;
}
void newRoutine(std::string fileName)
{
std::ofstream file{fileName};
task tasks[100];
tasks[0].name = "wake up";
std::cout << "I will ask you for your tasks in a moment.\nJust enter 'bedtime' when its your time to sleep";
std::cout << "\nWhen do you plan to wake up?: ";
std::cin >> tasks[0].stime;
while (true)
{
int n = 1;
std::string tName, strN;
std::stringstream sample;
sample << n;
sample >> strN;
std::cout << "Enter task " + strN + ": ";
std::cin >> tName;
tasks[n].name = tName;
if (tName[0] == 'b' && tName[1] == 'e' && tName[2] == 'd' && tName[3] == 't' && tName[4] == 'i' && tName[5] == 'm' && tName[6] == 'e')
break;
n++;
}
}
void oldRoutine(std::string fileName)
{
std::ifstream routine(fileName);
std::string dummy;
do
{
getline(routine, dummy);
std::cout << dummy << std::endl;
} while (!routine.eof()); //eof stands for end of file
std::cout << "Thats it";
routine.close();
}
void start(std::string fileName)
{
std::ofstream routine(fileName);
if (routine.fail())
{
std::cout<<"No routine found for today"<<std::endl;
newRoutine(fileName);
}
if (routine.good())
{
std::cout<<"A routine found for today"<<std::endl;
oldRoutine(fileName);
}
routine.close();
}
int main()
{
time_t tt;
struct tm *ti;
time(&tt);
ti = localtime(&tt);
std::string time = asctime(ti);
std::string finalFname = genFname(time);
start(finalFname);
// inp();
getch();
}
输出
A routine found for today
That's it
如果我没理解错的话,你想检查一个文件是否已经存在。在您的代码中,您创建了一个 std::ofstream
对象,它创建了一个新文件:
std::ofstream routine(fileName);
如果您想从现有文件中读取,请尝试 std::ifstream
(已解释 here)。