0

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.

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. 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:

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?

Lightness Races in Orbit
  • 378,754
  • 76
  • 643
  • 1,055
shenki
  • 3
  • 1
  • 8
    best way is to not use `fopen`. It's a c function. Use `std::ifstream`, if you are programming in c++. – Paul Rooney May 10 '17 at 09:10
  • 2
    Your whole approach is wrong. Don't open/close your file for each line you're writing. Open the file once, then do all the writes and close the file once all writes have been done. – Jabberwocky May 10 '17 at 09:16
  • 1
    Sorry but this question isn't clear. You haven't told us what the output is supposed to be instead. You say you understand the problem but didn't tell us what it is. – Lightness Races in Orbit May 10 '17 at 09:16
  • And `ajout_nombre_lignes` won't work as expected, you cannot _insert_ content into a file (see Blacktempel's answer below). – Jabberwocky May 10 '17 at 09:17
  • Oh, im sorry, actually my file is being modified at a high rate and stop when my application close, so I can't really close it at the end. I'll do what Blacktempel told me, thank you anyway – shenki May 10 '17 at 09:18
  • Change `ajout_nombre_ligne` to mode `w` instead of `r+` – M.M May 10 '17 at 09:20
  • @shenki you should tell us more about what you are _actually_ trying to achieve. Your comment _actually my file is being modified at a high rate and stop when my application close, so I can't really close it at the end._ doesn't make sense without further information. – Jabberwocky May 10 '17 at 09:22
  • 2
    Every non-English identifier in your SO code example halves¹ the number of people who are willing to read it. ¹My personal estimate not backed by hard experimental data. – n. m. could be an AI May 10 '17 at 09:24
  • @MichaelWalz well it doesn't make sense however you spin it. – n. m. could be an AI May 10 '17 at 09:27
  • @n.m. yes, you're probably right. – Jabberwocky May 10 '17 at 09:28

2 Answers2

2

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.

Blacktempel
  • 3,935
  • 3
  • 29
  • 53
1

fseek is primarily for searching in binary files - there are no line-based files in 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, i.e. something like:

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

I am not sure that the desired behavior is standardized.

Hans Olsson
  • 11,123
  • 15
  • 38
  • I understand what you mean and thank for your answer, however I have to keep a specific format, i'll find another way, thank you anyway – shenki May 10 '17 at 09:25
  • If "specific format" means "text file with numbers but with no extra spaces before or after each number", then it's a seriously ill-conceived format and you may want to start questioning the reasons why it was put in place. – n. m. could be an AI May 10 '17 at 09:46