Stringstream object and text file data access issues!

Hey guys! Please I need your help. I have two puzzling issues I am dealing with.

Issue 1: I am using a stringstream object in a block of my program that needs to be visited repeatedly depending on a user's selection from a menu. I want the contents of this stringstream object to be cleared any time control gets to this part of the program. I have tried the clear and flush functions to no avail. Any ideas?

Issue 2: I am reading data from a source text file that would be regularly changed during the course of program run. After the program run is over, I am supposed to save the results(which is basically the source text file AND all updates) in a destination file. This destination file would then serve as the source file when next the program is run. In other words, I want a scenario where my results overwrite the original contents of the source file; implying that my source and destination files are now one, pretty much. How can I do this?
1. The documentation of clear() and flush() makes it clear that they are not the functions that you are looking for. Try:
http://www.cplusplus.com/reference/sstream/stringstream/str/

2. How about:
- read file to memory
- modify memory
- (over)write memory to file
Thx keskiverto, I have resolved the first issue. I am still having problems with the second one though. Your second suggestion is the trivial solution that first pops up in one's mind which I have tried. I opened the same file name as my source and destination in function main(); read the data into the program; however, I observed I could not access the 'read' contents of the file during program execution. It defies logic for this to happen; I hope it's not some oversight of mine somewhere else in the program. Is this the same approach you would have adopted?
Close file after reading. Open for write only at end.
1
2
3
4
5
6
7
8
9
10
11
12
13
Foo data;

{
  istream f("file");
  f >> data;
}

data += 42;

{
  ostream f("file", overwrite-mode-flag);
  f << data;
}
It's still not working! What happens is I always find a blank file after the program has run. In essence, I cannot see any output showing the results. Consequently, the next run of the program has no source data to load!!

What is that
overwrite-mode-flag
stuff in your post? I'm guessing it's some file read/write mode; right?
openmode. See the documentation for std::ofstream.

Mine writes fine:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include <string>
#include <fstream>
#include <iostream>

int main()
{
  std::string data;
  {
    std::ifstream f("trunc.txt");
    f >> data;
  }
  std::cout << data << '\n';
  data = "world\n";
  {
    std::ofstream g("trunc.txt", std::ios::trunc | std::ios::out);
    g << data;
  }
  return 0;
}

$ g++ -W -Wall -o trunc trunc.cpp
$ echo "Hello" > trunc.txt
$ cat trunc.txt
Hello
$ ./trunc
Hello
$ cat trunc.txt
world
$
I can see that your data is roughly a simple data type(a string); mine is a structured data type (a multi-data-member object) based off of a linked-list class and several other classes. I overloaded the stream insertion operator for two of the classes that are derived classes from the linked-list base class. However, the function tasked with printing to the file is a member function in one of the derived classes. Should this difference in data formats have any bearing, at all, on this read-and-write-same-file scheme I am seeking?
Last edited on
@keskiverto:
Thx for your replies. I have finally resolved the issue; it is now working.
Topic archived. No new replies allowed.