注明:本程序仅实现一行有7个字符串代码的对齐,只是供简单的参考。
//字符串对齐
#include <fstream>
#include <string>
#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
ifstream in("1.txt");
ofstream out("2.txt");
string s[1024];
int i = 0;
if (in) // 有该文件
{
while (in) //遍历所有字符串
{
in >> s[i];
// cout << s[i] << endl;
i++;
}
for (int j = 0; j<i - 1; j += 7)//以7个字符串为基础分配每一行
{
out << setw(17) << left << s[j]
<< setw(23) << left << s[j + 1]
<< setw(63) << left << s[j + 2]
<< setw(23) << left << s[j + 3]
<< setw(63) << left << s[j + 4]
<< setw(17) << left << s[j + 5]
<< setw(17) << left << s[j + 6] << endl;
}
}
else // 没有该文件
{
cout << "没有文件" << endl;
}
in.close();
out.close();
return 0;
}