Home > Software engineering >  How can I delete a entry(row) after a 24hrs timestamp in csv using C ?
How can I delete a entry(row) after a 24hrs timestamp in csv using C ?

Time:12-14

[1]This image for reference I run this it prints the time and entry name.

I need to delete the entry from csv after 24hrs..How can I delete it by using C . Someone please help to crack this.

#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);         
}
  

This is my code someone please help me..

CodePudding user response:

Remember the time obtained at the beginning of the program. Use a loop to obtain the current time. Use the time to subtract the initial time. If the result is 24h, it will jump out of the loop and execute the file deletion operation.

I hope it will help you.

like this:

#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;
}

CodePudding user response:

#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;
}
  • Related