0

My problem is the following when I show the third file, It shows a lot of 0 and not the ints of file 1 and file 2. My file ends up with 4,00 KB (4.096 bytes) for some reason I don't know. Btw, the third file should have first: the first int of file1 and then the first of file2 and so on

Edit: I was testing the problem, and I realize that the size of the third file is correct before I show it. It's 24 and that's the sum of file1 and file 2. But when I read it, then it goes crazy

 #include <iostream>

using namespace std;
int main()
{
  int buffer1, buffer2, buffer3;
  
  //What is written in file 1 and file 2
  int file1_content[] = {1,3,5};
  int file2_content[] = {2,4,6};

  FILE* f3, *f1, *f2;
  f1 = fopen("archivo1", "rb");
  f2 = fopen("archivo2", "rb");
  f3 = fopen("archivo3", "wb+");
  if(!f1)
  {
      cout<<"Error el archivo 1 no se pudo abrir"<<endl;
      return 1;
  }
  if(!f2)
  {
      cout<<"Error el archivo 2 no se pudo abrir"<<endl;
      return 1;
  }
  if(!f3)
  {
      cout<<"Error el archivo 3 no se pudo abrir"<<endl;
      return 1;
  }

   //write file 1 and file two in file 3
   while(fread(&buffer1, sizeof(int), 1, f1) && fread(&buffer2, sizeof(int), 1, f2) )
   {
      fwrite(&buffer1, sizeof(int), 1, f3);
      fwrite(&buffer2, sizeof(int), 1, f3);
   }
   fclose(f1);
   fclose(f2);

   //show file 3
   while(fread(&buffer3, sizeof(int), 1, f3))
   {
       cout<<buffer3<<" ";
   }
   //EXPECTED OUTPUT
   //1 2 3 4 5 6 
   fclose(f3);

    return 0;
}
  • 1
    You probably should look at all 3 files in a hex editor. – drescherjm Feb 19 '22 at 19:11
  • 3
    It is generally considered a poor practice to mix C-style Standard I/O with C++ iostreams, even if one is used exclusively for console output and the other is exclusively used for file I/O. I would recommend switching entirely over to using `` for the entire project. – Schol-R-LEA Feb 19 '22 at 19:14
  • Also, this code does not constitute a [minimal, complete and reproducible example](https://stackoverflow.com/help/minimal-reproducible-example), so I would recommend editing the code to provide such. – Schol-R-LEA Feb 19 '22 at 19:15
  • @273k I put that c flag because the file method is written in a c way but the rest is c++. That's the idea, write in c++ but with the c way – Facundo Borrás Feb 19 '22 at 19:16
  • @FacundoBorrás is the a specific reason why it needs to use STDIO files? – Schol-R-LEA Feb 19 '22 at 19:17
  • @Schol-R-LEA I see, however, I have to do it like this. But I will take note of that – Facundo Borrás Feb 19 '22 at 19:23
  • Also, is there a reason why you are reading the files into the buffers one integer at a time? It would make more sense and be more efficient to read in a full buffer's worth at a time (making certain to check the return value of `fread()`) and stepping through the buffers in the loop, rather than reading on every pass of the interleaving loop. – Schol-R-LEA Feb 19 '22 at 19:27
  • @Schol-R-LEA the reason is that I'm learning about binary files and I don't know how to do that (I will try it anyways, however you do that). Btw, I put new info about my bug. – Facundo Borrás Feb 19 '22 at 19:32
  • @AndreasWenzel that was a mistake when I upload it. I was testing stuff so forget to change it. – Facundo Borrás Feb 19 '22 at 19:56

1 Answers1

1

Your code has undefined behavior.

According to §7.21.5.3 ¶7 of the ISO C11 standard, when a file is opened in update mode (+), the following restrictions apply:

  1. Output shall not be directly followed by input without an intervening call to the fflush function or to a file positioning function (fseek, fsetpos, or rewind).
  2. Input shall not be directly followed by output without an intervening call to a file positioning function, unless the input operation encounters end-of-file.

In your program, you are violating rule #1. The easiest solution would be to add a call to rewind between the calls to fwrite and fread, which you should do anyway, because you want to start reading from the start of the file.

Andreas Wenzel
  • 22,760
  • 4
  • 24
  • 39
  • BIG THANKS. I ended up fixing it, but this way is much better and I learned a bunch of things. Btw, how the hell did you know that? Did someone tell you that? Or did you just read all of that? I didn't even know that documentation even exist. Well, I'm a beginner so. Thks – Facundo Borrás Feb 19 '22 at 21:35
  • @FacundoBorrás: These restrictions are mentioned in [the C documentation of fopen on cppreference.com](https://en.cppreference.com/w/c/io/fopen) (though strangely not in [the corresponding C++ documentation](https://en.cppreference.com/w/cpp/io/c/fopen), although they probably also applies in C++). That is probably how I first learnt about them. – Andreas Wenzel Feb 19 '22 at 22:38
  • @FacundoBorrás: Also, this is not the first time that I provided this answer. Here are two other answers of mine in which I provided a similar answer: [1](https://stackoverflow.com/a/70570765/12149471) [2](https://stackoverflow.com/a/70312086/12149471) It seems that you are not the only person making this mistake. :-) – Andreas Wenzel Feb 19 '22 at 22:42