R6010 -abort() has been called

I am trying to make a simple program to try out some exporting to extrenal .txt files, but I'm encountering the R6010 error... I'm not very far into my studies of c++ but I've read through the tutorials on this site and understood most of it.
the errors started popping up when I started filling the letterpointer array
1
2
3
4
5
6
	for (int n = 0; n < lengte; n++)
	{
		letterpointer[n] = ttext.substr(n, 1);
		(stringstream)letterpointer[n] >> letter;
		point[n] = letter;
	}
(I forgot that earlier and had less problems then)
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
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
#include <fstream>
#include <string>
#include <iostream>
#include <ios>
#include <sstream>
using namespace std;

int main()
{
	ofstream streampje;

	bool failb;
	string response;
label1:
	streampje.open ("save_file.txt", fstream::in | fstream::out);
	failb = streampje.fail();
	if (failb)
	{
		cout << "errors encounted \n try again? (y/n)";
		cin >> response;
		if (response == "y")
			goto label1;
		else
			return 0;
	}
	cout << "no errors encounted!\n what texts should be exported to the file? \n";
	string ttext;
	getline(cin, ttext);
	long lengte = sizeof(ttext);
	char* point;
	point = new (nothrow) char[lengte];
	char letter;
	string* letterpointer;
	letterpointer = new (nothrow) string[lengte];
	for (int n = 0; n < lengte; n++)
	{
		letterpointer[n] = ttext.substr(n, 1);
		(stringstream)letterpointer[n] >> letter;
		point[n] = letter;
	}
	streampje.write(point, lengte);
	char* input;
	input = new char[lengte];
	fstream straempje;
	straempje.read(input,lengte);
	string finals;
	for (int b = 0; b < lengte; b++)
	{
		finals = finals + input[b];
	}
	cout << "what save_file.txt now contains is:\n" << finals;
	streampje.close();
	straempje.close();
	return 0;
}

any help on how I could make this work and advise to avoid the error next time?
line 29: sizeof(ttext); is not the size of the text. User either ttext.size() or ttext.length()
that helped slightly, but now I can't write spaces to the file and it gives a load of double lines in the output screen when I try to read it and when I go to look into the file after i've completed the program it contains the orriginal text and a load of ÌÌÌÌÌÌÌÌÌÌÌÌÌ(capital i with a little stripe to the left) and it writes over the orriginal text...
any help on that?
(I forgot to add this line and added that now: straempje.open("save_file.txt", fstream::in);)
This:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
	getline(cin, ttext);
	long lengte = sizeof(ttext);
	char* point;
	point = new (nothrow) char[lengte];
	char letter;
	string* letterpointer;
	letterpointer = new (nothrow) string[lengte];
	for (int n = 0; n < lengte; n++)
	{
		letterpointer[n] = ttext.substr(n, 1);
		(stringstream)letterpointer[n] >> letter;
		point[n] = letter;
	}
	streampje.write(point, lengte);
could be entirely replace by this:
1
2
	getline(cin, ttext);
	streampje << ttext;


But that's not your problem. The problem is that streams are buffered. The data is written to the buffer and only if the buffer is full or flush() is called (either explicit of implicit via endl or close()) the data is written to the disk. When you use another object (straempje) for reading the data you won't get what's currently in the buffer, hence you get garbage values.
thank you very much, that did fix the problem(except I had to put back the
long lengte=sizeof(ttext)
because that is needed for the input array.
but now I have the next problem, the return value now only returns the first 28 characters but the output file does contain the entire text.
any help on that?
long lengte=sizeof(ttext) is still wrong. read my first post.

because that is needed for the input array.
unlikely

any help on that?
show your actual code
yes, that completely fixed the problem! the code now is:
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
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
#include <fstream>
#include <string>
#include <iostream>
#include <ios>
#include <sstream>
using namespace std;

int main()
{
	ofstream streampje;

	bool failb;
	string response;
label1:
	streampje.open ("save_file.txt", fstream::in | fstream::out);
	failb = streampje.fail();
	if (failb)
	{
		cout << "errors encounted \n try again? (y/n)";
		cin >> response;
		if (response == "y")
			goto label1;
		else
			return 0;
	}
	cout << "no errors encounted!\n what texts should be exported to the file? \n";
	string ttext;
	getline(cin, ttext);
	long lengte = ttext.size();
	streampje << ttext;
	streampje.close();
	char* input;
	input = new char[lengte];
	fstream straempje;
	straempje.open("save_file.txt", fstream::in);
	straempje.read(input,lengte);
	string finals;
	for (int b = 0; b < lengte; b++)
	{
		finals = finals + input[b];
	}
	cout << "what save_file.txt now contains is:\n" << finals;
	straempje.close();
	return 0;
}

now my next question: is C++ able to create new .txt files?
Last edited on
If you want the file to be created, don't pass the in flag
so I should have something like:
1
2
fstream in
in.open("save.txt")
?
1
2
std::ofstream out("save.txt"); //would create the file if it does not exists
std::fstream out("save.txt", std::ios::out); //an alternative 


I don't see the point in reading from an non-existent or newly created file.
Last edited on
Just a note about confusion.
ofstream streampje;

and then defined as a base type of ofstream as
fstream straempje;

That sort of habit can cause problems if you continue to practice it.
Dyslexic?
Catastrophic.
Last edited on
Yes, I am dyslectic, but I knew the difference here. I changed the name because otherwise it wouldn't really make sense. do you have to reopen a file in a specific mode after it has been created? or can you instantly start writing?
Because it gets rather irritating to have to write:
1
2
3
4
ofstream streampje("textfile.txt");
streampje.close();
streampje.open("textfile.txt",fstream::out);
blablabla

everythime...
http://www.cplusplus.com/reference/fstream/ofstream/ofstream/
http://www.cplusplus.com/reference/fstream/ofstream/open/
out is always set for ofstream objects (even if explicitly not set in argument mode).
okay, thanks! that will save some typing!
Topic archived. No new replies allowed.