C ++ file input looping thru sets of data

Posted on 2003-03-27
Medium Priority
Last Modified: 2010-04-17
This is my first posting. I need to process the contents of a file that has about 4000
records where every 12 to 36 lines represent a discrete set of data that needs
to be fed into an object.

I have been able to open the file, fill an object but moving to the next
block of 12 to 36 lines has me scratching my head. How do I set up a loop
to do this.

If the “dealer” or “part number” changes then the block of data is done and the next object should be filled.
 Implementation ( .cpp) , the data file itself appear below and the class (.h).

#pragma warning(disable:4786)

#include <iostream>
#include <string>
#include <fstream>
#include <vector>
#include <sstream>
#include <stdlib.h>
#include "partNo.h"

using namespace std;

int main()

      string s1, s3, s4, s5, s6;  // s1 dealer code, s3 partnumber
      char delim[]=",";  // comma delimter to look for
      int m = 0;  // index for moving thru vector
      ifstream f("test.txt");  // open file
      vector<string> vs;            // vector to hold each string
      vector<string>::iterator strit ;
                  string s;
                  vs.push_back(s);  // put the entire string in the vector

      while(strit != vs.end()) // loop thru the vector parsing each line
            PartNumber p;                  // part number class

            string s2 = vs[m];  // using vector index assign string to string variable
      int j = 0, k =0;  // placement variables
      int i=0;   // for moving thru string
      int dcnt = 0; // count delimters to parse out dealer info
            i = s2.find_first_of(delim, j);  // find first delimiter , set i to end of string
            dcnt = dcnt + 1;  // increment delimiter count

            // each delimiter parses out a field based on dcnt
                  case 1 :
                        s3.assign(s2,j,i-j); // assign s2 with k start and i end of string , grab dealer
                  //      cout << s3 << " " ;
                        p.setDealer(s3);   // update dealer in part object
                         break;   // causes exit from loop

                  case 2 :
                        s4.assign(s2,j,i-j);  // grab partnumber
                        p.setPartNumber(s4);  // update partnumber in part object
                        //  cout << s4 << " "  ;

                  case 3 :
                        // s5.assign(s2,j,i-j); // date  unused field
                        //      p.set
                        // cout << s5 << " " ;

                  case 4 :
                        s6.assign(s2,j,10);   // units
                        //cout << s6 << endl;
                        // convert the string to double
                         p.setDmd(atof(s6.c_str()));  // update dmd vector in part object
                  j = i+1;  // move the start for delim search forward

            //       }while(i < s2.length());    // VERIFY THIS
             }while( i < s2.find_last_of(delim, 0));  // and i >= 0 ???

            p.getDmd();  // prints the units of demand contents of vector      

            m++;   // increment counter for vector
      // code needs to be added to put each object into a container for future work

       return 0;

/* test.txt note should stop at change
in dealer or partnumber.


/// class /////
#include <iostream>

#include <string>

using namespace std;

class PartNumber

      public :
            friend ostream& operator<< (ostream& ostr, const PartNumber& obj);

            setDealer( string&  s) ;  // address constants here and below
            setPartNumber(string& s);
            string getDealer() ;
            void getPartNumber();
            setDmd(const double& units);
            void getDmd();

      private :
            string strDealer;
            string strPartNumber;
            double unitDemand;
            vector<double> vdmd;  // hold units of demand
            vector<double>::iterator p ;


void PartNumber::getDmd()
            p = vdmd.begin();
            while(p != vdmd.end())
                  cout << *p << "    " << "units stored in obj's vector" << endl ;

// set the unitDemand variable
PartNumber::setDmd(const double& units)

// set dealer code
PartNumber::setDealer(string& s)
      strDealer = s;

// set dealer code
PartNumber::setPartNumber(string& s)
      strDealer = s;

// get the dealer code
string PartNumber::getDealer()
      return strDealer;

// output the object
ostream& operator<< (ostream& ostr, const PartNumber& obj)   // output
      ostr << obj;
      return ostr;
Question by:marcon33
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
LVL 84

Expert Comment

ID: 8222632

strit = vs.begin();
while(++strit != vs.end()) // loop thru the vector parsing each line

Author Comment

ID: 8225485
thanks for the thought. I can process each string held by the vector and even move thru each element of the vector. It is how to stop and start over with a new object that is the problem.
My latest thought is maybe a queue (?) could be used at the beginning , popping the strings that all have the same dealer code and part , then stopping and starting over when they change.

Accepted Solution

reid_henderson earned 1000 total points
ID: 8226549
If I understand the problem correctly, you want to create a new object if either of the first two numbers change(dealer or part number)

I would create the following variables outside of the main while loop:

vector<PartNumber*> vp; //vector of part numbers
PartNumber* pn = new PartNumber();
vp.push_back(vp); // initialize the first element of vp
int pObjIndex = 0; //keep track of position in vp (unnecessary, but simple)
in your case statements, only do the string assign, i.e.
 case 1 :

then after the case statement, test for a change in part or dealer:

   if(s3 != (vp[pObjIndex].getDealer()) || s4 != (vp[pObjIndex].getPartNumber())) {
      //create new partnumber object
      PartNumber* pn = new PartNumber();
      //push it onto the vector
      //increment index - this is not a necessary step

//now put your information into the top object in vp

//I might also suggest more useful names for your strings
//such as currDealerStr - s# is a pain to maintain
//Instead of using pObjIndex you could pop_back the last
//element from vObjIndex or use an iterator for vp.end()-//1;

Ok - I hope I answered the right question... :)

Author Comment

ID: 8226925
That gives me plenty to go on. Great clarity. Thanks!

Featured Post

Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

If you’re thinking to yourself “That description sounds a lot like two people doing the work that one could accomplish,” you’re not alone.
Computer science students often experience many of the same frustrations when going through their engineering courses. This article presents seven tips I found useful when completing a bachelors and masters degree in computing which I believe may he…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
Suggested Courses

752 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question