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

标签 c printf carriage-return

我有一个关于使用 fprintf 将其存储到输出文件的解决方案(二维数组)格式的基本问题。

这里是我的代码部分,其中 fprintf 这个“x0”二维数组(行的尺寸为 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");
   }

和文件内容:

 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 

我的问题是,每行末尾在下一行之前有一个空格。上面的解决方案数据并未真正显示出来,但在编辑此文件时会出现此空格。我认为问题来自于 fprintf(file,"\n"); :确实,它似乎在下一行之前添加了一个空格。

如何防止行尾出现这个单一空格?

最佳答案

在数据序列中放置分隔符的方法大致有两种。

  1. 不要向第一个元素输出分隔符。以下元素在输出元素之前输出分隔符。

    //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. 输出除最后一个元素之外的元素后输出分隔符。

    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
    }
    

关于C 语言 - 将解决方案存储到输出文件中 - 防止下一行之前出现空格字符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46270032/

相关文章:

shell - 回车符在 PowerShell ISE 中无法按预期工作

文本编辑器显示\r\n?

C : Generating structure variable name from inputted number

c - 第二个 printf,与我的第一个 printf 相同,使程序崩溃

c - 每次在c中输出不同?

c - 指向范围外变量的指针在 C 中如何表现?

firefox - 添加到多行文本框的额外字符

c - 如何在 OpenCV 中访问 3d 矩阵的切片

c - gcc 4.4.3 下 C 代码中的奇怪行为

c - Computet 是否将每个 ASCII 数字(二进制​​)转换为其等价数字(二进制​​)?