如何使用C++删除csv格式的24小时时间戳后的条目(行)?[已关闭]

yhuiod9q  于 2022-12-15  发布在  其他
关注(0)|答案(2)|浏览(172)

已关闭。此问题需要details or clarity。当前不接受答案。
**想要改进此问题?**添加详细信息并通过editing this post阐明问题。

22小时前关门了。
Improve this question
[1][This image for reference I run this it prints the time and entry name.](https://i.stack.imgur.com/J755w.png)
我需要从csv中删除该条目后24小时...我怎么才能使用C++删除它。有人请帮助破解这个。

#include <iostream>
#include <fstream>
#include <chrono>
using namespace std;
int main(){
    fstream myfile;
    string address_validation;
    myfile.open ("Data.csv", ios::out | ios::app);
    cout << "Enter the address:";
    
    
    cin >> address_validation;
    
    auto givemetime = chrono::system_clock::to_time_t(chrono::system_clock::now());
    cout<<ctime(&givemetime);
    myfile <<address_validation << ";" << ctime(&givemetime);         
}

这是我的代码有人请帮帮我..

wmtdaxz3

wmtdaxz31#

记住程序开始时得到的时间,用一个循环得到当前时间,用这个时间减去初始时间,如果结果是24h,则跳出循环,执行文件删除操作。
希望对你有帮助。
就像这样:

#include <iostream>
#include <ctime>

typedef void(*callback)(void*);
void settimer(unsigned int id, int msec, callback backcallfunc)
{
    if (msec < 0)
    {
        return;
    }
    clock_t start, finish;
    start = clock();
    double totaltime = 0;
    while (1)
    {
        finish = clock();
        totaltime = (double)(finish - start);
        if (totaltime > msec)
        {
            backcallfunc(&totaltime);
            break;
        }
    }
}

void delete_file(void*)
{
    // to do ...
    // Delete File Operation
}

int main(int argc, char* argv[])
{
    while (1)
    {
        settimer(1, 86400000 /* 24h */, delete_file);
    }
    return 0;
}
icnyk63a

icnyk63a2#

#include <iostream>
#include <fstream>
#include <string>
#include <ctime>

using namespace std;

string CharToStr(const char* contentChar)
{
    string tempStr;
    for (int i = 0; contentChar[i] != '\0'; i++)
    {
        tempStr += contentChar[i];
    }
    return tempStr;
}

void DelLineData(const char* fileName, int lineNum)
{
    ifstream in;
    in.open(fileName);

    string strFileData = "";
    int line = 1;
    char lineData[1024] = { 0 };
    while (in.getline(lineData, sizeof(lineData)))
    {
        if (line == lineNum)
        {
            strFileData += "\n";
        }
        else
        {
            strFileData += CharToStr(lineData);
            strFileData += "\n";
        }
        line++;
    }
    in.close();

    ofstream out;
    out.open(fileName);
    out.flush();
    out << strFileData;
    out.close();
}

typedef void(*callback)(void*);
void settimer(unsigned int id, int msec, callback backcallfunc)
{
    if (msec < 0)
    {
        return;
    }
    clock_t start, finish;
    start = clock();
    double totaltime = 0;
    while (1)
    {
        finish = clock();
        totaltime = (double)(finish - start);
        if (totaltime > msec)
        {
            backcallfunc(&totaltime);
            break;
        }
    }
}

void delete_file(void*)
{
    // to do ...
    // Delete File Operation
    DelLineData("Data.csv", 1);
}

int main(int argc, char* argv[])
{
    while (1)
    {
        settimer(1, 86400000 /* 24hrs */, delete_file);
    }
    return 0;
}

相关问题