0

I am trying to read a CSV file with such format.

902323;NewYork, newyork, US
43232;Mumbai, India

So I am trying to read the integer value and then everything else into a char till I hit the new line.

My code is as such:

datatype_t* read(FILE* fp, FILE* wfp)
{

 int weight;
 char *key;
 if (fscanf(fp, "%[^;],%[^\n]",weight,key) == 2) {
    printf("here\n");
    datatype_t *d = (datatype_t*)malloc(sizeof(datatype_t));
    if (d == NULL)
    {
        printf("No Space Allocated\n");
    }
    d->weight = strdup(weight);
    d->key= strdup(key);
    printf("%d", d->weight);

    return d;
}
else{
    printf("nothing\n");
}
return NULL;
}

So I have two files coming in from the main function, the first one is the file I read and the second one is the one I write to. I am getting seg faults on the fscanf line.

typedef struct{
     int  weight;
     char *key;
}datatype_t;
alk
  • 69,737
  • 10
  • 105
  • 255
Parnak Dave
  • 1
  • 1
  • 3
  • 4
    `read` is a poor name for your function. It conflicts with the POSIX [read](http://pubs.opengroup.org/onlinepubs/009695399/functions/read.html) – Basile Starynkevitch Aug 26 '17 at 09:04
  • 1
    code is incomplete, but i'm almost suer doesnt compile ( strdup(integer) ) – Jacek Cz Aug 26 '17 at 09:06
  • You need to read carefully the documentation of [scanf](http://en.cppreference.com/w/c/io/fscanf). You should compile with all warnings & debug info (`gcc -Wall -Wextra -g` with [GCC](http://gcc.gnu.org/)...) then **use the debugger** `gdb` – Basile Starynkevitch Aug 26 '17 at 09:06
  • You need to check pointer value of `fp`, it might not be valid. And [Please create a minimal, complete & verifiable example](https://stackoverflow.com/help/mcve) – Karim Manaouil Aug 26 '17 at 09:06
  • @BasileStarynkevitch : Unable to use GDB on my mac for some reason! :( . – Parnak Dave Aug 26 '17 at 09:07
  • "*everything else into a char*" no `char`s around. `key` is a ***pointer***. `char *key;` does not make `key` point anywhere. – alk Aug 26 '17 at 09:07
  • Then install `gdb` on your computer. BTW I recommend even installing an entire Linux distribution – Basile Starynkevitch Aug 26 '17 at 09:07
  • Then read [documentation](https://sourceware.org/gdb/download/onlinedocs/gdb/index.html) of `gdb` and investigate why it does not work (it is working for most people). – Basile Starynkevitch Aug 26 '17 at 09:09
  • 1
    You function have memory leaks !! Memory obtained from `malloc` is never returned. You need to use a tool like `Valgrind` alogside with `gdb` to have a good debugging environment. – Karim Manaouil Aug 26 '17 at 09:12
  • Also just take the compiler's warning serious. – alk Aug 26 '17 at 09:13
  • @alk : warning: format specifies type 'char *' but the argument has type 'int' [-Wformat] if (fscanf(fp, "%[^;],%[^\n]",weight,key) == 2) { This is the warning and this is the exact line where I get seg faults. I dont understand why ? – Parnak Dave Aug 26 '17 at 09:14
  • 1
    The warning tells you that the code makes `fscanf()` expect a `char*` were an `int` is passed. You might want to train yourself on how to craft format-strings for the use with `fscanf()`. This is done best by reading `fscanf()`'s documentation and lots of examples. Doing it by Trial&Error is know to cause depressions. – alk Aug 26 '17 at 09:17
  • @alk : I have read the documentation and I have checked the CSV file thoroughly and it passes an integer until it hits ; and then its all strings. So idk why is it throwing such a warning? – Parnak Dave Aug 26 '17 at 09:21
  • Again: The warning tells you that the code makes `fscanf()` expect a `char*` were an `int` is passed. Either make `fscanf()` to expect an `int` or pass it a `char*`? – alk Aug 26 '17 at 09:21
  • but I have declared the variable weight as an int so why is it expecting a char? :\ – Parnak Dave Aug 26 '17 at 09:30
  • Didn't you mention you read the documentation? From the [docs](http://man7.org/linux/man-pages/man3/scanf.3.html): "*The scanf() family of functions scans input according to format [...].*" – alk Aug 26 '17 at 09:32
  • @alk : yeah so thats exactly what I am saying that it should expect what it is declared as which is an integer as you can see from my code. – Parnak Dave Aug 26 '17 at 09:34
  • You might like to re-read the docs on how to use `[` inside a format-string and what it makes `fscanf()` expect. Start here: "*`[` Matches a nonempty sequence of characters from the specified set of accepted characters; the next pointer must be [...]*" – alk Aug 26 '17 at 09:42
  • like [this](https://wandbox.org/permlink/k8VpkG6kYcORjEh6) – BLUEPIXY Aug 26 '17 at 12:20
  • @BLUEPIXY : wow got it perfect :)). Is there a way I can ignore all the commas? When I try to print out the file in TERMINAL it prints out the commas with! – Parnak Dave Aug 27 '17 at 10:01
  • How about converting commas to spaces or removing them? – BLUEPIXY Aug 27 '17 at 12:06
  • @BLUEPIXY: Thanks for the code :)) I figured it out :)) – Parnak Dave Aug 29 '17 at 07:29

0 Answers0