Search code examples
cprintfcarriage-return

C language - Store solution into output file - prevent space character before next row


I have a basic issue about the format of a solution ( 2D array) with fprintf for storing it into an output file.

Here the part of my code which fprintf this "x0" 2D array (of dimensions size_tot_x for rows and size_tot_y for columns) :

for (i=0;i<size_tot_x;i++) {
      for (j=0;j<size_tot_y;j++)
         fprintf(file,"%15.11f ",x0[i][j]);
      fprintf(file,"\n");
   }

and the content of file :

 10.00000000000  10.00000000000  10.00000000000  10.00000000000  10.00000000000  10.00000000000  10.00000000000  10.00000000000 
 10.00000000000   9.94633107782   9.90329194436   9.87940702757   9.87940702757   9.90329194436   9.94633107782  10.00000000000 
 10.00000000000   9.89913542001   9.81824830799   9.77335934800   9.77335934800   9.81824830799   9.89913542001  10.00000000000 
 10.00000000000   9.86410551943   9.75512660855   9.69464787655   9.69464787655   9.75512660855   9.86410551943  10.00000000000 
 10.00000000000   9.84546649879   9.72154025406   9.65276637770   9.65276637770   9.72154025406   9.84546649879  10.00000000000 
 10.00000000000   9.84546649879   9.72154025406   9.65276637770   9.65276637770   9.72154025406   9.84546649879  10.00000000000 
 10.00000000000   9.86410551943   9.75512660855   9.69464787655   9.69464787655   9.75512660855   9.86410551943  10.00000000000 
 10.00000000000   9.89913542001   9.81824830799   9.77335934800   9.77335934800   9.81824830799   9.89913542001  10.00000000000 
 10.00000000000   9.94633107782   9.90329194436   9.87940702757   9.87940702757   9.90329194436   9.94633107782  10.00000000000 
 10.00000000000  10.00000000000  10.00000000000  10.00000000000  10.00000000000  10.00000000000  10.00000000000  10.00000000000 

My issue is that I have a single space at the end of each row before the next row. It is not really showed with above data of solution but this space appears when edit this file. I think problem comes from the fprintf(file,"\n"); : indeed, it seems to add a single space before the next line.

How to prevent this single space at each end of line ?


Solution

  • There are roughly two ways to put delimiters in the sequence of data.

    1. Do not output a delimiter to the first element. The following elements output delimiters before outputting elements.

      //Pseudocode
      for(int rows_index = 0; rows_index < rows_size; ++rows_index){
          for(int columns_index = 0; columns_index < columns_size; ++columns_index){
              if(columns_index != 0)//not first element
                  print_c(delimiter);
              print_e(elements[rows_index][columns_index]);
          }
          print_c(newline);//output record separator
      }
      
    2. Output the delimiter after outputting the element except the last element.

      for(int rows_index = 0; rows_index < rows_size; ++rows_index){
          for(int columns_index = 0; columns_index < columns_size; ++columns_index){
              print_e(elements[rows_index][columns_index]);
              if(columns_index != columns_size - 1)//not last element
                  print_c(delimiter);
          }
          print_c(newline);//output record separator
      }