File editing problem

I am trying to edit a file called "weaponlevels.txt". It stores data in the format

weapon-weaponlevel

I want to let the user enter a weapon and the preferred level, have the program go through the file until it reaches the entered weapon, and change the weapon level to the level entered by the user.

Here's what I have so far:
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
36
37
38
39
40
41
42
#include <iostream>
#include <fstream>
#include <sstream>
using namespace std;

int main()
{
    string temp;
    string line;
    string weapon;
    string weaponent;
    string weaponlevel;
    cout<<"enter weapon"<<endl;
    cin>>weaponent;
    cout<<"enter level"<<endl;
    cin>>temp;
    ifstream infile("weaponlevels.txt");

    std::string in_str((std::istreambuf_iterator<char>(infile)),
                 std::istreambuf_iterator<char>());

    infile.close();

    stringstream infile_ss(in_str);


    while (getline(infile_ss, line)) 
    {
        istringstream ss(line);   
        getline(ss,weapon,'-');   
        if (weapon == weaponent)  
        {
            ss>>weaponlevel;
            weaponlevel=temp;
            infile_ss<<weaponlevel<<endl; 
        }
    } 

    ofstream outfile("weaponlevels.txt");
    outfile << infile_ss.str();
    outfile.close();
}


But it doesn't work. If i enter m4a1 and 7, the doesn't become m4a1-7, it becomes
1
2
7
a1-4


What is wrong with my code?
I myself would create an ostringstream and write the information to this stream instead of trying to read then write in the same stringstream.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
   ostringstream out;
   while (getline(infile_ss, line))
   {
      istringstream ss(line);
      getline(ss,weapon,'-');
      out << weapon << '-';   // Write the first part of the line.
      if (weapon != weaponent)
      { // Not the correct weapon so just write the original information.
         ss >> weaponlevel;
         out << weaponlevel << endl;
      }
      else
      { // Found the desired weapon, change the level.
         out << temp << endl;
      }
   }

   ofstream outfile("weaponlevels.txt");
   outfile << out.str();
   outfile.close();
thank you. so very, very much. I have been stuck on this same problem for a week. and you have solved it. just...thank you.
Programming self is nice, but there is 'sed' that does similar tasks.
http://en.m.wikipedia.org/wiki/Sed
uh...i'm on windows, not unix
sed, like many GNU tools, is available for Windows.
http://gnuwin32.sourceforge.net/packages/sed.htm
Topic archived. No new replies allowed.