简体   繁体   English

使用fopen()的最佳方法是什么

[英]What is the best way to use fopen()

I am in my internship and Im a working on a projet of SLAM simulation, and I'm having a little problem while writing my cloud points into my file. 我正在实习,我正在研究SLAM模拟项目,在将云点写入文件时遇到了一些问题。

Here is my code, I have two functions : 这是我的代码,我有两个功能:

        void ecriture_fichier(double x, double y){
            FILE *fichier = NULL;
            fichier = fopen("/home/stagiairepierrick/points/points.txt", "a");
            if(fichier != NULL ){ 
                fprintf(fichier, "%lf %lf \n",x,y);
            } else {
                std::cerr << "probleme ecriture" << std::endl;
            }
            fclose(fichier);
        }

        void ajout_nombre_lignes(long nombre_lignes){
            FILE *fichier = NULL;
            fichier = fopen("/home/stagiairepierrick/points/points.txt", "r+");
            if(fichier != NULL ){ 
                fseek(fichier, 0, SEEK_SET);
                fprintf(fichier, "%ld \n", nombre_lignes);
            } else {
                std::cerr << "probleme ecriture nombre de lignes" << std::endl;
            }
            fclose(fichier);
        }

The function ajout_nombre_ligne is called just before ecriture_fichier . 函数ajout_nombre_ligneecriture_fichier之前被调用。 I want the first lign to be the number of ligne (variable nombre_lignes ), however when I execute this code, I get the following result in my file: 我希望第一个输入是ligne的数量(变量nombre_lignes ),但是当我执行此代码时,在文件中得到以下结果:

8070 
00000 5.200000 
5.200000 5.200000 
5.200000 5.200000 
5.200000 5.200000 
5.200000 5.200000 
5.200000 5.200000 

For each char added on my first line, I can see that one my char of the second line is being removed, I understand why but I can't find any solution. 对于第一行中添加的每个字符,我可以看到第二行中的一个字符已被删除,我知道为什么,但是找不到任何解决方案。

Could you guys help me? 你们能帮我吗?

There's nothing wrong with the behaviour. 行为没有错。

If you write a character mid file or somewhere not at the end, you overwrite whatever is at that position. 如果您写一个字符中间文件或不在结尾处的某个位置,则将覆盖该位置的任何内容。

If you want to write somewhere else than to the end (append), you will have to read and write the parts of the file you did not modify. 如果要写到末尾(追加)之外的其他地方,则必须读写文件中未修改的部分。

Also, you might want to use std::ifstream / std::ofstream / std::fstream instead. 另外,您可能想使用std::ifstream / std::ofstream / std::fstream

fseek is primarily for searching in binary files - there are no line-based files in C/C++. fseek主要用于搜索二进制文件-C / C ++中没有基于行的文件。

I understand that you want to append a line, and then update the first line based on the total number of lines - and the only possibility I can see is to use space/zero to pad the number of lines to ensure that it is always the same length, ie something like: 我了解您要添加一行,然后根据总行数更新第一行-我唯一看到的可能性是使用空格/零填充行数以确保它始终是相同的长度,即:

 fprintf(fichier, "%10ld \n", nombre_lignes);

I am not sure that the desired behavior is standardized. 我不确定所需的行为是否标准化。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM