【问题标题】:Why is my file being overwritten when it is not supposed to?为什么我的文件不应该被覆盖?
【发布时间】:2021-05-05 16:59:57
【问题描述】:

我正在用 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

【问题讨论】:

  • 您在start() 中做的第一件事是std::ofstream routine(fileName);,它将截断文件。您可能应该使用std::ifstream routine(fileName) 并将打开的文件通过引用传递给oldRoutine。
  • 当std::ofstream file{fileName}; 打开一个文件时,它会删除,“截断”,当前内容。如果您希望添加到文件,请考虑添加ios::app 打开模式。 Documentation link
  • 如果合适的话,Ted 的更新评论有一个更好的选择。不过,了解开放模式。他们会在以后为您节省大量时间。
  • 好的,非常感谢你们的回复和帮助。我会了解你向我介绍的这些新事物,如果我有什么要问的,我会再次回复^_^

标签: c++ time fstream stringstream


【解决方案1】:

如果我理解正确,您想检查一个文件是否已经存在。在您的代码中,您创建一个 std::ofstream 对象,该对象创建一个新文件: std::ofstream routine(fileName);

如果您想从现有文件中读取,请尝试std::ifstream(解释为here)。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-02-21
    • 2011-09-22
    • 2015-07-31
    • 1970-01-01
    • 2020-03-06
    • 2019-10-06
    • 1970-01-01
    相关资源
    最近更新 更多