Question

I can't get it, why I can't add data to the file with this code

Here I am using a+, but it doesn't append to the end of the file at all

double massiv_of_yi[43];

FILE *file;

file = fopen("p_all.txt", "a+");
ofstream fout ("p_all.txt");
fout << "New variables " << "\n";

for(int i = 0; i < 43; i++) {

   double returned_xl = f(xl, Li[i], di[i], L0);
   double returned_xr = f(xr, Li[i], di[i], L0);

   while ( abs(returned_xl)>EPS || abs(returned_xr)>EPS ) {   
      n = n + 1;                   
      xd = xd / 2;                  
      xm = xl + xd;                 
      signfxl = ( returned_xl > 0 ) ? 1 : -1;
      signfxm = (f(xm, Li[i], di[i], L0) > 0 ) ? 1 : -1;

      if ( signfxl != signfxm ) {    
         xr = xm;                  
         break;
      } else {                        
         xl = xm;                   
         break;
      }
   }
   fout << i+1 << ") " << (xl + xr) / 2 << "\n";
   massiv_of_yi[i] = (xl + xr) / 2; 
}

   fout << "\n" << "\n";

   fclose(file);

I will appreciate for your help, thank you in advance!

Was it helpful?

Solution

You are mixing two distinct streams: The C-stream opened with fopen and the C++ stream opened with the default open-mode. In your code, the first is plain useless.

You might try:

#include <fstream>
int main() {
    std::ofstream fout ("p_all.txt", std::ios_base::app);
    fout << "Hello\n";
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top