setting a string equal to the data inside a document

closed account (4Gb4jE8b)
my code:

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
for(x = 0;x<number;x++,y++)
{
	//Writing the file names
	ostringstream child_file;
	string in_text,final_text;
	int startp,endp,p_endp,last_char,p_last_char;
	startp = 4096 * x;endp = 4096 * y;p_endp = 4096 * (y-1);
	child_file << std_name << " Pt " << y << ".txt";
	child_name = child_file.str();
	fout.open(child_name.c_str());
	if(fexists(child_name.c_str()) == false)
	{
		cout << "\nIn file " << child_name.c_str() << "\n";
		cout << "Error: file unable to be made correctly\n";
		pause();
		return 1;
	}

	//Writing the file content
	fin.open(File_name_opened.c_str());
	in_text = fin._Stdstr;
	//endpoint of previous iteration
	p_last_char = in_text.rfind('.',p_endp);
	if(p_last_char < 0){p_last_char = 0;}

	//endpoint of this iteration
	last_char = in_text.rfind('.',endp);

	//what should go in the file
	final_text = in_text.substr(p_last_char,last_char);
	fout << final_text;
	fin.close();
	fout.close();
}


what i want to happen is similar to the split function, but ending at the last period. What is happening is that i get a space in each file and nothing more. I believe it has to do with the initialization of in_text not being equal to the data inside the document. How can i make it equal to said data?
in_text = fin._Stdstr; //line 21 What is that supposed to do?
1
2
//if(fexists(child_name.c_str()) == false)
if( !fout.is_open() )

Did you need to open and close fin in every iteration? maybe you could avoid the magic number in line 7
closed account (4Gb4jE8b)
line 21 was my pathetic attempt at trying to find a solution to my problem. It didn't work.

fexists() was a function i defined earlier that returns a bool value, which works pretty much as you said, i added the line so it's a little easier to read (imo).

no i probably don't. i wasn't sure how it would effect in_text, and considering there were problems not opening and closing fout, i did it to be safe temporarily (as it is a waste of energy and time to keep it there if i don't need it to be there, as you said).

though i'm unsure of what you mean by "magic number" i could technically avoid p_endp because using rfind('.',startp) would technically be the same. But again this is for readability on my part.

Though i do appreciate your help, you did not make any mention of what in_text is equal to, or how to make it equal to the data inside the document. Is this because you don't know, or that i'm asking the wrong question? (no intended offense of course, i really do appreciate all the help i get on this site)
in_text = fin._Stdstr; what will contain in_text after? (one word, one line, all the file ? )
If you want to dump all the data from the file to the string you can use
1
2
while( getline(fin, aux, '\n') )
  in_text += aux+"\n";


why 4096 ?

what i want to happen is similar to the split function, but ending at the last period
Don't sure if I understand you
1
2
3
while( getline(fin, aux, '.') )
  List.push_back(aux);
std::reverse( List.begin(), List.end() );
And then output the container, every cell to a different archive
closed account (4Gb4jE8b)
I wouldn't think in_text would need to be contained or pointed to.

in_text should be the whole file

4096 characters is equal to 4 kb of data, which is exactly what i'm trying to get.

what is aux in your example code?

the file is a book written to a txt file. There will be multiple new line characters, so using getline, at least as i understand it in your code, won't work will it?
std::string aux;
4096 characters is equal to 4 kb of data, which is exactly what i'm trying to get.
1
2
3
4
5
//getting a block of 4Kb
const in size = 4096;
char buffer[size]; //or char buffer[size+1] so you can end with '\0'
fin.open( filename, ios::binary );
fin.read( buffer, size );


the file is a book written to a txt file. There will be multiple new line characters, so using getline, at least as i understand it in your code, won't work will it?
The idea is to get a line, that I will know that ends with new line or it reaches the end of file. And just keep appending every line with a "\n" at the end. So in_text should have all the file in memory.
Topic archived. No new replies allowed.