| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1692 人关注过本帖
标题:c语言文件操作出错,求指点
只看楼主 加入收藏
tomye
Rank: 2
等 级:论坛游民
帖 子:32
专家分:10
注 册:2017-12-1
结帖率:33.33%
收藏
 问题点数:0 回复次数:3 
c语言文件操作出错,求指点
程序代码:
#include<stdio.h>
#include<time.h>
#include<string.h>
#include <stdlib.h>
///////////////////
const int v_max = 5;           
const double randomization = 0.3;  
const int road_length = 500;         
int v[2][600];
///////////////////////////////////
int lead[10];                        
int last[10];                        
const int car_num = 100;
int total_node_num, total_road_num, total_car_num, road_num, lattice, sample, time_run;
int rand_int()
{

    int seed;
    seed = rand() % 501;  //make a num 0-100;
    return seed;
}
//////////产生随机中子数,范围在0-1之间
double rand_double()
{
    return ((double)rand_int() / 100);
}
void add_car()
{

    if (rand_int() % 2 == 1)
    {
        if (last[0] > 0)
            v[0][0] = v_max;
    }
    else
    {
        if (last[1] > 0)
            v[1][0] = v_max;
    }
    //return 0;
    printf("尾车=%2d\n", last[0]);
    //getchar();

}
void inicars()
{
    FILE *ff;
    ff = fopen("初始化.txt", "w");
    int temp_1, i, j;
    for (i = 0; i<2; i++)
        for (j = 0; j<road_length; j++)
        {
            v[i][j] = -1;
        }
    ///////////////
    for (i = 0; i < 2; i++)
    {
        for (j = 0; j < car_num;)
        {
            temp_1 = rand_int() % road_length;
            if (v[i][temp_1] < 0)
            {
                v[i][temp_1] = rand_int() % (v_max + 1);
                j++;
                fprintf(ff, "道路=%2d,weizhi=%2d,sudu=%2d\n", i, temp_1, v[i][temp_1]);
            }
            
        }
        
    }
    printf("1");
    //getchar();
}
void update_lattice()
{
    //ofstream Vfile("V_1.txt", ios::app);
    FILE *fp;
    fp = fopen("wz_1.txt", "a");
    int temp_1, temp_2, temp_3;
    for (road_num = 0; road_num < 2; road_num++)
    {
        for (lattice = last[road_num]; lattice <= lead[road_num]; lattice++)
        {
            if (v[road_num][lattice] >= 0)//////////////
            {
                if (v[road_num][lattice] < v_max)   
                {
                    v[road_num][lattice]++;
                }

                if (lattice < lead[road_num])  
                {
                    for (temp_1 = 1; temp_1 <= v_max; temp_1++)    /
                    {
                        if (v[road_num][lattice + temp_1] >= 0 )
                        {
                            v[road_num][lattice] = temp_1 - 1; break;        
                        }
                    }
                }
                else if (lattice == lead[road_num])
                {
                    if (v[road_num][lattice] >= road_length - lattice - 1)
                    {
                        v[road_num][lattice] = -1;   //
                    }
                }
                if (rand_double() < randomization && v[road_num][lattice] > 0)
                {
                    v[road_num][lattice]--;
                }
                //////////////////////////////

            }
            ///////////////////////////////////////////////
            if (v[road_num][lattice] >= 0)
            {
                temp_2 = lattice + v[road_num][lattice];
                if (temp_2 < road_length)
                {
                    v[road_num][temp_2] = v[road_num][lattice];
                    v[road_num][lattice] = -1;
                }
                else
                {
                    v[road_num][lattice] = -1;
                }
                if (road_num == 0)
                    fprintf(fp, "道路:%2d,位置=%2d\n", road_num, lattice);
                //    Vfile << road_num << "    " << lattice << endl;
            }
        }
        fputs("\n", fp);
    }
    //Vfile.close();
    fclose(fp);
    //return 0;
}
void main()
{
    srand(time(NULL));
    int time_count;
    inicars();
    FILE *p;
    p = fopen("时间.txt", "w");
    for (time_count = 0; time_count < 1000; time_count++)
    {
        for (road_num = 0; road_num < 2; road_num++)
        {
            for (lattice = 0; lattice < road_length; lattice++)
            {
                if (v[road_num][lattice] >= 0)
                    last[road_num] = lattice; break;
            }
        }
        ///////////////

        for (road_num = 0; road_num < 2; road_num++) ////////////////
            for (lattice = road_length - 1; lattice > -1; lattice--)
            {
                if (v[road_num][lattice] >= 0)
                {
                    lead[road_num] = lattice;    break;
                }
            }
        ///////////////////////////////////////

        add_car();
        update_lattice();
        fprintf(p, "time=%2d\n", time_count);
        printf("%d\n", time_count);

    }
    fclose(p);
    printf("ok");
}



出错的地方在fprintf(fp, "道路:%2d,位置=%2d\n", road_num, lattice);
                //    Vfile << road_num << "    " << lattice << endl;
            }
        }
        fputs("\n", fp);
    }
    这里为什么只循环了一次,按正确的想法是一个时间步有100个数据,总共有1000*100个数据,,,,,,, 但是输出的文件只有100个,我命名文件操作类型是追加型fp = fopen("wz_1.txt", "a");为什么还是出错,求大神指点一下,小弟不胜感激。。。。。。。
搜索更多相关主题的帖子: 文件 出错 int txt for 
2018-11-01 21:26
tomye
Rank: 2
等 级:论坛游民
帖 子:32
专家分:10
注 册:2017-12-1
收藏
得分:0 
在线等,谢谢大家
2018-11-01 21:27
wang20080441
Rank: 1
等 级:新手上路
帖 子:4
专家分:8
注 册:2018-10-30
收藏
得分:0 
回复 2楼 tomye
代码一时看不出什么问题,给你几个建议:
1、确认一下你的文件里那100个数据是不是最后的100个数据;
2、单步测试一下,确定循环是不是走了1000遍;
3、如果实在不行,有一个简单的解决办法:在main函数打开文件,把fp作为指针传进update函数,函数内只管写,不管开关。最后退出以后再关闭文件。
希望有帮助
2018-11-01 22:44
tomye
Rank: 2
等 级:论坛游民
帖 子:32
专家分:10
注 册:2017-12-1
收藏
得分:0 
回复 3楼 wang20080441
谢谢回答,写的数据我测试了一下是初始化的数据,也就是第一步写入的数据,往后随时间计算的那99步没写进去,
2018-11-04 11:40
快速回复:c语言文件操作出错,求指点
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.020788 second(s), 9 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved