Reading and Replacing a String in a Buffer

Jan 22, 2009 at 6:40pm
Hello all,

I am trying to replace a string in a buffer. I have code that will replace the string I'm looking for once. However, I need it to to replace every occurrence of the string. I'm not sure why my code is not replacing all of the occurrences of the string I'm looking for. The string I'm looking for in that file is and the code is below. Thank you.

The File is here:

http://filebox.vt.edu/users/jallen5/myfile/myfile.txt






Here is the code:


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
57
58
59
60
61
62
63
64
65
66
// print the content of a text file.
#include <iostream>
#include <fstream>
#include <string>
using namespace std;

int main () {



 // This is where we'll put the stuff we read from file
  char buffer[ 50000 ];


  // Fill the buffer with zeros (char strings must be null-terminated)
  fill_n( buffer, 50000, '\0' );



  ifstream infile;
  infile.open ("myfile.txt", ifstream::in);


ofstream                   fFile;
fFile.open("myfile3.txt", ofstream::out);

  
  while (infile.good())
  {

	// Read as many as 100 bytes (chars) from the file and stick them in our array
  infile.read( buffer,  50000);

  // Convert that char array into a STL string, and show the user what we got.
  string g( buffer );

   
	string str2="fr3y.txt";
	string str="fr3y1.txt";
	
	 
	g.replace(g.find(str2), str2.length(), str);

	cout << g << " successfully read from file.\n";


fFile << g << endl;
 
  infile.close();

fFile.close();


  }

 



  return 0;





}
Jan 22, 2009 at 7:28pm
Well, if you follow your program logically you will find the error...(it is in the while loop).
Jan 22, 2009 at 7:57pm
IMO. Don't use a character array. Use the actual string type, and when loading from the file use getline();
Jan 22, 2009 at 8:03pm
Hey Firedraco,

I have come up with three possibilities,

1. I need to replace str2.length() with str.length()
2. Instead of using infile.read(), I should use infile.getline()
3. I need to write: string g(buffer, 50000)

Are any of these correct?
Jan 22, 2009 at 8:05pm
Zaita, I'm not following:

Are you saying I need to get rid of the buffer?
Jan 22, 2009 at 8:08pm
I'm pointing out what you've said for possibility 2
Jan 22, 2009 at 8:39pm
Hello Zaita,

After following your suggestion as well as referencing the getline() command from this website, a problem is still occurring. Apparently, the program is terminating abnormally, can you help me with this matter? The code is below:

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
57
58
59
60
61
62
63
64
65
66
// print the content of a text file.
#include <iostream>
#include <fstream>
#include <string>
using namespace std;

int main () {



 // This is where we'll put the stuff we read from file
  char buffer[ 50000 ];


  // Fill the buffer with zeros (char strings must be null-terminated)
  fill_n( buffer, 50000, '\0' );



  ifstream infile;
  infile.open ("myfile.txt", ifstream::in);


ofstream                   fFile;
fFile.open("myfile3.txt", ofstream::out);

  
  while (infile.good())
  {

	// Read as many as 100 bytes (chars) from the file and stick them in our array
  //infile.read( buffer,  50000);
  infile.getline(buffer,  50000);
  // Convert that char array into a STL string, and show the user what we got.
  string g( buffer, 50000 );

   
	string str2="fr3y.txt";
	string str="fr3y1.txt";
	
	 
	g.replace(g.find(str2), str2.length(), str);

	cout << buffer << " successfully read from file.\n";


fFile << buffer << endl;
 
  infile.close();

fFile.close();


  }

 



  return 0;





}
Jan 22, 2009 at 9:40pm
Ok. I'll give ya some help.

You shouldn't be using character arrays. There is really no need for them.
You can't do a .replace on a string the way you have if the find returns -1
Your closing your files after every loop.

Some help:
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
#include <iostream>
#include <fstream>
#include <string>
using namespace std;

int main () {

  ifstream infile;
  infile.open ("input.txt", ifstream::in);

  string sReplace = "fr3y.txt";
  string sWith    = "fr3y1.txt";

  string sLine = "";
  while (getline(infile, sLine)) {    
    cout << "Before: " << sLine << endl;
    
    if ((int)sLine.find(sReplace) > 0) 
      sLine.replace((int)sLine.find(sReplace), sReplace.length(), sWith);
     
    cout << "After: " << sLine << endl << endl;
     
  }

  infile.close();

  return 0;
}


Edit: Fixed :) Shouldn't hack code while submitting it.
Last edited on Jan 22, 2009 at 10:26pm
Jan 22, 2009 at 10:20pm
Hey Zaita,

Thank you for the help I really appreciate it, however I'm still having a problem getting the program to run this is what i changed in your code to gel with the input text. However, the program terminates abnormally

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
// print the content of a text file.
#include <iostream>
#include <fstream>
#include <string>
using namespace std;




int main () {

  ifstream infile;
  infile.open ("myfile.txt", ifstream::in);

  string sReplace = "fr3y.txt";
  string sWith    = "fr3y1.txt";

  string sLine = "";
  while (getline(infile, sLine)) {
    if ((int)sLine.find(sReplace) > 0)
      cout << "Before: " << sLine << endl;
      sLine.replace((int)sLine.find(sReplace), sReplace.length(), sWith);
      cout << "After: " << sLine << endl; << endl;
  }

  infile.close();

  return 0;
}
Jan 22, 2009 at 10:25pm
The if inside your while has no braces.
There's also a loose << endl; at the end of line 23.
Last edited on Jan 22, 2009 at 10:26pm
Jan 22, 2009 at 10:27pm
Fixed the code :) I made changes after copying+pasting from IDE. Always a bad idea :P
Jan 22, 2009 at 10:38pm
Zaita! you're the best. Thank you so much for your help :)!
Jan 22, 2009 at 10:41pm
It's not complete, but you get the idea of what your trying to do I hope.\
Key points are not using char arrays, and checking before you try to replace.
Jan 23, 2009 at 9:23am
Okay, this is all that I want my program to do. It runs, but it is getting hung up somewhere and is not writing the output files. I think it is due to an infinite loop, how would I fix this:

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
57
58
59
60
61
62
63
// print the content of a text file.
#include <iostream>
#include <fstream>
#include <string>
#include <sstream>

using namespace std;

string make_filename( const string& basename, int index, const string& ext )
  {
  ostringstream result;
  result << basename << index << ext;
  return result.str();
  }

int main () {

	for (int n=0; n=5; n++)
	{



  ifstream infile;
  infile.open ("myfile.txt", ifstream::in);

  ofstream                   fFile;
fFile.open(make_filename("myfile",n,".txt").c_str());


  string sReplace = "fr3y.txt";
  string sWith    = make_filename("fr3y_",n,".txt").c_str();

  string sReplace2 = "Zoom_In";
  string sWith2 ="Zoom_out";

  string sLine = "";

  while (getline(infile, sLine)) {    
 cout << "Before: " << sLine << endl;
    
    if ((int)sLine.find(sReplace) > 0) 
      sLine.replace((int)sLine.find(sReplace), sReplace.length(), sWith);

	if ((int)sLine.find(sReplace2) > 0) 
      sLine.replace((int)sLine.find(sReplace2), sReplace2.length(), sWith2);




     
 cout << "After: " << sLine <<n<< endl << endl;
     fFile<<sLine<<endl;

	 
 }
  
 fFile.close();
  infile.close();
}

	
  return 0;
}
Jan 23, 2009 at 4:15pm
Nevermind, I figured it out.
Topic archived. No new replies allowed.