以下是一个使用C++读取文本信息并与当前时间进行比较的示例代码:

#include <iostream>
#include <fstream>
#include <ctime>
#include <iomanip>

// 获取当前时间的字符串表示
std::string getCurrentTime() {
    time_t now = time(0);
    struct tm timeInfo;
    char buffer[80];
    timeInfo = *localtime(&now);
    strftime(buffer, sizeof(buffer), "%Y-%m-%d", &timeInfo);
    return buffer;
}

// 判断日期是否已经过期
bool isExpired(const std::string& date) {
    std::string currentDate = getCurrentTime();
    return date < currentDate;
}

int main() {
    std::ifstream file("text.txt");
    if (!file) {
        std::cout << "无法打开文件!" << std::endl;
        return 1;
    }

    std::string expirationDate;
    if (std::getline(file, expirationDate)) {
        if (isExpired(expirationDate)) {
            std::cout << "已过期!" << std::endl;
        } else {
            std::cout << "尚未过期!" << std::endl;
        }
    } else {
        std::cout << "无法读取到有效日期!" << std::endl;
        return 1;
    }

    file.close();
    return 0;
}

注意,这里的代码假设文本文件 "text.txt" 中只有一行内容,即到期日期,格式为 "YYYY-MM-DD"。你需要根据实际情况修改文件名和日期格式。

这个示例代码首先定义了一个 getCurrentTime 函数,用于获取当前时间的字符串表示。然后定义了一个 isExpired 函数,用于判断给定的日期是否已经过期。最后,在 main 函数中,打开文本文件并读取到期日期,然后调用 isExpired 函数进行判断,并输出结果

请用C++写一份读取文本信息与现行时间进行判断 是否到期不使用get_time库

原文地址: https://www.cveoy.top/t/topic/hUuR 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录