0

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

IMG

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?

  • 2
    But the file is not comma separated? – Andreas DM Jun 01 '16 at 17:23
  • Read things as string, [split them](http://www.cplusplus.com/faq/sequences/strings/split/) and them use functions to convert numbers to int or to doubles – Leonardo Alves Machado Jun 01 '16 at 17:32
  • Aside: please improve your error-catch: `printf("File opening problem!");` may well not appear on the console before you get a segfault. Add a `\n` at the end of the message, and `return 1` from `main`. – Weather Vane Jun 01 '16 at 17:34
  • Am I being picky when I ask why the distance from Kecskemet to Miskolc is 231, but the distance from Miskolc to Kecskemet is 123? There are many other such anomolies in the data. Also as commented, it is not a CSV file. If it were, it could handle city names with more than one word, but now it cannot. Perhaps it is a TSV (Tab-separated values) file. – Weather Vane Jun 01 '16 at 18:01

1 Answers1

0

(side-note: CSV means comma-separated values; your file is not in CSV format)

If you know the dimensions of your array, and don't need error checking, you can use fscanf.

The following will skip a word (delimited by spaces). Use it to skip the redundant names that go before the numbers, which you actually need.

fscanf(file, "%*s");

The following will read a number:

int distance;
fscanf(file, "%d", &distance);

Combine these in a loop, with proper counting, and you will get your data.


Note: the above is a quick-and-dirty solution. You will have errors in your file or in the code that reads it, and fscanf makes it hard to handle errors. You should learn how to use fgets and split a string in C.

Community
  • 1
  • 1
anatolyg
  • 26,506
  • 9
  • 60
  • 134