0

Hello World!

I'm new in the forum and I don't have too much experience with C++, I'm looking for help with my code, I hope you could help me :)

I have to read from a file.txt different collumns, each of them corresponds to a parameter and store them in a deque, I have found out how to read the first 3, but the problem comes with the last parameter, because is a vector that I have insert in the deque, let me show you:

My file would be something like this:
1 A0003 128 4,4,4
2 A0005 128 1,8,3,2
3 A0002 128 2,2

The 1st, 3rd and 4th collumns are int in dec but the 2nd is in HEX.

I have used something like:

std::istream ifs(parameter("file").stringValue(),std::ifstream::in);
myfile.clear();
ifs >> std::skipws;
while (!ifs.fail())
    {
      Entry e; // a class where I have my parameters
      ifs >> src; if (ifs.fail()) break;
      ifs >> src::setbase(16) >> e.dst; if (ifs.fail()) break;
      ifs >> e.size; if (ifs.fail()) break;
      ifs >> e.path; if (ifs.fail()) break;
   }
ifs.close();

if (!ev.Disabled())
   {
    ev << "Show the parameters" << addr << endl;
      for (std::deque<Entry>::iterator l = myfile.begin(); l != traces.end(); l++)
        {
          ev << "dst = " << std::hex << l ->dst
             << "size = " << l ->size
             << "path = " << l ->path // here it should show the content of the vector i.e. 4,4,4
             << endl;
         }
   }

How can I read from the file the vector and insert it on the deque to be able to take later each one of the values of the vector from the deque later???

Thanks in advance for your help :)

2
Contributors
1
Reply
2
Views
7 Years
Discussion Span
Last Post by sfuo
1

Not sure if you want to add the extra number in so you know how long the vector is. Otherwise you would have to read it in and check for the commas.

#include <iostream>
#include <vector>
#include <deque>
#include <fstream>

using namespace std;

struct Entry
{
	int addr;
	int dst;
	int size;
	vector<int> path;
};

/* "test.txt"

1 A0003 128 3 4 4 4
2 A0005 128 4 1 8 3 2
3 A0002 128 2 2 2

*/

int main()
{
	deque<Entry> myFile;
	ifstream in("test.txt");
	Entry temp;
	while( in >> temp.addr )
	{
		int counter;
		in >> hex >> temp.dst >> dec >> temp.size >> counter;
		temp.path = vector<int>(counter);

		for( int i = 0; i < counter; i++ )
			in >> temp.path[i];

		myFile.push_back(temp);
	}

	in.close();

	//not sure if you wanted this to print to a file or something but its exactly the same just make an ofstream and replace cout
	for( unsigned int i = 0; i < myFile.size(); i++ )
	{
		cout << "Show the parameters " << myFile[i].addr << endl;
		cout << hex << myFile[i].dst << dec << endl;
		cout << myFile[i].size << endl;
		for( unsigned int c = 0; c < myFile[i].path.size(); c++ )
			cout << myFile[i].path[c] << " ";
		cout << endl;
	}

	return 0;
}
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.