How to use fprintf for writing data to a file

前端 未结 3 1028
后悔当初
后悔当初 2021-01-27 01:46

I want to write data from a C program to a file, so that Excel can read the file to plot a graph of the data. But I\'m not sure of the exact syntax to use for fprintf. I have st

相关标签:
3条回答
  • 2021-01-27 02:16

    Your logic should look something like this:

    fp = fopen( "out_file.txt", "w" ); // Open file for writing
    
    while ( some condition )
    {
    
        ... some calculations
    
        fprintf(fp, "x = %f, y = %f, vx = %f, vy = %f, time = %f, ", x,y,vx,vy,time);
    }
    
    fclose(fp);
    
    0 讨论(0)
  • 2021-01-27 02:22

    The stdio file type is FILE (all uppercase).

    0 讨论(0)
  • 2021-01-27 02:32
    #include <stdio.h>
    

    should be enough, but be careful because the structure's name is FILE (all uppercase) and not File. Finally, dont forget to close the file calling fclose()

    0 讨论(0)
提交回复
热议问题