There is a segmentation fault when sorting. Where did I do wrong? The C code is written below #include #include struct Wstation { int id; double latitude; double longitude; int capacity; char url[100]; }; struct Wstation *stations; int read_ws(const char *fname) { FILE *fp = fopen(fname, "r"); if (!fp) { printf("not found %s\n", fname); return 0; } int num; fscanf(fp, "%d", &num); stations = malloc(num * sizeof(struct Wstation)); for (int i = 0; i < num; i++) { fscanf(fp, "%d, %lf, %lf, %d, %s", &stations[i].id, &stations[i].latitude, &stations[i].longitude, &stations[i].capacity, stations[i].url); } free(stations); fclose(fp); return num; } int cmp(const void *p1, const void *p2) { struct Wstation s1 = *(struct Wstation *)p1; struct Wstation s2 = *(struct Wstation *)p2; return s1.capacity - s2.capacity; } int main(void) { int N; N = read_ws("./waters-1.txt"); qsort(stations, N, sizeof(struct Wstation), cmp); printf("The stop with the largest capacity is \n"); printf("%d, %lf, %lf, %d, %s\n", stations[N-1].id, stations[N-1].latitude, stations[N-1].longitude, stations[N-1].capacity, stations[N-1].url); return 0; }

Database System Concepts
7th Edition
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Chapter1: Introduction
Section: Chapter Questions
Problem 1PE
icon
Related questions
Topic Video
Question

There is a segmentation fault when sorting. Where did I do wrong?

The C code is written below

#include <stdio.h>
#include <stdlib.h>

struct Wstation {
int id;
double latitude;
double longitude;
int capacity;
char url[100];
};

struct Wstation *stations;

int read_ws(const char *fname) {
FILE *fp = fopen(fname, "r");
if (!fp) {
printf("not found %s\n", fname);
return 0;
}
int num;
fscanf(fp, "%d", &num);

stations = malloc(num * sizeof(struct Wstation));

for (int i = 0; i < num; i++) {
fscanf(fp, "%d, %lf, %lf, %d, %s", &stations[i].id, &stations[i].latitude, &stations[i].longitude,
&stations[i].capacity, stations[i].url);
}
free(stations);
fclose(fp);

return num;
}

int cmp(const void *p1, const void *p2) {
struct Wstation s1 = *(struct Wstation *)p1;
struct Wstation s2 = *(struct Wstation *)p2;

return s1.capacity - s2.capacity;
}

int main(void) {

int N;
N = read_ws("./waters-1.txt");
qsort(stations, N, sizeof(struct Wstation), cmp);

printf("The stop with the largest capacity is \n");
printf("%d, %lf, %lf, %d, %s\n", stations[N-1].id, stations[N-1].latitude, stations[N-1].longitude,
stations[N-1].capacity, stations[N-1].url);

return 0;
}

Expert Solution
steps

Step by step

Solved in 2 steps with 1 images

Blurred answer
Knowledge Booster
Instruction Format
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
Recommended textbooks for you
Database System Concepts
Database System Concepts
Computer Science
ISBN:
9780078022159
Author:
Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:
McGraw-Hill Education
Starting Out with Python (4th Edition)
Starting Out with Python (4th Edition)
Computer Science
ISBN:
9780134444321
Author:
Tony Gaddis
Publisher:
PEARSON
Digital Fundamentals (11th Edition)
Digital Fundamentals (11th Edition)
Computer Science
ISBN:
9780132737968
Author:
Thomas L. Floyd
Publisher:
PEARSON
C How to Program (8th Edition)
C How to Program (8th Edition)
Computer Science
ISBN:
9780133976892
Author:
Paul J. Deitel, Harvey Deitel
Publisher:
PEARSON
Database Systems: Design, Implementation, & Manag…
Database Systems: Design, Implementation, & Manag…
Computer Science
ISBN:
9781337627900
Author:
Carlos Coronel, Steven Morris
Publisher:
Cengage Learning
Programmable Logic Controllers
Programmable Logic Controllers
Computer Science
ISBN:
9780073373843
Author:
Frank D. Petruzella
Publisher:
McGraw-Hill Education