820th number in a list messes up

I can't display my whole file, its too complicated/too big to show. I know that may impede things but I think this may be a fundamental issue with these codes. I understand if no one has any ideas/answers with the information I'm providing. I am using this basic list code:

1
2
3
4
5
6
7
8
9
10
for (vector<int>::iterator i = List.begin() ; i < List.end() ; i++)
		for (vector<int>::iterator j = i+1 ; j < List.end(); j++)
			if (*i == *j) 
				List.erase(j--);
	ofstream gfile ("newlist.txt", ios::trunc);
	for(vector<int>::iterator i = List.begin(); i != List.end(); i++)
	{
		gfile << *i << endl;
		howmany++;
	}


as well as:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
std::ifstream afile ("newlist.txt");
	std::istream_iterator<int> abeg(afile), aend;
	std::set<int> aset(abeg, aend);
	
	std::ifstream gfile ("allfinished.txt");
	std::istream_iterator<int> gbeg(gfile), gend;
	std::set<int> gset(gbeg, gend);
	
	std::ofstream cfile("pivotfile.txt", ios::trunc);
	set_symmetric_difference(aset.begin(), aset.end(),
                            gset.begin(), gset.end(),
                            std::ostream_iterator<int>(cfile, "\n"));

	ofstream ggfile ("allfinished.txt", ios::trunc);
	ggfile << endl;
	ggfile.close();
	
	ifstream f1("pivotfile.txt", fstream::binary);
	ofstream f2("newlist.txt", fstream::trunc|fstream::binary);
	f2 << f1.rdbuf();
	f1.close();
	f2.close();

	ofstream f3("pivotfile.txt", ios::trunc);
	f3 << endl;
	f3.close();


The program loops back over and over again. It runs the first code a few times, and I think the 2nd set of code once before it screws up.

The screw up is that during the List part (which I have confirmed with couts to see where it errors out) when its supposed to be doing "file << *i << endl;" the program errors out and stops responding. This is a console program.

This code works with only 9 digit numbers between 100,000,000 and 999,999,999.
When the program errors out I read the "newlist.txt" and every time (about 5+ times now), the 820th line/number begins with the numbers "82" (as do the few previous lines/numbers, and legitimately so) but then is followed by one of the legitimate 9 digit numbers, but, in that happenstance, the 820th number is no longer a 9 digit number, but rather an 11 digit number. Again, preceded with an "82". This correlation leads me to believe its some glitch in the c++ commands. I even started using the List.clear(); command before each list command to clear it out, but it hasn't solved anything. The newlist.txt typically contains a few thousand numbers. But only number/line 820 has the issue.

I know this may be a shot in the dark, but I'm just hoping for any information which could assist me.
Last edited on
Post some of the file around where you think the error is occurring.

Also your first code block, is this how the file is generated?

Verify this doesn't screw things up..
1
2
if (*i == *j) 
   List.erase(j--);


Erasing from a vector invalidates all iterator at that position and any element after that position, since a vector's memory is contiguous. If removal occurs often and not at the end this is probably not the container you want to use.

Also use
1
2
; i < List.end() ; i++) //not this
; i != List.end() ; i++) //use this syntax 
Last edited on
in the for use i != List.end() instead of i < List.end(). The same for the second for.
Last edited on
Altered the codes to "i != List.end()". But I still got the error at the same point, but no 11 digit number in my file (when its supposed to be a 9 digit number). So that problem was addressed. But still getting the error didn't make sense. Since I changed it I've also gotten another error in a different part of the program where the result was the same; windows just told me the program had stopped responding. Is there a code I can put into my program so that it actually spits out an error that I can use to discern what is going on?
It shoudn't matter in this case as a vector iterator is just a pointer, but strictly speaking the loop should be:
1
2
3
4
5
6
7
8
9
10
11
for (vector<int>::iterator i = List.begin(); i != List.end(); ++i)
{
	vector<int>::iterator j = i;
	for (advance(j, 1); j != List.end(); )
	{
		if (*i == *j)
			j = List.erase(j);
		else
			++j;
	}
}

But as I said, that won't change the result you're seeing.

The traditional way to remove duplicates is to use sort() then unique()


I don't know what this code does.
1
2
3
4
5
6
7
8
9
	ifstream f1("pivotfile.txt", fstream::binary);
	ofstream f2("newlist.txt", fstream::trunc|fstream::binary);
	f2 << f1.rdbuf();
	f1.close();
	f2.close();

	ofstream f3("pivotfile.txt", ios::trunc);
	f3 << endl;
	f3.close();

As far as I can tell, you just want to rename pivorfile.txt to newlist.txt. In which case, just use rename()
Last edited on
That last piece of code copies the files. The reason I do it like that is because newlist.txt already exists. But my problem at this point is just the program erroring out as windows does not inform me of any particular error and just tells me that the program has stopped responding.
There is unlink(), that deletes a file.
Have you thrown break points in there, ran until each break point to at least narrow down where the error is occurring?
Yes, I did break points for the first error but now experiencing another unexplained error made me wonder if there was a command I could use to derive the error rather than using break points. This program worked fine for the first few days I was using it. I have not done it yet with the newest crash.
Sans the error question, this matter appears to be resolved. The list thing appears to be the solution. Thank you.
Topic archived. No new replies allowed.