c - 使用输出以下程序的循环编写程序

标签 c printf output

所以我编写了一个程序,将度数转换为弧度,然后将结果打印为输出。

 CODE (website not phone friendly)
 #include <stdio.h>
 #define PI 3.141593
 int main (){
     int degrees =10;
     double radians;
     printf("Degrees to Radians \n");

     while( degrees <= 360){
         radians = degrees*PI/180;
         printf("%6i %9.6f \n", degrees, radians);
        degrees +=10;
     }
 }
 END CODE

所以我希望能够使这个程序成为我的程序创建的实际输出。我有人向我建议 fprintf 但他们没有详细说明。我是 C 编程新手,谁能解释一下我如何使用 fprintf (也许是广泛的想法?)。我知道 fprint 是打印输出的另一种方式,但仅此而已。

最佳答案

以下是如何在代码中使用 fprintf

int main() {
    FILE * fp;
     int degrees =10;
     double radians;
     printf("Degrees to Radians \n");
    fp = fopen("radians.txt","w+");
     while( degrees <= 360){
         radians = degrees*PI/180;
         printf("%6i %9.6f \n", degrees, radians);
         fprintf(fp, "%6i\t%9.6f \n", degrees, radians);
        degrees +=10;
     }
    return 0;
}

这里我在 fprintf 中添加了 \t 以获取制表符空间。 fopen 中的 "w+" 将创建具有给定名称的新文件,因此所有输出都将保存在文件中。

关于c - 使用输出以下程序的循环编写程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41972906/

相关文章:

c++ - 具有无符号短整数的结构的大小

c++ - 为什么在 C++ 中必须从 void 指针进行类型转换?

c - 将 C 中 fork() 的结果记录到文件中以查看结果

c - printf 的 h 和 hh 修饰符的用途是什么?

c - printf,如何为整数插入小数点

c - 文件中没有输出

c - 关于箭头键和 fflush(stdin) 的输出

java - 如何将n个数字相加,相乘到数组中的给定范围,并以O(n)时间反转数组中的范围?

c - C 中的邻接矩阵上的 Dijkstra

c - Linux fork() 命令