0

I have a requirement where i need to search product barcode from a csv file shown below.

Sr. No.,Product Barcode,Product Description,Discount (%),Price Per Unit
1,011001,TOILET PAPER,5,40
2,011002,BATHING SOAP,0,27
3,011003,MOISTURIZER,3,95
4,011004,SHAMPOO,0,115
5,011005,CONDITIONER,5,155
6,011006,TISSUE,10,60
7,011007,HAIR OIL,0,75
8,011008,HAIR GEL,0,96
9,011009,BODY LOTION,7,195
10,011010,FACE WASH,0,85

When the user enters the barcode, it should search the csv file and should pick up the Price per unit as well as discount and generate the bill. This has to be implemented in C++. In the later stage of development I also need to update the csv file content. For instance if it has a column 'Quantity' which shows the stock presence of a particular product, and as that product is sold I need to update the csv field quantity as well.

Please guide me how to search the csv file and pick up the fields. I searched net but could not find satisfactory answer.

I am using Bloodshed Dev C++.

Gaurav K
  • 2,864
  • 9
  • 39
  • 68
  • There are many examples of how to tokenize a string (each line of the file) here: http://stackoverflow.com/questions/53849/how-do-i-tokenize-a-string-in-c You then just need to convert the relevant token into an integer if necessary. – goji Dec 07 '12 at 04:19
  • Loading and searching, that's easy. Updating en'masse, just as easy. Updating in-place. considerably more work. Consider an unordered_map for the first two. The third, avoid it if-at-all possible. – WhozCraig Dec 07 '12 at 04:26
  • It is basic C++ file handling question where you do a getline() and then break the input string on ','. Give it a try and then update your question if you face any problems. – TheZelus Dec 07 '12 at 04:40
  • Please update your IDE to the following version, which fixes an immense list of bugs, ships with GCC 4.6.1 or 4.7.0, and is fully portable: sourceforge.net/projects/orwelldevcpp – Orwell Dec 07 '12 at 08:01
  • Duplicate of the following question: http://stackoverflow.com/questions/1120140/csv-parser-in-c – Zamfir Kerlukson Feb 23 '13 at 09:56

2 Answers2

0

I am able to extract the product price for entered barcode and generate the bill, using getline().

Cheers Guys for ur resposes.

Gaurav K
  • 2,864
  • 9
  • 39
  • 68
0

I would advise using libpcre

You could do something like this --

#include <iostream>
#include <vector>
#include <string>
#include <pcre++.h>

using namespace std;
using namespace pcrepp;

int main(int argc, char **argv) {
    string pat = ",";
    vector<string> v;
    string s;
    Pcre p(pat);
    int ctr;
    while( getline(cin, s) ) {
        v = p.split(s);
        vector<string>::iterator i;
        for(i=v.begin(),ctr=0;i!=v.end();i++,ctr++) {
            if(ctr==2) {
                 cout << *i << endl;
            }
        }
    }
    return 0;
}

I agree this might be overkill for extracting the second column or the nth column, but I like this method because pcre gives you a lot of freedom in terms of what text you wish to extract from the CSV file.

ssb
  • 7,422
  • 10
  • 36
  • 61