将值写入C中的文件以用于排队系统

时间:2016-11-19 00:28:28

标签: c file file-writing

我为排队系统编写了一段代码用于模拟。我需要在编译代码后将一些值打印到txt文件。我编写了用于写入文件的代码,但是虽然它创建了文件,但它不会将值打印到我的文本文件中。如果你能帮忙,我会恭喜你。这是我的代码片段......

#include<stdio.h>
#include<time.h>
#include<stdlib.h>
#include<math.h>

int main()
{
float rho_start ; // start value of the utilization factor 
float rho_end ; // end value of the utilization factor
float rho_step ; // step value of the utilization factor
float lambda ; // ratio of customers and time
float N ; // mean number of customers in the system
float Q ; // mean number of customers in queue
float res_time ; // response time
float mu ; // service rate , assuming mu = 10 customers/s for this assignment
float i ; // for the loop

printf("Please enter the start value of the rho : ");
scanf("%f",&rho_start);

printf("Please enter the end value of the rho : ");
scanf("%f",&rho_end);

printf("Please enter the step value of the rho : ");
scanf("%f",&rho_step);

printf("Please enter the mu : ");
scanf("%f",&mu);

printf("RHO \t\t N \t\t Q \t\t RT \n");
printf("--- \t\t --- \t\t --- \t\t --- \n");
for( i = rho_start ; i < rho_end ; i = i + rho_step)
{
    N = i/(1-i) ;
    lambda = i * mu ;
    Q = (i*i)/(1-i) ;
    res_time = N/lambda ;
    printf("%.3f \t\t %.3f \t\t %.3f \t\t %.3f \n", i , N , Q , res_time);
}

    FILE * f1;

    f1 = fopen("sample.txt","w");

    if( rho_step == 0.1 )
    {   

        for(i = rho_start ; i < rho_end ; i = i + rho_step)
        {
            N = i/(1-i) ;
            fprintf(f1 , "%.3f \t\t %.3f \n" , i , N);

        }
    }



    if( f1 == NULL ) 
    {       
    printf("Cannot open file.\n");
    exit(1);
    }

    fclose(f1);




return 0;

}

0 个答案:

没有答案