I am reading in from a text file and storing the data into different string variables which are in my struct. Currently I use an asterisk to separate the info in the text file, and have a garbage string variable that I pass the asterisk into in my struct. I would like to be able to get rid of that and just omit any asterisk. Can you guys help me figure out how this would be done? Thanks

void store::load()
{//////////////////////////////////load movies into template
	LOAD.open("movies.txt", /*ios::app |*/ ios::beg);
	if(!LOAD.is_open())
	{
		cout << "Could not open movies.txt";

		cin.ignore();
		cin.get();

		exit(1);
	}

	movieList.clear(); //emptying my stl list object.

	while(!LOAD.eof())
	{
		movie *mov = new movie;

		getline(LOAD, mov->title);
		getline(LOAD, mov->star);
		getline(LOAD, mov->yr);
		
		getline(LOAD, mov->garbage); //collect my aterisks

		movieList.push_back(*mov);
	}

	LOAD.close();
	LOAD.clear();

///////////////////////////////////load customers into template
	LOAD.open("customers.txt", ios::app | ios::beg);
	if(!LOAD.is_open())
	{
		cout << "Could not open customers.txt";
		
		cin.ignore();
		cin.get();

		exit(1);
	}

	customerList.clear();

	while(!LOAD.eof())
	{
		customer *cust = new customer;
		
		getline(LOAD, cust->name);
		getline(LOAD, cust->age);
		getline(LOAD, cust->gender);

		getline(LOAD, cust->garbage);

		customerList.push_back(*cust);
	}

	LOAD.close();
	LOAD.clear();
	
}

Recommended Answers

All 6 Replies

How is the file formated? with getline you can set the delimiter a '*'.

name 1
age 1
gender 1
*
name 2
age 2
gender 2
*
name 3
age 3
gender 3
*
name 4
age 4
gender 4
*
name 5
age 5
gender 5


That is how my .txt file is formatted.

Well you don't need to have a garbage in your struct then. you can just create a temporary string in your function and keep putting the astrix in there.

//...
movieList.clear(); //emptying my stl list object.
string temp;
while(!LOAD.eof())
{
	movie *mov = new movie;

	getline(LOAD, mov->title);
	getline(LOAD, mov->star);
	getline(LOAD, mov->yr);
		
	getline(LOAD, temp); //collect my aterisks

	movieList.push_back(*mov);
}

well what you suggest does the same exact thing as what I am doing now except with a temp variable outside of my struct. Im trying to find a way, maybe like and if(getline etc.. etc..'*')
continue;

know what i mean? I want to be able to do it without a garbage variable. Thanks

Since the * is on its own line it makes it more difficult. You could have 2 LOAD.get(); calls after getline(LOAD, mov->yr); but you should comment why you are doing that.

while(!LOAD.eof())
{
	movie *mov = new movie;

	getline(LOAD, mov->title);
	getline(LOAD, mov->star);
	getline(LOAD, mov->yr);
		
	LOAD.get();  // this will get rid of the *
        LOAD.get();  // this gets rid of the newline

	movieList.push_back(*mov);
}

The reason why, is because I also have a delete function that opens the text and deletes up to and including the asterisk. So im basically going for, If line has text other then '*' or only(blank line) '\n', read it into my variable.

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.