0
点赞
收藏
分享

微信扫一扫

【C++指南】类和对象(三):类的默认成员函数——全面剖析: 析构函数

王远洋 2024-10-14 阅读 13

读取并打印

#include <stdio.h>  
#include <stdlib.h>  
#include <unistd.h>  
  
#define BUFFER_SIZE 1024  
  
void print_file_lines(const char *filename) {  
    FILE *file = fopen(filename, "r");  
    if (file == NULL) {  
        perror("Error opening file");  
        exit(EXIT_FAILURE);  
    }  
  
    char buffer[BUFFER_SIZE];  
    while (fgets(buffer, BUFFER_SIZE, file) != NULL) {  
        printf("%s", buffer);  
    }  
  
    fclose(file);  
}  
  
int main(int argc, char *argv[]) {  
    if (argc != 2) {  
        fprintf(stderr, "Usage: %s <filename>\n", argv[0]);  
        return EXIT_FAILURE;  
    }  
  
    const char *filename = argv[1];  
  
    while (1) {  
        print_file_lines(filename);  
        sleep(3);  // Sleep for 3 seconds  
    }  
  
    return EXIT_SUCCESS;  
}

写入

#include <stdio.h>  
#include <stdlib.h>  
#include <unistd.h>  
#include <time.h>  
  
#define FILENAME "execution_count.txt"  
  
int main() {  
    FILE *file = fopen(FILENAME, "a"); // 使用 "a" 模式以追加方式打开文件  
    if (file == NULL) {  
        perror("Error opening file");  
        return EXIT_FAILURE;  
    }  
  
    int count = 0;  
    while (1) {  
        // 获取当前时间戳(可选,用于在文件中记录时间)  
        time_t now = time(NULL);  
        char time_str[20];  
        strftime(time_str, sizeof(time_str), "%Y-%m-%d %H:%M:%S", localtime(&now));  
  
        // 构造要写入的内容  
        char buffer[100];  
        snprintf(buffer, sizeof(buffer), "[%s] Execution count: %d\n", time_str, count);  
  
        // 写入文件  
        if (fputs(buffer, file) == EOF) {  
            perror("Error writing to file");  
            fclose(file);  
            return EXIT_FAILURE;  
        }  
  
        // 更新执行次数  
        count++;  
  
        // 关闭文件(每次写入后关闭,但这样效率较低,通常可以移到循环外并在程序结束时关闭)  
        // 注意:为了效率,这里我们不移除 fclose(file); ,但在实际应用中可能会选择其他方式处理文件关闭  
        fclose(file);  
  
        // 等待5秒  
        sleep(5);  
  
        // 重新打开文件以追加(因为我们在循环中关闭了文件,所以需要重新打开)  
        file = fopen(FILENAME, "a");  
        if (file == NULL) {  
            perror("Error reopening file");  
            return EXIT_FAILURE;  
        }  
    }  
  
    // 注意:由于上面是一个无限循环,这里的代码实际上永远不会被执行到。  
    // 如果你想要一个能够正常退出的程序,你需要在循环内部添加一个退出条件。  
  
    // 正常情况下,应该在退出前关闭文件  
    // fclose(file); // 这行代码在上面的无限循环中不会被执行到  
  
    return EXIT_SUCCESS; // 这行代码同样不会被执行到,因为上面是一个无限循环  
}

下面写入方式是无效的(需要用上边每次重新打开的方式写入)

#include <stdio.h>  
#include <stdlib.h>  
#include <unistd.h>  
#include <time.h>  
  
#define FILENAME "execution_count.txt"  
#define MAX_COUNT 10 // 假设我们想要运行10次后退出  
  
int main() {  
    FILE *file = fopen(FILENAME, "a");  
    if (file == NULL) {  
        perror("Error opening file");  
        return EXIT_FAILURE;  
    }  
  
    int count = 0;  
    while (count < MAX_COUNT) {  
        time_t now = time(NULL);  
        char time_str[20];  
        strftime(time_str, sizeof(time_str), "%Y-%m-%d %H:%M:%S", localtime(&now));  
  
        char buffer[100];  
        snprintf(buffer, sizeof(buffer), "[%s] Execution count: %d\n", time_str, count);  
  
        if (fputs(buffer, file) == EOF) {  
            perror("Error writing to file");  
            fclose(file);  
            return EXIT_FAILURE;  
        }  
  
        count++;  
  
        sleep(5);  
    }  
  
    fclose(file);  
    return EXIT_SUCCESS;  
}
举报

相关推荐

0 条评论