[英]output as format in C++
我有一個文本文件template.txt:
至:
| 1 | | 2 | | 3 |
| 4 |
| 5 |,| 6 | | 7 |
尊敬的| 1 | | 3 |:您和| 3 | 家庭可能是C ++編程競賽中$ 1,000,000的幸運獲獎者!
我有一個文本文件database.txt:
先生|哈里|摩根| 1105 Main st |舊金山| CA | 95014 |
博士|約翰|李|第九街702號第4公寓|聖何塞| CA | 95109 |
小姐|伊夫琳|加西亞| 100大學廣場|安娜堡|密歇根州| 48105 |
我的代碼可以正確顯示database.txt中的第一行作為template.txt中的格式。
我在last.txt(由我自己創建)中的輸出:
致:哈里·摩根先生1105 Main st San Francisco,CA 95014親愛的摩根先生:您和摩根家族可能是C ++編程競賽中$ 1,000,000的幸運獲獎者!
我也想在last.txt中以相同的格式顯示接下來的兩行。 我使用了while循環,甚至創建了一個新函數,它根本不起作用。 希望可以有人幫幫我! 謝謝!
#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");
}
處理名稱文件中的一行后,可以使用seekg
重新定位到模板文件的開頭。 但是,您需要重組代碼以使其具有兩個循環。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.