Using fscanf() to read from a csv file in C
<p>I'm having a bit of trouble with this piece of code. I have a file products.csv that I'm trying to read values in from and store in a struct. One of the lines are</p>

<p><code>Book,B123,The Hunt for Red October,12.99,28</code></p>

<p>Because the title has whitespaces in it, I know I need to use <code>[^,]</code> instead of <code>%s</code> so <code>fscanf()</code> doesn't stop at whitespaces, but it's messing up the reading of the file and I don't know exactly how to fix it. Here's the code:</p>

<pre><code>typedef struct {
    char productType[15];
    char productID[4];
    char productDescription[100];
    double productPrice;
    int quantityInStock;
} product_t;    

int main() {
    product_t product1;

    read_products(&amp;product1);
}

int read_products(product_t *product) {
    FILE *inFile = fopen("products.csv", "r");

    fscanf(inFile, "%[^,] %[^,] %[^,] %lf %d", product-&gt;productType, product-&gt;productID, product-&gt;productDescription, product-&gt;productPrice, product-&gt;quantityInStock);

    printf("%s %s %s %lf %d", product-&gt;productType, product-&gt;productID, product-&gt;productDescription, product-&gt;productPrice, product-&gt;quantityInStock);
}
</code></pre>