My csv file contains the following:
BP BCS DEB EG GY KAP KEC MI
BP 0 216 511 311 22 23 242 550
Bekescsaba 216 0 23 34 11 345 23 42
Debrecen 511 642 0 124 31 124 123 315
Eger 311 1351 31 0 342 532 211 134
Gyor 22 135 341 431 0 134 23 312
Kaposvar 23 14 24 341 14 0 15 32
Kecskemet 242 241 241 135 14 15 0 231
Miskolc 550 24 4 12 352 124 123 0
I want to read the numbers into an array. Question is, how can I do that, while skipping the first string as well?
This is the code that reads everything:
#include <stdio.h>
#include <stdlib.h>
#include <locale.h>
#include <math.h>
#include <time.h>
#define MAX 1024
void main() {
FILE *f;
float r;
int i, j;
char line[1024];
char city[MAX];
int mtx[20][20];
int szam[50];
char *cities[9] = {"Budapest", "Bekescsaba", "Debrecen", "Eger", "Gyor", "Kaposvar", "Kecskemet", "Miskolc"};
f = fopen("/home/dumika/Desktop/asd.csv", "r");
if(!f) printf("File opening problem!");
//fgets(line, 1024, f);
while(fscanf(f, "%s", city) != EOF)
{
printf("%s \n", city);
}
}
Very little info on how to actually manage csv i/o on C. The actual task is to ask a user to input two cities, and calculate the distance. What I would do is read the numbers in a matrix, and have cities in a pointer array. A user writes, for example Budapest (index 0) and Eger (index 3) the distance will be 311. (mtx[0][3]) But I have trouble reading the numbers into a 2D array. What is the best method to do so?