Search and replace string in txt file in Dev c++ HELP!

My TEXT FILE: test.txt
id_1
arfan
haider

id_2
saleem
haider

id_3
someone
otherone
_________________
CODE

#include <iostream>
#include <fstream>
#include <string>

using namespace std;

int main(){
istream readFile("test.txt");
string readout,
search,
firstname,
lastname;

cout << "Enter the id which you want to modify";
cin >> search;

while(getline(readFile,readout)){
if(readout == search){
/*
id remains the same
But the First name and Last name are replaced with
the user `firstname` and `lastname` input
*/
cout << "Enter new First name";
cin >> firstname;

cout << "Enter Last name";
cin >> lastname;
}
}
}

i have an errors

-in function int main()
-[Error] no matching function for call to 'std::basic_istream<char>::basic_istream(const char [9])'

PLEASE HELP!
istream should be ifstream 'f' for 'file'
thanks how about this?

#include <iostream>
#include <fstream.h>
#include <string.h>

int main(){
ostream outFile("replaced.txt");
istream readFile("test.txt");
string readout;
string search, Fname, Lname;
unsigned int skipLines = 0;

cout << "Enter id which you want Modify";
cin >> search;
cout << "Enter new First name";
cin >> Fname;
cout << "Enter Last name";
cin >> Lname;

while(getline(readFile,readout)) {
if (skipLines != 0) {
skipLines--;
continue;
}
else if (readout == search) {
outFile << search << endl;
outFile << Fname << endl;
outFile << Lname << endl;
skipLines = 2;
}
else {
outFile << readout;
}
}
}

I have 1 error it said

[Error] fstream.h: No such file or directory
You still have:
istream readFile("test.txt"); which should be
ifstream readFile("test.txt");

In addition, you changed the correct headers
1
2
#include <fstream>
#include <string> 


to the incorrect
1
2
#include <fstream.h>
#include <string.h> 


<fstream.h> is not standard C++
<string.h> is a C header, it is something completely different to the C++ <string>

thanks for reply

i have 3 errors

ostream outFile("replaced.txt");

it said
-[Error] 'ostream' was not declared in this scope
-In function 'int main()':
-[Note] suggested alternative:

btw i created replaced.txt file
Last edited on
The istream --> ifstream problem is exactly the same as
ostream --> ofstream 'f' for 'file' again.
still the same errors
Topic archived. No new replies allowed.