Hello everyone

Could any one please help me parsing a line having different fields separated through commas. I want to store each comma separated sub string in a struct member variable. The lines are being read from a file.

105:1:CME,20100601,07:34:22.796,GRC,GE,201009,FUT,XGGRC,0G4LHZ013,14ijpol1vsu7l7,Fill,0000D9DB,B,00000,99.155,2,99.155,20100601,07:27:34

for example the line ID which is "105:1:CME" is one sub string to be stored in a struct member variable called lineID. "20100601" to be stored in another struct variable called startTime and so on....

and also is there any way I can merge the date/time sub strings and store them in one struct member variable like this "20100601-07:34:22.796" that is removing the comma and putting a dash (-) between them instead of treating them as two different sub strings?

Thanks

Recommended Answers

All 2 Replies

Here's a little program I wrote for you using stringstream. This will split your long string into 20 parts, into an array (type string), named "data".

//includes...need sstream (stringstream)
#include <string>
#include <sstream>
#include <iostream>
//don't forget your namespaces
using namespace std;

/main
int main()
{
    //your string
    string str = "105:1:CME,20100601,07:34:22.796,GRC,GE,201009,FUT,XGGRC,0G4LHZ013,14ijpol1vsu7l7,Fill,0000D9DB,B,00000,99.155,2,99.155,20100601,07:27:34";
    //declare array for output from split
    string data[20];
    
    
    //create the stream using your string above
    stringstream stream(str);
    //loop repetition value
    int i = 0;
    //loop to split string
    while(getline(stream, data[i], ',') )
    {
        //increase repetition number
        i++;
    }
    
    
    //loop repition value
    int j = 0;
    //print output
    while (j<20){cout<<data[j]<<endl;j++;}
    //pause so we can read
    system("Pause");
}

For writing into a struct, take a look at this page:
http://www.cplusplus.com/doc/tutorial/structures/

Ask me if you need further help :)

Does your file save each of the line in the same manner for the whole file? What I mean is that each line will contain exactly the same number of commas? If so, you can use find(",", startIndex) to find each string token you want for each line. You already know the meaning of each token already, so you can do whatever you want with it.

// i.e. code portion
// read in each string line as str

int found, start=0, tokenNum=0;
string token;
while (start<str.length()) {
  found = str.find(",", start);
  if (found>=0) {  // found
    // each substring here is the token
    token = str.substring(start, found-1);  // not include the ","
    start = found+1;  // start again but not include ","
    tokenNum++;
    switch (tokenNum) {
      // add it to an array or whatever data structure you want here
    }
  }
  else { break; }  // not found anymore, break out of loop
}
// may need to get the last string portion
token = str.substring(start, str.length());
// add it to an array or whatever data structure you want here
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.