Changing text in file

Hey guys,
Ok, I have a text file containing these elements...

<Create Character>
<Create Character>
<Create Character>
<Create Character>

now I prompt the user to pick a character because eventually there will be character names. How would I tell it to access line 1 and write "Frank" instead of <Create Character>?

Secondly, how when the file reads...

Frank
<Create Character>
<Create Character>
<Create Character>

tell the program to change the second line instead of the first? Thanks again you guys are awesome!!! :)
I would read the entire file into a vector.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <fstream>
#include <string>
#include <vector>

std::vector<std::string> read_names( const std::string& filename )
  {
  std::vector<std::string> result;
  std::string s;

  std::ifstream file( filename.c_str() );
  while (std::getline( file, s ))
    result.push_back( s );
  file.close();

  return result;
  }


You can then play with the list of names to your heart's content. When done, write them back over the file.
method one
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
// method one
#include <fstream>
#include <string>
#include <vector>

void write_names( const std::string& filename, const std::vector<std::string>& names )
  {
  std::ofstream file( filename.c_str() );

  for (std::vector<std::string>::const_iterator i = names.begin();
       i != names.end();
       i++)
    file << *i << std::endl;

  file.close();
  }

method two
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
// method two
#include <algorithm>
#include <fstream>
#include <iterator>
#include <string>
#include <vector>

void write_names( const std::string& filename, const std::vector<std::string>& names )
  {
  std::ofstream file( filename.c_str() );

  std::transform(
    names.begin(),
    names.end(),
    std::ostream_iterator( file ),
    std::bind2nd( std::plus<std::string>(), std::string( "\n" ) )
    );

  file.close();
  }


Hope this helps.
Because I already knew the amount of characters I am letting them create i set the strings from the file into an array, but the process of doing that was what you gave me and it helped out a lot!!! Thanks again Duoas, your the man!!

Here was my approach...

string names[4];

// Read in character names
ifstream charnames(dirName.c_str());
if (charnames.is_open())
{
for (int i = 0; i < 4; i++)
{
getline (charnames, names[i]);
}
charnames.close();
}
if (menuChoice == 'A' && names[0] == "<Create Character>")
{
name = names[0];
charCreate(name);
names[0] = name;
}
if (menuChoice == 'B' && names[1] == "<Create Character>")
{
name = names[1];
charCreate(name);
names[1] = name;
}
if (menuChoice == 'C' && names[2] == "<Create Character>")
{
name = names[2];
charCreate(name);
names[2] = name;
}
if (menuChoice == 'D' && names[3] == "<Create Character>")
{
name = names[3];
charCreate(name);
names[3] = name;
}

// Rewrite character names
ofstream newcharnames(dirName.c_str());
for (int i = 0; i < 4; i++)
{
newcharnames << names[i] << endl;
}


Passing the names to a procedure that creates the character and returns the names, than saves it into the array and rewrites the file...

Once again thanks Duoas!!!
P.S. How do you get your code in those cool little code boxes?
[code]
code goes here
[/code]

Glad to have been of help.
Thanks again!!
Topic archived. No new replies allowed.