Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <error.h>
- #include <memory.h>
- #define NAMELEN 255
- #define STATIONS 5
- struct station {
- int id;
- char name[NAMELEN];
- struct edge * edge;
- };
- struct station* stations;//Ãëîáàëüíûé êîíòåéíåð äëÿ ñòàíöèé
- struct edge {
- struct station * to;
- int weight;
- struct edge * next;
- };
- struct station * getStationById(int id)//TODO: refactor
- {
- int i;
- for(i = 0; i < STATIONS; i++){
- if(&stations[i] != NULL){
- if(stations[i].id == id)
- return &stations[i];
- }
- }
- return NULL;
- }
- void createEdge(struct station * source, int to, int weight) {
- struct edge *newEdge, *lastEdge;
- struct station *destStation;
- newEdge = (struct edge*)malloc(sizeof(struct edge));
- if(!newEdge) {
- perror("Memory allocation failude while edge creation");
- exit(1);
- }
- destStation = getStationById(to);
- if(!destStation){
- perror("Failed to find station while creation");
- exit(1);
- }
- newEdge->weight = weight;
- newEdge->to = destStation;
- newEdge->next = NULL;
- if(source->edge == NULL){//Ó ñòàíöèè åù¸ íåò ñâÿçåé
- source->edge = newEdge;
- } else {//Ó ñòàíöèè óæå åñòü ñâÿçè
- lastEdge = source->edge;
- while (lastEdge->next){
- lastEdge = lastEdge->next;
- }
- lastEdge->next = newEdge;
- }
- }
- void readEdges()
- {
- struct {
- int from;
- int to;
- int weight;
- } edgeData;
- char filename = "edges.dat";
- //Ñ÷èòûâàíèå äàííûõ è ñîçäàíèå ãðàíåé â ïàìÿòè
- }
- int main(int argc, char * argv[])
- {
- FILE *stream;
- int n;
- stations = (struct station*)calloc(STATIONS,sizeof(struct station));//Âûäåëåíèå ïàìÿòè ïîä ìàññèâ ñòàíöèé
- memset(stations,0,sizeof(struct station)*STATIONS);
- if(!stations){
- perror("Memory allocation failure.");
- exit(1);
- }
- stream = fopen("stations.dat", "wb");
- if(stream == NULL){
- perror("Failed to open stations file\n");
- exit(1);
- }
- stations[1].id = 10;
- stations[2].id = 11;
- createEdge(&stations[1],11,5);
- n = fwrite(stations,sizeof(struct station), STATIONS, stream);
- printf("Hello, world %d\n",n);
- fclose(stream);
- stations[1].id = 0;
- stream = fopen("stations.dat","rb");
- if(stream == NULL){
- perror("Failed to open stations file\n");
- exit(1);
- }
- n = fread(stations,sizeof(struct station),10,stream);
- printf("Hello, world %d; value is =%d \n",n,stations[1].id);
- getchar();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement