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");为什么还是出错,求大神指点一下,小弟不胜感激。。。。。。。