新人请教问题,可有偿,如果积分不够可加
有一个关于文件读写的问题这是main.cpp
#include<iostream>
#include <string.h>
#include <stdlib.h>
#include"map.h"
#include"model.h"
#define maxlen 100
#define None false
int BUS_NUM=4;
int STATION_NUM=30;
int ROUTE_NUM=56;
using namespace std;
struct BusMap busmap;
struct BUSES
{
char busname[maxlen];
};
struct BUSESNUM
{
int busnumber;
int busstart;
int busend;
};
struct STATIONS
{
char stationname[2*maxlen];
};
struct ROUTES
{
int firstnum;
int secondnum;
int thirdnum;
int distancenum;
};
int LoadMapDate()
{
struct BUSES BUSES[BUS_NUM];
struct BUSESNUM BUSESNUM[BUS_NUM];
struct STATIONS STATIONS[STATION_NUM];
struct ROUTES ROUTES[ROUTE_NUM];
FILE *busdata=fopen("buses.TXT","r");//要读取的文件指针
FILE *busnumdata=fopen("busesnumbers.TXT","r");
FILE *stationdata=fopen("stations.TXT","r");
FILE *routedata=fopen("routes.TXT","r");
busmap.bus_num=BUS_NUM;
busmap.station_num=STATION_NUM;
busmap.buses=(Bus*)malloc(sizeof(Bus)*BUS_NUM);
busmap.stations=(Station*)malloc(sizeof(Station)*STATION_NUM);
int i=0;
if((busdata=fopen("buses.TXT","r"))==NULL)//判断文件是否为空
{
cout<<"Can not open this file\n"<<endl;
return 0;
}
while(!feof(busdata))//从文件中读取数据
{
fscanf(busdata,"%s\n",&BUSES[i].busname);
busmap.buses[i].name = BUSES[i].busname;
busmap.buses[i].start=busmap.buses[i].end=None;
//cout<<busmap.buses[i].name<<endl;
i++;
}
fclose(busdata);
}
int FindBus(char *bus)//查找Bus对象
{
for(int i=0;i<BUS_NUM;i++){
//cout<<busmap.buses[i].name<<endl;
if(strcmp(busmap.buses[i].name,bus)==0){
return i;
}
}
return -1;
}
int main(){
LoadMapDate();
// cout<<busmap.buses[1].name;
for(int i=0;i<BUS_NUM;i++){
cout<<busmap.buses[i].name<<endl;
}
}
model.h
typedef struct Bus
{
char *name;//公交名
int start;//起点
int end;//终点
}Bus;
typedef struct Station
{
char *station;//站点名
struct Route*routes;//从该站点出发的所有下行路线的链域
}Station;
typedef struct Route
{
int station;//指向的站点索引号
int bus;//公交索引号
int distance;//两站之间公路的距离
struct Route*next;//起始站点相同的,下一条下行路线
}Route;
typedef struct Path
{
int station_num;//路径中站点数
char **stations;//路径中站点数组
char **buses;//站点与站点之间的路径数组
int transfer;//换乘次数;
int need_time;//所需时间
struct Path *next;//指向下一个路径结点
}Path;
typedef struct BusMap
{
Bus *buses;//公交线路数组
Station *stations;//站点数组
int station_num;//站点数
int bus_num;//公交线路数
} BusMap;
map.h
int LoadMapDate();//读入信息
int FindBus(char *bus);//查找Bus对象
int FindStation(char *station);//查找Station对象
int GetBus(char *bus);//插入一个公交路线
int GetStation(char *station);//插入一个站点
void AddBus(int bus,int pStart,int pEnd);//添加公交信息
int AddRoute(int pBus,int pStart,int pEnd,int distance);//添加路段信息,形成邻接表
int Contrast(int *busnumber,int busname,int number);
int QueryStation(char *pStation);//查询站点信息,输出该站点所经线路信息
void QueryBus(char *pBus);
运行结果是这样
13
?