help with find and replace

Hello;

I wrote this program read a text file and find "0,to 9," and replace them with "0 to 9 " Note there is a space after the digit instead of a comma. For some reason it will not read my string array, but will work if I hard code it to look for "0,"

for input text file, is just a "0,0,0,1,1,1,2,2,2,3,3,3 and so on to ,9,9,9"
for the output text file should be the same lines with the commas replaced with a space.

Thanks for any help

lank23

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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
#include <iostream>
#include <fstream>
#include <string>

using namespace std;

ifstream myfile_in ("test.txt");
ofstream myfile_out ("test1.txt");
string line;

void find_and_replace( string &source,  string find, string replace ) {

    size_t j;

    for ( ; (j = source.find( find )) != string::npos ; ) {
        source.replace( j, find.length(), replace );
        }

        myfile_out << source <<endl;
        cout << source << endl;
        }

int main () {

  if (myfile_in.is_open())
  {
    int i = 0;
    char mychar [] = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9' };
    string strcomma ;
    string strspace ;

    while (! myfile_in.eof() )
    {

      getline (myfile_in,line);

      strcomma = mychar [i] + ",";
      strspace = mychar [i] + " ";

      find_and_replace( line , strcomma , strspace );

      i++;

    }

    myfile_in.close();

  }

  else cout << "Unable to open file(s) ";

  system("PAUSE");
    return EXIT_SUCCESS;

  return 0;
}
Topic archived. No new replies allowed.