Ok, I'm a bit confused on this whole thing, I guess I just need another brain on this.

Each line in my CSV file reads something like this:

LAV32,Aluminum RTV Silicone 30/bx,T614A,Lavanture,12

Now, LAV32 would be the id tag that would be read by a barcode scanner. What I need to do is have the program search the CSV for LAV32 and display the related information.

I figure this could be done by having the program read each line and break the read line up and assign them to a struct, such as:

struct itemData
{
       char barcodeID [256];
       char itemDescription [256];
       char partNumber [256];
       char vendor [256];
       int qty;
} stockItem;

I guess what I need to know first, is how to read each line in sequence.

Recommended Answers

All 5 Replies

I guess what I need to know first, is how to read each line in sequence.

std::ifstream file("file.txt");
   std::string line;
   while ( getline(file,line) )
   {
      // parse line
   }

Thanks. One more thing, how do I tell it to move from one line to the next?

say the barcode scanned in was LAV32. My thought is that after the line was broken up into the struct...

if (strcmp(stockItem.barcodeID, scannedBarcode) == 0)
{
  //move on to next tasks
}
else
{
//read the next line in the file and compare it to the barcode scanned
}

A solution is to instead of separating each word with comas do it with spaces. Then read each line, and store the information in each variable. The compare the barcode[21] with lav32. If it matches display the rest of the information (thats what you wanna do right?).

Might not be the best solution.

Maybe later I can give you an example.

Thanks. One more thing, how do I tell it to move from one line to the next?

Maybe observe the output first?

#include <iostream>
#include <fstream>

int main()
{
   std::ifstream file("file.txt");
   std::string line;
   while ( getline(file,line) )
   {
      std::cout << line << "\n";
      // parse line
   }
}
commented: Wow... I would've never thought of that :) +28

Ohh.. Ok now I feel stupid. Thanks for the help!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.