I have a text file template.txt:
To:
|1| |2| |3|
|4|
|5|, |6| |7|
Dear |1| |3|: You and the |3| family may be the lucky winner of $1,000,000 in the C++ programming competition!.....
and i have a text file database.txt:
Mr.|Harry|Morgan|1105 Main st|San Francisco|CA|95014|
Dr.|John|Lee|702 Ninth St Apt 4|San Jose|CA|95109|
Miss|Evelyn|Garcia|100 University Place|Ann Arbor|MI|48105|
my code can display the first line in database.txt as the format in template.txt correctly.
my output in the last.txt(created by myself) :
To: Mr. Harry Morgan 1105 Main st San Francisco, CA 95014 Dear Mr. Morgan: You and the Morgan family may be the lucky winner of $1,000,000 in the C++ programming competition!.....
I want to display the next two lines as the same format in last.txt as well. I used while loop, even make a new function, it does not work at all. Hope someone can help me! thanks!
#include<iostream>
#include<fstream>
#include<string>
#include <cstdlib>
using namespace std;
void edit(ifstream& data, ifstream& temp, ofstream& last)
{
char x;
char x2, x3;
string arr[21];
string line;
for (int j = 1; j<21; j++)
{
getline(data, line, '|');
arr[j] = line;
cout << arr[j] << endl;
}
while (temp.get(x))
{
if ('|' == x)
{
temp.get(x2);
if (isdigit(x2))
{
temp.get(x3);
if ('|' == x3)
{
int n = x2 - '0';
//n+=7;
last << arr[n];
}
else
temp.putback(x3);
}
else
temp.putback(x2);
}
else
last << x;
}
}
int main()
{
ofstream last;
ifstream temp;
ifstream data;
temp.open("template.txt");
data.open("database.txt");
last.open("last.txt", fstream::app);
if (data.fail()) { return 0; }
edit(data, temp, last);
system("pause");
}
You can use seekg
to reposition to the beginning of the template file after processing a line from the names file. You'll need to restructure your code to have two loops though.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.