Replacing String from a text file

Dec 22, 2008 at 9:06pm
Hello, I've been searching this website for hints on how to search and replace a string from a text file. What I've come up with is a program that opens and reads a text file and gets each line in a buffer. In each line, it is supposed to find and replace strings that I have initiated. However, I am not sure if it is finding and replacing anything. Below is my code, are there any modifications anyone can suggest? Thank you all.

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

int main () {

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


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



  ifstream infile;

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

  while (infile.good())

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

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

   
	string str2="exit";
	string str="t";
	
	 
	g.replace(g.find(str2), str2.length(), "str");

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


  infile.close();

  return 0;
}
Last edited on Dec 22, 2008 at 9:07pm
Dec 22, 2008 at 9:12pm
Just have it cout 'g' out and see if it replaced anything...it looks like it's fine though.
Dec 22, 2008 at 9:25pm
Hmmm, I tried as you said firedraco, however, my compiler is telling me I have one error. This is what I wrote starting from line 34.

1
2
3
4
5
6
7
8
9
	g.replace(g.find(str2), str2.length(), "str");

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


  infile.close();

  return 0;
}
Dec 22, 2008 at 9:37pm
What was the error....?
Dec 22, 2008 at 9:44pm
I'm not sure, but this is what the build reported...

--------------------Configuration: work1 - Win32 Debug--------------------
Compiling...
catcode.cpp
C:\Documents and Settings\Administrator\Desktop\catcode.cpp(36) : error C2679: binary '<<' : no operator defined which takes a right-hand operand of type 'class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >' (or t
here is no acceptable conversion)
Error executing cl.exe.

work1.exe - 1 error(s), 0 warning(s)

Dec 22, 2008 at 10:14pm
I just figured it out.....I needed to use # include <string>

it works now. Thank you for helping me out firedraco
Last edited on Dec 22, 2008 at 10:14pm
Dec 31, 2008 at 12:01am
Okay,

I have a new problem. I want to convert the elements in the string "g" into a character so that I can store them into a buffer and output those contents into a new file. How would I do this?

Here is my code with modifications

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

int main () {

FILE * pFile;

pFile = fopen ( "myfile2.bin" , "wb" );

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


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



  ifstream infile;
	ofstream outfile;
	outfile.open("myfile2.txt");


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

  while (infile.good())
  {

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

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

   
	string str2="trail";
	string str="no";
	
	 
	g.replace(g.find(str2), str2.length(), str);

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

	


  
  
  fwrite (buffer, 1 , sizeof(buffer) , pFile );
 fclose (pFile);
  infile.close();

  }

 

  return 0;






}
Topic archived. No new replies allowed.