C++ Not printing duplicate numbers to the output - c++

I have to read in a .txt file that contains votes used to determine who would win a pretend election. Here is a little bit of the file so you can get an understanding.
1YYYYYYYYYYThe New Guy
2YNYNYNYNYNHarry Potter
2YNNYYNNYYNHarry Potter
2NNNNNNNNNNThe New Guy
3NYNYNYNYNYThe New Guy
3YYYYYYYYYYHarry Potter
3YYYYYYYYNYHarry Potter
The first number is the "ID" number and if their are any duplicates of that number I am not supposed to read it into the output.The list of number goes all the way up to 99 with duplicates scattered throughout the file as shown. The numbers are all in order though. So it can go 1222333 as shown but cant do 122332. Here is the code I have so far, the loop just stops when it hits the first duplicate, any help would be appreciated.
#include <iostream>
#include <iomanip>
#include <fstream>
#include <sstream>
#include <limits>
#include <cstdio>
#include <string>
int main()
{
int ID; //pirate ID number
int IDTest;
char ch1, ch2, ch3, ch4, ch5, ch6, ch7, ch8, ch9, ch10; //vote on amendments
std::string vote; //vote for captain
std::ifstream fileReader; //open file for reading
fileReader.open("BallotsHogwart.txt");
if (fileReader.fail())
{
std::cout << "The ballots failed to open with code: " << fileReader.failbit;
}
else
{
do
{
IDTest = ID;
fileReader >> ID;
if (IDTest != ID)
{
std::cout << "Id:" << ID;
fileReader >> ch1;
fileReader >> ch2;
fileReader >> ch3;
fileReader >> ch4;
fileReader >> ch5;
fileReader >> ch6;
fileReader >> ch7;
fileReader >> ch8;
fileReader >> ch9;
fileReader >> ch10;
std::cout << " char 1 - 10: " << ch1 << ch2 << ch3 << ch4 << ch5 << ch6 << ch7 << ch8 << ch9 << ch10;
getline(fileReader,vote);
std::cout << " the votee: " << vote << std::endl;
}
} while(!fileReader.eof());
}
fileReader.close();
return 0;
}

You need to ignore the line when the ID is duplicated using getline()
The code will be like this:
#include <iostream>
#include <iomanip>
#include <fstream>
#include <sstream>
#include <limits>
#include <cstdio>
#include <string>
int main() {
int ID = 0; //pirate ID number
int IDTest;
char ch1, ch2, ch3, ch4, ch5, ch6, ch7, ch8, ch9, ch10; //vote on amendments
std::string vote; //vote for captain
std::string ignore; //ignore
std::ifstream fileReader; //open file for reading
fileReader.open("asdas.txt");
if (fileReader.fail())
{
std::cout << "The ballots failed to open with code: " << fileReader.failbit;
}
else
{
do
{
IDTest = ID;
fileReader >> ID;
if (IDTest != ID)
{
std::cout << "Id:" << ID;
fileReader >> ch1;
fileReader >> ch2;
fileReader >> ch3;
fileReader >> ch4;
fileReader >> ch5;
fileReader >> ch6;
fileReader >> ch7;
fileReader >> ch8;
fileReader >> ch9;
fileReader >> ch10;
std::cout << " char 1 - 10: " << ch1 << ch2 << ch3 << ch4 << ch5 << ch6 << ch7 << ch8 << ch9 << ch10;
getline(fileReader, vote);
std::cout << " the votee: " << vote << std::endl;
}
else {
getline(fileReader, ignore);
}
} while (!fileReader.eof());
}
fileReader.close();
std::cin.get();
return 0;
}

I would use a map
std::ifstream fileReader; //open file for reading
fileReader.open("BallotsHogwart.txt");
if (fileReader.fail())
{
std::cout << "The ballots failed to open with code: " << fileReader.failbit;
}
else
{
string line;
std::map<string, string> myvotes;
do
{
getline(filereader, line);
// pull out ID
std::map<string,string>::iterator value = myvotes.find(ID);
if (value == myvotes.end())
continue;
// do the rest of your processing on line.
// put "Harry Potter" in map.
myvotes.insert(ID, candidate);
} while(!fileReader.eof());
fileReader.close();
return 0;
}

Related

C++ writing content of a vector to a txt class?

So I made a function that reads lines from two files and writes them to a vector. Called the function twice, once for each of the files. Now how should I go for writing the content of the vector to a new file? Also, am I doing it correctly?
#include "stdafx.h"
#include <iostream>
#include <string>
#include <vector>
#include <fstream>
#include <algorithm>
using namespace std;
bool getFile(string filename, vector<string> & vecOfStrs){
ifstream in(filename.c_str());
if (in.fail()) {
cout << "No such file" << "\n";
return 1;
}
string str;
while (getline(in, str))
{
if (str.size() > 0)
vecOfStrs.push_back(str);
}
in.close();
return true;
}
int main(){
vector<string> A;
string lecCourse;
cout << "First file: ";
cin >> lecCourse;
string lab_ex;
cout << "Second file: ";
cin >> lab_ex;
string exit;
cout << "Name of exit file: " << "\n";
cin >> exit;
bool result = getFile(lecCourse, A);
bool result2 = getFile(lab_ex, A);
ofstream output;
output.open(exit.c_str());
if (output.fail()) {
cout << "error" << "\n";
return 1;
}
}
#include <iostream>
#include <string>
#include <vector>
#include <fstream>
#include <algorithm>
using namespace std;
vector<string> A;
bool getFile(string filename, vector<string> & vecOfStrs){
ifstream in(filename.c_str());
if (in.fail()) {
cout << "No such file" << "\n";
return 1;
}
string str;
while (getline(in, str))
{
if (str.size() > 0)
vecOfStrs.push_back(str);
}
in.close();
return true;
}
int main()
{
string lecCourse;
cout << "First file: ";
cin >> lecCourse;
string lab_ex;
cout << "Second file: ";
cin >> lab_ex;
string exit;
cout << "Name of exit file: " << "\n";
cin >> exit;
bool result = getFile(lecCourse, A);
bool result2 = getFile(lab_ex, A);
ofstream output;
output.open(exit.c_str());
for(int i=0;i<A.size();i++)
{
output<<A[i]<<"\n";
}
if (output.fail()) {
cout << "error" << "\n";
return 1;
}
}
Hope!!
This might helps:))

Assigning variables information from a infile txt C++

This is my text file:
12345 shoe 5 0
34534 foot 72 1
34562 race 10 0
34672 chicken 24 150
88 pop 65 0
I need to take this file and go row by row, assign the first number as an identifier itemNum, second word as itemName, third number as itemPrice, and last number as itemAdjusmentValue. I will need to perform arithmetic with the last two numbers itemPrice and itemAdjusmentValue.
Code so far:
using namespace std;
// making a struct to store each value of the cuadre
struct Cuadre
{
int itemNum;
string itemName;
int itemPrice;
int itemAdjusment;
};
int main (){
ifstream infile("sheet_1.txt");
string checkLine;
if (infile.is_open()){
while ( infile.good()){
getline (infile, checkLine);
cout << checkLine << endl;
}
}
else
cout << "error with name of file" << endl;
vector<Cuadre> procedures;
Cuadre line;
while(Cuadre >> line.itemNum >> line.itemName >> line.itemPrice >> line.itemAdjusment){
procedures.push_back(line);
}
This code generates an error on the last while statement
expected primary-expression before '>>' token
I cant really find a specific tutorial on how to do this, and i've looked a good amount.
From the code you posted, (with reference to the >> istream operators) it looks like you want to stream the contents of the string data read from your file into the members of a struct.
It is not possible to stream directly from a std::string (eg: checkLine >> x >> y >> z), as string does not provide a streaming interface.
In order to do that you need to use a stream, such as std::stringstream.
You could populate a with your string checkLine, and then stream from that into your data members
std::stringstream ss(checkLine);
ss >> line.itemNum >> line.itemName >> line.itemPrice >> line.itemAdjusment;
Example code:
#include <iostream>
#include <fstream>
#include <vector>
#include <sstream>
using namespace std;
// making a struct to store each value of the cuadre
struct Cuadre
{
int itemNum;
string itemName;
int itemPrice;
int itemAdjusment;
};
int main (){
ifstream infile("sheet_1.txt");
string checkLine;
vector<Cuadre> procedures;
if (infile.is_open()){
while ( infile.good()){
getline (infile, checkLine);
cout << checkLine << endl;
Cuadre line;
std::stringstream ss(checkLine);
ss >> line.itemNum >> line.itemName >> line.itemPrice >> line.itemAdjusment;
procedures.push_back(line);
}
}
else
cout << "error with name of file" << endl;
return 0;
}
You need to push into the vector inside the loop that reads from the file. And you should be getting the fields from checkLine -- Cuadre is a type name, not a variable you can read from. But to do that you need to create a stringstream.
int main (){
ifstream infile("sheet_1.txt");
string checkLine;
vector<Cuadre> procedures;
if (infile.is_open()){
while (getline (infile, checkLine)){
Cuadre line;
cout << checkLine << endl;
stringstream linestream(checkline);
if (linestream >> line.itemNum >> line.itemName >> line.itemPrice >> line.itemAdjusment) {
procedures.push_back(line);
} else {
cout << "incorrect line" << endl;
break;
}
}
}
else {
cout << "error with name of file" << endl;
}
}
while (infile.good()) is also not correct, it's essentially the same as while (!infile.eof()). See Why is iostream::eof inside a loop condition considered wrong?
You can try reading directly into struct as well:
#include <iostream>
#include <fstream>
#include <vector>
#include <string>
struct Cuadre
{
int itemNum;
std::string itemName;
int itemPrice;
int itemAdjusment;
};
int main()
{
std::vector<Cuadre> procedures;
std::ifstream infile("sheet_1.txt");
while (infile)
{
Cuadre line;
if (infile >> line.itemNum >> line.itemName >> line.itemPrice >> line.itemAdjusment)
procedures.push_back(line);
else
break;
}
if (!procedures.empty())
{
for (auto &p : procedures)
std::cout
<< "itemNum: " << p.itemNum << "\t"
<< "itemName: " << p.itemName << "\t"
<< "itemPrice: " << p.itemPrice << "\t"
<< "itemAdjusment: " << p.itemAdjusment
<< std::endl;
}
else
std::cout << "error with file or data" << std::endl;
return 0;
}
Prints:
itemNum: 12345 itemName: shoe itemPrice: 5 itemAdjusment: 0
itemNum: 34534 itemName: foot itemPrice: 72 itemAdjusment: 1
itemNum: 34562 itemName: race itemPrice: 10 itemAdjusment: 0
itemNum: 34672 itemName: chicken itemPrice: 24 itemAdjusment: 150
itemNum: 88 itemName: pop itemPrice: 65 itemAdjusment: 0
If you define an overload of >> for your type Cuadre, you can read directly from the file into Cuadre objects.
std::istream& operator>>(std::istream& is, Cuadre & cuadre)
{
std::string s;
getline(is, s);
std::cout << s;
std::stringstream ss(s);
ss >> cuadre.itemNum >> cuadre.itemName >> cuadre.itemPrice >> cuadre.itemAdjusment;
return is;
// or without logging
return is >> cuadre.itemNum >> cuadre.itemName >> cuadre.itemPrice >> cuadre.itemAdjusment;
}
int main (){
ifstream infile("sheet_1.txt");
vector<Cuadre> procedures;
for (Cuadre line; infile >> line;) {
procedures.push_back(line);
}
// or with #include <algorithm>
std::copy(std::istream_iterator<Cuadre>{ infile }, {}, std::back_inserter(procedures));
}

Vector Function program issues

#include<iostream>
#include<string>
#include<fstream>
#include<vector>
using namespace std;
void check(ifstream &iFile)
{
if (!iFile.is_open())
{
cout << "Data file not found!" << endl;
system("pause");
exit(1); // exit the program if the file is not found.
}
}
void readIn(ifstream &iFile, vector<string> &fName, vector<string> &lName, vector<string> &jTitle, vector<string> &eID, vector<double> &hoursWorked, vector<double> &wage, vector<int> &deductions, vector<double> &sPay, string sTemp, double dTemp, int iTemp)
{
while (!iFile.eof())
{
iFile >> sTemp;
fName.push_back(sTemp);
iFile >> sTemp;
lName.push_back(sTemp);
iFile.ignore();
getline(iFile, sTemp);
jTitle.push_back(sTemp);
iFile >> sTemp;
eID.push_back(sTemp);
iFile >> dTemp;
hoursWorked.push_back(dTemp);
iFile >> dTemp;
wage.push_back(dTemp);
iFile >> iTemp;
deductions.push_back(iTemp);
iFile >> dTemp;
sPay.push_back(dTemp);
}
cout << "completed" << endl;
}
int main()
{
ifstream iFile;
iFile.open("data.txt");
check(iFile);
vector<string> fName, lName, eID, eStatus, jTitle;
vector<double> nPay, gPay, oPay, oHours;
vector<double> hoursWorked, wage, sPay;
vector<int> deductions;
// temporary names to pass to the vector
string sTemp; // string temp
double dTemp=0; // double temp
int iTemp=0; // integar temp
readIn(iFile, fName, lName, jTitle, eID, hoursWorked, wage, deductions, sPay, sTemp, dTemp, iTemp);
/* while (!iFile.eof())
{
iFile >> sTemp;
fName.push_back(sTemp);
iFile >> sTemp;
lName.push_back(sTemp);
iFile.ignore();
getline(iFile, sTemp);
jTitle.push_back(sTemp);
iFile >> sTemp;
eID.push_back(sTemp);
iFile >> dTemp;
hoursWorked.push_back(dTemp);
iFile >> dTemp;
wage.push_back(dTemp);
iFile >> iTemp;
deductions.push_back(iTemp);
iFile >> dTemp;
sPay.push_back(dTemp);
}*/
int sizeOf = fName.size();
for (int a = 0; a < sizeOf; a++)
{
cout << fName.size() << " FName " << fName[a] << " LName " << lName[a] << " JobTitle " << jTitle[a] << endl;
cout << "EmployeeID " << eID[a] << " Hours Worked " << hoursWorked[a] << " Hourly Wage " << wage[a] << endl;
cout << "Deductions " << deductions[a] << " Salary Pay " << sPay[a] << endl;
}
system("pause");
return 0;
}
I'm running into an issue where my function will not do anything. It will compile, but there is no output. The thing is when I take out the vector sPay from all the parts it works perfectly fine. Any suggestions as to why that one part isn't working? From my limited knowledge it should work perfectly fine, however I can't figure out what would be causing this.
My example text file is
Alan
WakeField
IT GUY
T2034
40
15
1
Hourly
0.00
Your input file does not match your reading code. There are 9 values in the file that you have shown, but your code is attempting to read only 8 values.
When readIn() gets to this code:
iFile >> dTemp;
sPay.push_back(dTemp);
It attempt to read a double but the file has Hourly instead, so the read fails.
So, either remove the Hourly line from the file, or else add a call to iFile >> sTemp to read that line.
Also, the parameters string sTemp, double dTemp, and int iTemp should be declared as local variables instead of as input parameters.
Also, readIn() is not doing any error handling. Your main() code makes an invalid assumption that the vector of first names exactly matches the size of the other vectors, but readIn() does not guarantee that.
And lastly, checking eof() before you have read anything is wrong. The stream's eofbit flag is not updated until a read operation attempts to read past EOF.
You should consider re-writing this code. For example, try something more like this:
#include <iostream>
#include <string>
#include <fstream>
#include <vector>
struct Employee
{
std::string fName;
std::string lName;
std::string title;
std::string eID;
double hoursWorked;
double wage;
int deductions;
std::string wageType;
double sPay;
Employee() :
hoursWorked(0), wage(0), deductions(0), sPay(0)
{
}
};
void check(std::ifstream &iFile)
{
if (!iFile.is_open())
{
std::cout << "Data file not found or unable to open!" << std::endl;
std::system("pause");
exit(1); // exit the program.
}
}
void readIn(std::ifstream &iFile, std::vector<Employee> &employees)
{
std::ios_base::iostate oldstate = iFile.exceptions();
iFile.exceptions(std::ifstream::badbit | std::ifstream::failbit);
try
{
do
{
Employee emp;
iFile >> emp.fName;
iFile >> emp.lName;
std::getline(iFile, emp.title);
iFile >> emp.eID;
iFile >> emp.hoursWorked;
iFile >> emp.wage;
iFile >> emp.deductions;
iFile >> emp.wageType;
iFile >> emp.sPay;
employees.push_back(emp);
}
while (!iFile.eof());
}
catch (const std::ios_base::failure &)
{
std::cout << "Data file corrupted!" << std::endl;
std::system("pause");
exit(1); // exit the program.
}
iFile.exceptions(oldstate);
std::cout << "completed" << std::endl;
}
int main()
{
std::ifstream iFile("data.txt");
check(iFile);
std::vector<Employee> employees;
readIn(iFile, employees);
int sizeOf = employees.size();
for (int a = 0; a < sizeOf; a++)
{
std::cout << "FName " << employees[a].fName
<< " LName " << employees[a].lName
<< " JobTitle " << employees[a].title
<< std::endl;
std::cout << "EmployeeID " << employees[a].eID
<< " Hours Worked " << employees[a].hoursWorked
<< " << employees[a].wageType << " Wage " << employees[a].wage
<< std::endl;
std::cout << "Deductions " << employees[a].deductions
<< " Salary Pay " << employees[a].sPay
<< std::endl;
std::cout << std::endl;
}
std::system("pause");
return 0;
}
Alternatively, since your data is line-based, you should use std::getline() to read each line, and then use std::istringstream to parse values:
void readIn(std::ifstream &iFile, std::vector<Employee> &employees)
{
std::string sTemp;
std::ios_base::iostate oldstate = iFile.exceptions();
iFile.exceptions(std::ifstream::badbit | std::ifstream::failbit);
try
{
do
{
Employee emp;
std::getline(iFile, emp.fName);
std::getline(iFile, emp.lName);
std::getline(iFile, emp.title);
std::getline(iFile, emp.eID);
std::getline(iFile, sTemp);
if (!(std::istringstream(sTemp) >> emp.hoursWorked))
iFile.setstate(std::ifstream::failbit);
std::getline(iFile, sTemp);
if (!(std::istringstream(sTemp) >> emp.wage))
iFile.setstate(std::ifstream::failbit);
std::getline(iFile, sTemp);
if (!(std::istringstream(sTemp) >> emp.deductions))
iFile.setstate(std::ifstream::failbit);
std::getline(iFile, emp.wageType);
std::getline(iFile, sTemp);
if (!(std::istringstream(sTemp) >> emp.sPay))
iFile.setstate(std::ifstream::failbit);
employees.push_back(emp);
}
while (!iFile.eof());
}
catch (const std::ios_base::failure &)
{
std::cout << "Data file corrupted!" << std::endl;
std::system("pause");
exit(1); // exit the program if the file is corrupted.
}
iFile.exceptions(oldstate);
std::cout << "completed" << std::endl;
}
I went derp for a moment. I forgot to read in the hourly or salaried before going to salary pay.

C++ reading from a .txt file line by line string and number data type on each line

So I am trying to read a .txt file in c++ program. Each line in the text file has firstName, lastName and yearlySalary (e,g, Tomm Dally, 120000).
I can seem to read a file properly - it skips the first column (firstName) and stops reading the data in after first line. Why is that?
#include <iostream>
#include <string>
#include <iomanip>
#include <fstream>
using namespace std;
int main()
{
string fName;
string lName;
double yearlyPay;
double backPayDue;
double newAnualSalary;
double newMonthlyWage;
int numOfEmployees = 0;
double totalBackPayDue = 0;
ifstream empSalariesOld("EmpSalaries.txt");
ofstream empSalariesNew("EmpSalariesNew.txt");
if (!empSalariesOld.fail())
{
while (empSalariesOld >> fName)
{
empSalariesOld >> fName >> lName >> yearlyPay;
std::cout << fName << " " << lName << " " << yearlyPay << endl;
numOfEmployees++;
}
}
empSalariesOld.close();
empSalariesNew.close();
system("pause");
return 0;
}
You are not reading the lines correctly.
When your while statement calls empSalariesOld >> fName for the first time, it reads an employee's first name. Then, inside the loop body, when you call empSalariesOld >> fName >> lName >> yearlyPay, >> fName reads the employee's last name (because you already read the first name), then >> lName reads the employee's salary, and >> yearlyPay tries to read the next employee's first name and fails!
Try something more like the following instead. Use std::getline() to read a whole line, and then use std::istringstream to parse it:
#include <iostream>
#include <string>
#include <iomanip>
#include <fstream>
#include <sstream>
using namespace std;
int main()
{
string fName;
string lName;
double yearlyPay;
//...
int numOfEmployees = 0;
ifstream empSalariesOld("EmpSalaries.txt");
ofstream empSalariesNew("EmpSalariesNew.txt");
if (empSalariesOld)
{
string line;
while (getline(empSalariesOld, line))
{
istringstream iss(line);
if (iss >> fName >> lName >> yearlyPay) {
std::cout << fName << " " << lName << " " << yearlyPay << endl;
++numOfEmployees;
}
}
}
empSalariesOld.close();
empSalariesNew.close();
cout << "Press any key";
cin.get();
return 0;
}
However, if the lines actually have a comma between the name and salary like you showed (Tomm Dally, 120000), then try the following instead:
#include <iostream>
#include <string>
#include <iomanip>
#include <fstream>
#include <sstream>
using namespace std;
int main()
{
string name;
double yearlyPay;
//...
int numOfEmployees = 0;
ifstream empSalariesOld("EmpSalaries.txt");
ofstream empSalariesNew("EmpSalariesNew.txt");
if (empSalariesOld)
{
string line;
while (getline(empSalariesOld, line))
{
istringstream iss(line);
if (getline(iss, name, ',') && (iss >> yearlyPay))
std::cout << name << " " << yearlyPay << endl;
++numOfEmployees;
}
}
}
empSalariesOld.close();
empSalariesNew.close();
cout << "Press any key";
cin.get();
return 0;
}

Search and replace string in txt file in c++

I want to find a string in a file and replace it with user input.
Here is my rough code.
#include <iostream>
#include <fstream.h>
#include <string.h>
int main(){
istream readFile("test.txt");
string readout,
search,
replace;
while(getline(readFile,readout)){
if(readout == search){
// How do I replace `readout` with `replace`?
}
}
}
UPDATE
Here is the code that solved my problem
test.txt:
id_1
arfan
haider
id_2
saleem
haider
id_3
someone
otherone
C++ Code:
#include <iostream>
#include <fstream>
#include <string>
using namesapce 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;
}
}
}
Suppose:
A user searches for id id_2. After that user enter First name and Last name Shafiq and Ahmed.
After runing this code the test.txt File must modify the record like that:
…
id_2
Shafiq
Ahmad
…
Only the id_2 record changes, the remaining file will stay the same.
This should work. I used string::find to find the desired substring within each line, and string::replace to replace it if something has been found.
Edit: I forgot about the case where the word occurs multiple times per line. Added a while to fix this.
#include <fstream>
#include <iostream>
using namespace std;
int main(int argc, char **argv)
{
ifstream in(argv[1]);
ofstream out(argv[2]);
string wordToReplace(argv[3]);
string wordToReplaceWith(argv[4]);
if (!in)
{
cerr << "Could not open " << argv[1] << "\n";
return 1;
}
if (!out)
{
cerr << "Could not open " << argv[2] << "\n";
return 1;
}
string line;
size_t len = wordToReplace.length();
while (getline(in, line))
{
while (true)
{
size_t pos = line.find(wordToReplace);
if (pos != string::npos)
line.replace(pos, len, wordToReplaceWith);
else
break;
}
out << line << '\n';
}
}
I would do what #stefaanv said:
#include <iostream>
#include <fstream.h>
#include <string.h>
int main(){
ostream outFile("replaced.txt");
istream readFile("test.txt");
string readout;
string search;
string replace;
while(getline(readFile,readout)){
if(readout == search){
outFile << replace;
}
else {
outFile << readout;
}
}
}
Edit: the above solution works if the information on each line is independent of the information on the other lines. In your update, the information on the name lines is dependent on the information on the id lines. So, to extend the above technique, you'll need to maintain state in the while loop that indicates when you've reached the end of one data block.
#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;
}
}
}
A slightly more elegant approach would be to store each data block in a struct, which allows you to use overloaded operators << & >>. This makes the code for file reading & writing more clear - it's practically the same as the code for the "data on each line is independent" situation.
#include <iostream>
#include <fstream.h>
#include <string.h>
struct NameRecord {
string id;
string fname;
string lname;
friend std::ostream& operator<<(std::ostream &os, const NameRecord &src);
friend std::istream& operator>>(std::istream &is, NameRecord &dst);
};
std::ostream& operator <<(std::ostream &os, const NameRecord &src) {
os << src.id << endl << src.fname << endl << src.lname << endl;
return os;
}
std::istream& operator >>(std::istream &is, NameRecord &dst) {
// may need to have more code to ignore whitespace, I'm not sure
if (is.good ()) {
is >> dst.id;
}
if (is.good ()) {
is >> dst.fname;
}
if (is.good ()) {
is >> dst.lname;
}
return is;
}
int main(){
ostream outFile("replaced.txt");
istream readFile("test.txt");
NameRecord inRecord, replaceRecord;
cout << "Enter id which you want Modify";
cin >> replaceRecord.id;
cout << "Enter new First name";
cin >> replaceRecord.Fname;
cout << "Enter Last name";
cin >> replaceRecord.Lname;
while (readFile.good()) {
// the >> operator reads the whole record (id, fname, lname)
readFile >> inRecord;
// the << operator writes the whole record
if (inRecord.id == replaceRecord.id) {
outFile << replaceRecord;
}
else {
outFile << inRecord;
}
}
}
#include <iostream>
#include <fstream>
using namespace std;
int main(int argc, char **argv) {
if (argc < 4) {
cout << "Invalid input" << endl;
cout << "\tchange <old_word> <new_word> <file_list>";
}
fstream fs;
string tmp;
string oldw = argv[1];
string neww = argv[2];
for (int i = 3; i < argc; i++) {
fs.open(argv[i] , ios::in);
while (!fs.eof()) {
getline(fs, tmp);
while (tmp.find(oldw) != string::npos)
tmp.replace(tmp.find(oldw), sizeof(oldw), neww);
cout << tmp << endl;
}
}
fs.close();
return 0;
}
Usage:
./a.out old_word new_word filename
You probably meant to write:
tmp.replace(tmp.find(oldw), oldw.length(), neww);
for this to work properly. sizeof() will most likely always return 4.