2017-09-18 8 views
0

出力ファイルに格納するためのfprintfを使用したソリューション(2D配列)の形式に関する基本的な問題があります。ここC言語 - 出力ファイルへのソリューションの保存 - 次の行の前にスペース文字を使用しない

(寸法列の行のsize_tot_xsize_tot_yの)この "x0" の2Dアレイをfprintfの私のコードの一部:

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 

私の問題は、次の行の前に各行の最後に1つのスペースがあることです。それは実際に上記のデータのソリューションでは表示されませんが、このファイルを編集するときにこのスペースが表示されます。私は問題がfprintf(file,"\n");から来ると思う:確かに、それは次の行の前に単一のスペースを加えるようである。

この行の各行でこの1つのスペースを防ぐ方法を教えてください。

よろしく

+0

方法: 'のため(J = 0; J BLUEPIXY

+0

あなたは確かに' fputc'を意味します。とにかく、これは簡単な解決策です、ありがとう! – youpilat13

+0

ああ、はい、私の悪いです。 'putchar( '')' - > 'fputc( ''、file)' – BLUEPIXY

答えて

1

は、データの列に区切り文字を入れるには大きく分けて二つの方法があります。

  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 
    } 
    
関連する問題