the data in a text file

closed account (4Gb4jE8b)
I've been stuck on this for weeks trying getline, read, seemingly everything. It's ridiculous :P

So can someone please answer me, in the following code, how would I make it so buffer is equal to the data inside the text file opened by fin.open()?

1
2
3
4
5
6
7
8
9
10
11
12
various declarations
using namespace std;
int main
{
	char* buffer;
	int last;
	fin.open(Parent_file.c_str());
	last = ceil(end);
        //here I need buffer = the text document.
	fin.close();
	pause();
}
Why would you need to do that?
What's the code supposed to do?
If you tell that it'll be easier for us to help you ;)
closed account (4Gb4jE8b)
i need to do that because the rest of the code works very very nicely when buffer is equal to a string of text (sounds cliche and obvious but hey). I've tested it using buffer = "whatever example text i want." and made everything work out nicely.

The program is actually going to be used for splitting up a text file into sets of 4kb. Like I said, i've gotten everything very nicely worked out up until this part. Why 4kb? Because i want to read books on my iPod.

sooooo.... any ideas on how i could make buffer equal the text in the document?
1
2
3
//size is the length of the text
buffer = new char[size]; //or size+1 so it ends with '\0'
fin.read(buffer, sizeof(char)*size); //fin must be open in binary mode 


Or read line by line, and append with a '\n' character
closed account (4Gb4jE8b)
I've been stuck on this for weeks trying getline, read, seemingly everything. It's ridiculous :P


ne555, i've tried read, i've tried that code. It doesn't work. In this case it's returning a buffer not being initialized error. that was a pch issue, my bad. In this case it's storing not storing anything though.

How can I do it without the file being in binary mode?
Last edited on
closed account (4Gb4jE8b)
...anybody?
http://www.cplusplus.com/reference/iostream/istream/getline/

You use getline and it is not working ? How about provide a runnable short code snippet so we can try out ourselves too.
closed account (4Gb4jE8b)
That's kind of really hard to do, as each variable is stored and used in each part of the program. But here, i've attached a full copy of the cpp file and header.

http://www.filedropper.com/filesplitter
http://www.filedropper.com/stdafx

hopefully these sites work, sorry if they don't, let me know and i'll find another site (or you can reference one yourself!)

edit:
they do but sorry bout the ads
Last edited on
77
78
79
80
81
82
83
84
	fin.open(Parent_file.c_str());
	fout.open(std_name.c_str());
	last = ceil(end);
	buffer = new char [last];
	fin.getline(buffer,last);
	fin.close();
	cout << "buffer is..." << buffer << "\n";
	pause();
What is the output? (IIRC should be one line)

You could forget about dynamic allocation with strings
1
2
string buffer;
getline( fin, buffer );


Edit: added context
Last edited on
closed account (4Gb4jE8b)
I don't quite understand what you mean ne555
buffer just contains the first line of your file, not all the file.
1
2
3
buffer = new char [last+1];
fin.getline( buffer, last+1, EOF ); //delim is '\n' by default
buffer[last] = '\0';
Also, don't call main as a recursive function.
You open fout in line 78, but is not closed.
114
115
		last_char = in_text.rfind('.',endp);
		if(last_char == string::npos)


The division logic is hard to follow. The file was divided, but not in equal parts, and were repeated data
3.4K 8.0K 4.6K 640
closed account (4Gb4jE8b)
I understand not to call main, but i've no idea how to get around it in this case.

Fout being open was due to previous lines of script that i have since deleted, thank you for catching it.

the division (should) make it so that each file ends at a period and that the next starts up after the space after the end of the period.

When i directly set buffer equal to more than 4kb of text in the code, this is exactly what happened.

I don't understand what you mean by "and were repeated data 3.4K 8.0K 4.6K 640"
Last edited on
Those were the sizes of the files. With repeated data I mean something like
Original: "the quick brown fox jumps over the lazy dog"
First: "the quick brown"
Second: "brown fox jumps over the"
Third: "jumps over the lazy dog"
closed account (4Gb4jE8b)
interesting. well, considering this still isn't working, even though i implemented what you said, i think i'll worry more about this than that for now. Buffer still isn't storing anything at all for me.
closed account (4Gb4jE8b)
any idea why or how to fix that?
closed account (4Gb4jE8b)
I'm now trying fin.get() and a stringbuf to try and make this work

1
2
3
4
5
6
        stringbuf buffer;
	fin.open(Parent_file.c_str());
	fin.get(buffer,EOF);
	fin.close();
	cout << "buffer is... '" << buffer.str() << "'\n";
	pause();


however fin.get is returning null as well as getline and read did. What's up with that?
Last edited on
Topic archived. No new replies allowed.