c - 为什么我使用 fopen fclose c++ linux 在我的书面文件中有 NUL 行?

标签 c linux fclose nul

我有一些 C 代码在 Linux 操作系统上运行到可移植设备中。我正在使用 VRmagic 系统,类似于 BeagleBone 等...)。

在此代码中,我使用以下函数将结果写入 txt 文件。


//globale definition
FILE *logfile;
const char *logpath = "/MY_DEVICE/log.txt";
const char *main_folder_result_path = "/MEASUREMENT_RESULTS/";
const char *all_measurement_results_file_name = "all_computation_data.txt";

void save_to_log_file(const char *logpath,const char *message){
    #ifdef savealllog
        logfile = fopen(logpath,"a");  //logpath = "/MY_DEVICE/log.txt";
        fprintf(logfile,"%s",message);
        fclose(logfile);
    #endif
    #ifdef printalloutput
        printf("%s",message);
    #endif
}

void append_all_measurement_file(){

    char buff[255];
    char filename[255];

    save_to_log_file(logpath,"     Appending all measurement file...");

    sprintf(filename,"%s%s",main_folder_result_path,all_measurement_results_file_name);

    //here after we create the header if the file does not exist already
    FILE *pFile_all_measurement_results = fopen(filename, "r"); //lets try to read the file
    if (pFile_all_measurement_results == NULL){ // if file does not exist
        pFile_all_measurement_results = fopen(filename, "w");
        fprintf(pFile_all_measurement_results,"date-time S_type Part_name batch count abs value_1 value_2 value_3 value_4\n");
        fclose(pFile_all_measurement_results);
    }
    else{
        fclose(pFile_all_measurement_results); //if file does exist then we have to close it here
    }

    //here after we are going to write results
    pFile_all_measurement_results = fopen(filename, "a"); //lets open the file in append mode

    fprintf(pFile_all_measurement_results,"%s %s %s %d %d %d ",dateandtimetps.dt,measurement_type_str,Name_Str, batch_number,count_number,absolute_measurement_number);
    fprintf(pFile_all_measurement_results,"%.03f ", value_1);
    fprintf(pFile_all_measurement_results,"%.03f ", value_2);
    fprintf(pFile_all_measurement_results,"%.03f ", value_3);
    fprintf(pFile_all_measurement_results,"%.03f\n", value_4); //(there are a bit more in reality.....)

    fclose(pFile_all_measurement_results); //we can now close the file

    save_to_log_file(logpath,"done.\n");
}

99.9% 的时间一切正常。 但是,随机地,我的文件中确实有一些 NUL 字符,当我关闭系统时会发生这种情况。

看起来文件由于某种原因没有正确关闭或类似的东西.....

当我得到我的 txt 文件,并在我的电脑上用 Notepad++ 打开它时,它看起来像下面这样:

NUL line in the file

我可以确认设备已在第 172 行和第 174 行之间关闭。

非常感谢您的帮助

最佳答案

在 Unix 系统上关闭文件并不意味着内容会立即写入磁盘;通常启用缓冲/缓存。

要确保内容已写入,您可以使用 sync()在写入和/或关闭操作之后立即执行,这将最大限度地降低丢失更新的风险。

此外,我建议使用像 ext4 这样的日志文件系统(这可能不适用于 USB 笔等外部驱动器,但强烈建议用于所有系统和数据分区)。这不会在电源故障或崩溃的情况下避免数据丢失,但会避免像您遇到的不一致/部分写入。

关于c - 为什么我使用 fopen fclose c++ linux 在我的书面文件中有 NUL 行?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47693391/

相关文章:

c - 在终止程序之前存储值

c - 在 C 中,什么时候将 char 类型转换为整数不会返回 0 到 127 之间的值?

python - 关于管道 stdio 和 subprocess.Popen

linux - TCP 协议(protocol)的代理

c - fclose(stdout) 在使用 freopen 时即使在 NULL 检查后也会导致崩溃

jquery - 图像处理编程

c - 使用 PIC 18F4550 使 3 个 LED 闪烁

linux - 在 Linux 上使用 bazel 构建 tensorflow r1.12 后 libtensorflow.so 和 header 的位置

C - 使用相同的指针打开不同的文件

c - 是否存在一种无需使用全局变量即可在 atexit 或类似程序中释放内存的方法?