Reading a string from a file and replacing it

closed account (jhb7Djzh)
This is what I have thus far. I'm leaving the input file alone and trying to replace "#N#" with a new string name and outputting it to a new file. My compiler is telling me .get can only be used for chars and not strings. So how would I get it to read a string then replace it with another string? Thanks.
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
  #include <iostream>
#include <string>
#include <fstream>
#include <cstdlib>

using namespace std;
void add_name(ifstream& in_stream, ofstream& out_stream, string name);

int main(){

	ifstream fin;
	ofstream fout;
	string name;
	
	fin.open("junkmail-body.txt");
	if(fin.fail())
	{
		cout << "Error opening file.";
		exit(1);
	}
	
	fout.open("junkmail-body-name.txt");
	if(fout.fail())
	{
		cout << "Error opening file";
		exit(1);
	}
	
	add_name(fin, fout, name);
	fin.close();
	fout.close();
	return 0;
	

}
//add_name fcn/////////////////
void add_name(ifstream& in_stream, ofstream& out_stream, string name)
{
	cout << "Enter your name: ";
	cin >> name;
	
	string place;
	in_stream.get(place); //reading a string
	
	while(! in_stream.eof()){
		if(place == "#N#")
			out_stream << name; //replacing #N# with name string
		else
			out_stream << place; //leaves it alone	  
	}
}
Use the extraction operator (>>).
1
2
string place;
in_stream >> place;

Note that like cin, this will extract up to a whitespace character, so if you have, for example "Hello World", only "Hello" will be extracted. If you want to extract the whole line "Hello World", use std::getline().
http://www.cplusplus.com/reference/string/string/getline/
how would I get it to read a string then replace it with another string
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
#include <iostream>
#include <string>
#include <utility>
#include <fstream>

int main()
{
    std::ifstream inFile("F:\\test.txt");

    std::ofstream outFile ("F:\\test1.txt");
    if(inFile)
    {
        std::string word{};
        while (inFile >> word)
        {
            if (word == std::string("#N#"))
            {
                word = std::move(std::string("replaced"));
            }
            if(inFile)
            {
                outFile << word << " ";
            }
        }
    }
}

Last edited on
closed account (jhb7Djzh)
I used integralfx's suggestion. It works for the first case, but the 2 other "#N#" stay the same.
Also, all the spaces disappear and everything is pushed onto one line.

For example: input file:

#N#
Dear #N#,
Now it the time...

output file:

BobDear#N#Nowitthetime...
Did you solved your issue? Just because I was wondering if your line number 43
in_stream.get(place); //reading a string
shouldn't stay inside the while-loop, like:
1
2
3
while(! in_stream.eof()){
    in_stream.get(place); //reading a string
    if(place == "#N#")

closed account (jhb7Djzh)
I tried that, it would still only change the first instance of "#N#".
Could you provide a piece of your input file to carry out some tests on it?
And perhaps add some details about the expected output?
This version seems to work (sorry, I made some little changes at the beginning... you can reject them without consequences):
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
#include <iostream>
#include <string>
#include <fstream>

using std::cin;
using std::cout;
using std::endl;
using std::ifstream;
using std::ofstream;
using std::string;

void add_name(ifstream& in_stream, ofstream& out_stream, string name);

int main(){

  ifstream fin;
  ofstream fout;
  string name;

  fin.open("junkmail-body.txt");
  if(fin.fail())
  {
      cout << "Error opening file.";
      exit(1);
  }

  fout.open("junkmail-body-name.txt");
  if(fout.fail())
  {
      cout << "Error opening file";
      exit(1);
  }

  add_name(fin, fout, name);
  fin.close();
  fout.close();
  return 0;


}
//add_name fcn/////////////////
void add_name(ifstream& in_stream, ofstream& out_stream, string name)
{
  cout << "Enter your name: ";
  cin >> name;

  string place;

  while(getline(in_stream, place)){ //reading a string
      if(place == "#N#")
          out_stream << name; //replacing #N# with name string
      else
          out_stream << place; //leaves it alone
  }
}


unkmail-body.txt:
1
2
3
4
5
6
7
Ann
Bill
#N#
John
Sam
#N#
Sue


Output:
Enter your name: Enoizat


junkmail-body-name.txt:
AnnBillEnoizatJohnSamEnoizatSue

The problem is it doesn't make any sense, since you pass the string "name" and then you ask for the name inside the function - the two methods clash each other, I think.
Last edited on
Topic archived. No new replies allowed.