-5

这是我遇到错误的代码....

for (i=0; i<portcount; i++)
{   
    printf("%f ", ccds[i]/100000);
    fp=fopen("/administrator/IDS/et.dat", "a");
    //fprintf(fp, "er");        
    fprintf(fp, "%d ", (int)ccds[i]/100000);
    fclose(fp); 
}
4

1 回答 1

6

You should check that the file actually opens successfully, and probably also put the file open/close outside the loop:

fp = fopen("/administrator/IDS/et.dat", "a");
if (fp == NULL)             // always check for success when opening a file
{
    perror("fopen failed");
}
else
{
    for (i = 0; i < portcount; i++)
    {   
        fprintf(fp, "%d ", (int)ccds[i]/100000);
    }
    fclose(fp); 
}
于 2012-06-15T08:06:26.790 回答