C++ : Reading file and store data in multi-vector

I am trying to read data file with this format:

T1: I1,I2,I5
T2: I2,I4
T3: I2,I3
T4: I1,I2,I4
T5: I1,I3
T6: I2,I3
T7: I1,I3
T8: I1,I2,I3,I5
T9: I1,I2,I3

I don't want to read the first column T1,T2,T3 ...... , but each line will be a dataset I want to start read after (space '  ' ) and end with each line and how can I separate the data according to (comma ',')

I wrote this code but it didn't work correctly and it reads the first column anyway

string CItem;

// set of elements
set< CItem > CItemSet;

//Transactions
 CItemSet CTransaction;

// set of transactions
vector< CTransaction > CTransactionSet;

ifstream inFile(inFileName);
	if (!inFile)
	{
		cout << "Failed to open input file filename:." << inFileName;
	}

CTransactionSet transSet;
	CTransaction tran;
	string txtLine;
	// read every line from the stream
	while (getline(inFile, txtLine))
	{
		
		istringstream txtStream(txtLine);
		string txtElement;
		// read every element from the line that is seperated by commas
		// and put it into the vector or strings
		
		while (getline(txtStream, txtElement, ','))
		{
			if (txtElement == ": ") break;
			else tran.insert(txtElement);
		}
		transSet.push_back(tran);
	}

Open in new window

AaeshahAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Karrtik IyerSoftware ArchitectCommented:
I suggest you simply do a get line first to read entire lines as in the file, then perform string operations to get the required data.
1) read  entire line, so you will get 4-5 strings (number of rows)
2) then perform substring to remove T1,T2 part..
3) the perform string split with comma as the separator to get the individual values.
0
Karrtik IyerSoftware ArchitectCommented:
Your below line of code :
while (getline(txtStream, txtElement, ','))
Shall return T1: I1
So your if condition shall never succeed to break (one more thing ideally instead of break you should do continue since you want other data in the row), the string shall never match to be only ":"
Hence you shall always get T1 as well in your vector which you wanted to skip...
0
AaeshahAuthor Commented:
Could you please show me an example how to perform substring to remove T1,T2 part..
and how to split them in a code, Please!
0
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

Karrtik IyerSoftware ArchitectCommented:
Try this sample, please let me know if something is not clear
  std::ifstream ifs ("test.txt", std::ifstream::in);//Read the file in input mode
  string eachline;
  while(getline(ifs,eachline)){//Read each line into a string
    if(!ifs.good())//if it (file state) is no more valid break and close the file
        break;
    stringstream txtStream(eachline);//Insert the string to string stream for further processing
    txtStream.ignore(eachline.length(),':');// Ignore the T1: part since we are not interested in it
    std::string token;//to contain each element that we are interested in
    while(std::getline(txtStream, token, ',')) {//Get elements seperated by comma into the token variable.
        
        std::cout << token << ',';// This token might still contain white spaces
    //You can read this token and insert into your vector
    }
    std::cout << endl;//Insert new line
  }
  ifs.close();//Close the file stream handle

Open in new window

Test.txt
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Karrtik IyerSoftware ArchitectCommented:
Also as a better optimization I suggested to keep the file handle open for as less time as possible in case if it is a shared resource. If it involves pretty straightforward and small amount of string processing it might still not create any problems. But if you plan to do more string processing and formatting after reading each line and if file can contain lot of records then  it would be ideal that you read the entire file into a string. And the perform all the string processing and formatting in memory without keeping the file handle open for the entire time when string processing is done. It depends on how the file is updated by other clients...
Hope it is clear.
Thanks,
Karrtik
0
AaeshahAuthor Commented:
Thank you very much that solve my problem using .ignore is all I need. Thanks again
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C++

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.