我有一个txt文件,我想从中读取数据。数据由项目输入组成,该输入具有以下属性:
numberofProjects
numofActivitiesperProject
numofResources
maxAvailableResources[numofResources]
,以及每项活动的持续时间和资源使用情况。例如,如果一个项目总共有30个任务,那么我们每30个任务有30个持续时间和资源使用,总数为numofResources
。
我已成功从文本文件中读取以下内容,但无法读取其他信息。
numofActivitiesperProject
numofResources
maxAvailableResources[numofResources]
具体而言,我无法将数据读取到activityList [k]->duration
和activityList [k]->maxResourceUse[j]
。
问题是什么?
int numofProjects;
int numofActivitiesperProject;
int numofResources;
int *maxAvailableResources;
typedef struct Activity {
int id;
int projectNo;
int nofPre; //number of predecessors
int nofSuc;
int *precedessor; //precedence array
int *successor; //successor array
int duration;
int *maxResourceUse; //resource usage if an activity
} Activity, PActivity;
Activity *activity;
Activity **activityList;
Activity *readFile(char *fileName) {
int i = 0, j, k;
int *numOfPres;
int Total;
FILE *fd = fopen(fileName, "r");
if (fd == NULL) {
printf("unable to open file %s\n", fileName);
exit(1);
}
fscanf(fd, "%d%d%d", &numofProjects, &numofActivitiesperProject, &numofResources);
maxAvailableResources = (int*) malloc(sizeof(int) * numofResources);
for (i = 0; i < numofResources; i++) {
fscanf(fd, "%d", &maxAvailableResources[i]);
}
Total =numofProjects*numofActivitiesperProject;
activityList = (Activity **) malloc(sizeof(Activity *) * (Total));
activity=(Activity *) malloc(sizeof(Activity));
numOfPres = (int *) malloc(sizeof(int) * (Total));
activity->maxResourceUse = (int*) malloc(sizeof(int) * numofResources);
for (k=0;k<Total;k++){
//activityList [k]->duration = (int) malloc(sizeof(Total));
activity->id=k+1;
fscanf(fd, "%d", &(activity->duration));
for (j = 0; j < numofResources; j++)
fscanf(fd, "%d", &(activity->maxResourceUse[j]));
activityList[k]=activity; //THERE IS A PROBLEM HERE...
}
}
fclose(fd);
}
void main() {
readFile ("file.txt");
_getch();
}
读取输入的Txt文件:
10
10
6
11 9 7 9 11 20
20 0 0 0 0 0 5
goes on like this...
输入文件的格式如下:
Number of projects
Number of activities per project
Number of resources
R1 R2 R3 R4 R5 R6 ....Rk Total amount of each resource
Duration Activitity-1 of project-1, Required resources of Activitity-1 of project-1
Duration Activitity-2 of project-1, Required resources of Activitity-2 of project-1
...
Duration Activitity-n of project-1, Required resources of Activitity-n of project-1
Duration Activitity-1 of project-2, Required resources of Activitity-1 of project-2
...
...
Duration Activitity-1 of project-m, Required resources of Activitity-1 of project-m
Duration Activitity-2 of project-m , Required resources of Activitity-n of project-m