Re-use stringstream

I'm having some trouble re-using a stringstream. What I mean by re-use, is that I've filled the stringstream up with file contents so that I don't have to read the file again later on.
My program works pretty much like this:

1
2
3
fill stringstream fileContents from a fstream;
read fileContents to build GUI;
Add to fileContents/remove from fileContents;

Here is where my problem begins. When I allow the user to remove a line from the file, I try to use

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
stringstream newFC (stringstream::in|stringstream::out);
fileContents.seekg(0,ios::beg);//Move the get pointer to the beginning of the stringstream
fileContents.clear();//Reset the flags only
while(!fileContents.eof())
{
        getline(fileContents,buffer,','); 
        string buffer;
	getline(fileContents,buffer,','); 
	buffer.resize(buffer.size());//trim the strings for an accurate comparison
	if(buffer.compare(name) == 0)//name is a string passed into my function
	{
		getline(fileContents,buffer,',');
		getline(fileContents,buffer,'\n');
		count--;
	}
	else
	{
		newFC << buffer << ',';
		getline(fileContents,buffer,',');//Remove the second column associated with name
		newFC << buffer << ',';
		getline(fileContents,buffer,',');//Remove the third column associated with name
		newFC << buffer << ',';
	}
}
fileContents.str() = newFC.str();


where buffer is an empty string. However, when running the debugger I see that buffer is still empty after that line of code executes. It looks like I'm not moving the get pointer to the beginning of the stringstream to me, but I'm not sure why it isn't moving back. I'm trying to use as little memory as possible, so I'd rather not create a whole new stringstream that holds the exact same contents. Is there a way to do this?

I can't post a whole lot of my code, but I'll try to clarify if my question/code isn't clear.
Last edited on
clear() first, then seekg()

and of course, loop should be while(getline(...)) { ... }

I allow the user to remove a line from the file

How do you plan on removing something from the data held inside a stringstream? It may make more sense to read the file into a naked std::string instead, so that you can insert and erase.

(after edit: oh.. I see, you're filling in a new stream, and then copying its contents over into the old one.)
Last edited on
You have to clear the error flags before you can use seekg.
1
2
fileContents.clear();
fileContents.seekg(0,ios::beg);
Last edited on
Ah, that was it. Thanks guys!

Cubbi, that's what the newFC stringstream is for. Basically, the user types in a name to a text field and hits the Remove button. So I scan through the fileContents stringstream and clone it over into newFC. I skip over the name and it's associated values. Then, when the program terminates, I write the updated fileContents stringstream back to the disk. At least that's what it's supposed to do, I haven't gotten quite that far yet, haha.
Topic archived. No new replies allowed.