char * pointer question

closed account (4Gb4jE8b)
How do I initialize and use one?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
	
for(x = 0;x<number;x++,y++)
{
	ostringstream child_file;
	istringstream s;
	child_file << std_name << " Pt " << y << ".txt";
	test = child_file.str();
	fout.open(test.c_str());
	if(fexists(test.c_str()) == false)
	{
		cout << "\nIn file " << test.c_str() << "\n";
		cout << "Error: file unable to be made correctly\n";
		pause();
		return 1;
	}
	fin.open(File_name_opened.c_str());
	fin.seekg(4096*x);
	fin.read(s.str(),4096);
	fout << s.str();
	fin.close();
	fout.close();
}


in the fin.read() function it is saying that it needs a "char*_Str" and a stringsize count. I have the count down, but how would I accurately initialize a "char*_Str" that can then be passed into fout as a string?
You're trying to read from a file into a string, you can't do it that way. If it's a text file (as opposed to a binary file), it's normal to use getline to read an entire line into a string.

For example:
1
2
3
4
    string line;
    ifstream fin(inputfile.c_str());
    while (getline(fin, line))
        cout << line << std::endl;
closed account (4Gb4jE8b)
But I need to get a specific set of 4096 characters, and from what i've read getline is unable to do that.
That's fine, grab everything and load it into memory first. THEN parse through the data and pull out what you need.

People say that parsing data is trickey but you just have to process it one step at a time.
Do you know any way of identifying the data? Is the data prefixed with anything? Is the header to the file in a standard format? Is there a set pattern that the data forms? Checksum? Anything? After we have this we can help you impliment it into code.
closed account (4Gb4jE8b)
Well the data loaded by fin.open() is going to be any .txt file greater than 4kb. The goal is to then split it into a set of files, each 4kb in size, with the same text as in the original text file. That's why I don't want to use getline, because it requires a specific type of character that may not be in the text file.

It is to be used as a way of getting large files of text onto my iPod, so i can read books off my iPod from gutenburg.org. So to say that the data follows a set pattern would not be true as the books can follow any pattern.

I hope that answered your questions... if not let me know and i'll do my best
Pfft, you're doing too much work my friend. 4kb of data in a .txt file will always be X number of characters. Find out what X is, round back to the last puctuation mark and parse away.

iPod huh? I remember looking at the Mac API once, I don't believe I have ever experinanced so much rage from simply reading a document at any other point in my life. It was like a dyslexic monkey skimmed Charles Babbages footnotes about the first computer then tried to build one from memory making software commands up on the fly. End of rant although I will point out the lack of a Mac section in any major C++ site ;) something to think about.
closed account (4Gb4jE8b)
4096 characters, already figured out. I'm not actually using the MAC API or anything, I'm building this as a windows console app.

I don't quite understand how to do what you're saying though that is exactly what i want to do. Which is why i'm here. Care to show me an example of how you would do it?

and that sounds absolutely horrific...
Untested code:
1
2
3
4
5
6
7
8
9
10
int main()
{
   //Code to read 4096 characters from a file to a string named FData

   int i = FData.rfind('.', 4096); //Finds the LAST period in the FData string 4096 characters long
   
   std::string MyData = FData.substr(0, i); 
   /*Populates a new string with everything from the first  character in FData to the last period found.*/

   //Output to newfile. 


That's the gist of it anyway. Check out some more stuff here:
http://www.cplusplus.com/reference/string/string/
closed account (4Gb4jE8b)
Alright using what you did I've made this 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
35
for(x = 0;x<number;x++,y++)
{
	//Writing the file names
	ostringstream child_file;		//allows for dynamic manipulation of strings
	string in_text(File_name_opened.c_str()),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());

	//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;
	cout << "last character = "<< last_char << "\nFinal text = " << final_text << "\n";
	fin.close();
	fout.close();
}


I have a feeling i'm very very close to my goal, but I am unable to initialize in_text to the data that is in the opened text file, causing multiple lines to return -1.

so hopefully my last question on this project, how would I initialize the in_text to make it a string of the data inside File_name_opened.c_str()?
If File_name_opened is a string as well, you can just assign them normally without using c_str().
closed account (4Gb4jE8b)
oh i understand that, but that doesn't answer my current question
It is to be used as a way of getting large files of text onto my iPod
split already does that.
http://pubs.opengroup.org/onlinepubs/009695399/utilities/split.html

If you want to do it yourself, you need to treat the file as binary. The algorithm's something like:
1
2
3
4
5
6
7
8
9
10
declare 4k buffer
declare count := 0
open input file
while !input.eof
    read buffer (last chuck will be less than 4k)
    output file name = :count
    open output file
    write buffer to output file
    increment count
end while
Last edited on
@ kbw: I thought about something like that at first to, but then I imagined how obnoxious it would be to have a text file end in the middle of a sentence which is where my suggestion about parsing on the punctuation came from. Do you think it adds too much work to the scope of the project?
I see what you mean. I think your suggestion has merit. Mine completely ignores the content, so isn't as helpful as yours.
Topic archived. No new replies allowed.