简体   繁体   English

C语言-将解决方案存储到输出文件中-防止在下一行之前出现空格

[英]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. 我有一个关于fprintf的解决方案(2D数组)格式的基本问题,用于将其存储到输出文件中。

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) : 这里其fprintf中这种“我的代码的部分x0 ” 2D阵列(维度size_tot_x行和size_tot_y为列):

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"); 我认为问题来自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 ? 如何在行的两端防止出现单个空格?

Regards 问候

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 } 

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

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