Filter and process data Download the file population.txt from webcourse.
The file is a formatted text file with 3 columns: country, city and population. Read the file and create a short c program to do the following:
a.display the name and the population of the city with the highest population
b. Display on the screen the sum of all the population of all the cities
#include<stdio.h>
#include<stdlib.h>
main()
FILE *fp;
char country[26];
char cities;
long num;
long sum = 0;
long current;
long highest;
fp = fopen("F:\\Programming with Persistent Data\\population.txt", "r");
//fp_even = fopen("F:\\Programming with Persistent Data\\even_lines.txt", "w");
//fp_odd = fopen("F:\\Programming with Persistent Data\\odd_lines.txt", "w");
if (fp == NULL)
{
printf("Cannot open file\n");
exit(1);
}
while((fscanf(fp, "%s %s %ld",country, &cities, &num))!= EOF)
{
flushall();
printf(" %ld \n", num);
current = num;
printf(" \ncurrent is %ld\n", current);
sum = sum + num;
if(num > current)
{
highest = num;
}
printf(" \n highest is %ld\n", highest);
}
printf("\n The sum of all population of all cities is - %ld", sum);
printf("\n\n The city with highest population - %ld", highest);
fclose(fp);
//fclose(fp_odd);
//fclose(fp_even);
getchar();
}
Program works, but it's display that highest is = 0; I can't find the error. What is wrong with my code?