C++ 如何格式化.txt文件中垂直行中的特定文本?

C++ 如何格式化.txt文件中垂直行中的特定文本?,c++,format,C++,Format,我使用以下示例代码成功地将1到10之间的一些随机数据写入文件: for (int i = 1; i <= size; i++) { type = rand () % 3; switch (type) { case 0: afile << rand () % 10; break; case 1: afile << rand () % 10; afile << "

我使用以下示例代码成功地将1到10之间的一些随机数据写入文件:

for (int i = 1; i <= size; i++)
{
    type = rand () % 3;
    switch (type)
    {
    case 0:  afile << rand () % 10;
             break;
    case 1:  afile << rand () % 10;
             afile << "\t\t";
             afile << rand () % 10;
             break;
    case 2:  afile << rand () % 10;
             afile << "\t\t";
             afile << rand () % 10;
             afile << "\t\t";
             afile << rand () % 10;
    }

    afile << "\t\t" << "// Possible type " << i << endl;
}
如何设置注释的格式,使其保持在一条垂直线上,如下所示:

8       // Possible type 1
1       7       // Possible type 2
4       0       3       // Possible type 3
8                       // Possible type 1
1       7               // Possible type 2
4       0       3       // Possible type 3

我尝试了
setw
,但没有成功,因为我正在运行一个循环,这使得
setw
在这里不适用。有什么功能可以帮助我解决这个问题吗

一个选项是更新您的
案例
声明,如下所示:

switch (type)
{
case 0:  afile << rand () % 10;
         afile << "\t\t\t\t\t\t";
         break;
case 1:  afile << rand () % 10;
         afile << "\t\t";
         afile << rand () % 10;
         afile << "\t\t\t\t";
         break;
case 2:  afile << rand () % 10;
         afile << "\t\t";
         afile << rand () % 10;
         afile << "\t\t";
         afile << rand () % 10;
         afile << "\t\t";
}
开关(类型)
{

案例0:afile一种方法是在中断前的switch语句中:

for (int i = 1; i <= 3; i++)
{
    type = rand() % 3;
    switch (type)
    {
    case 0:  
        afile << rand() % 10;
        afile << "\t\t\t\t\t\t" << "// Possible type " << i << endl;
        break;
    case 1:  
        afile << rand() % 10 << "\t\t" << rand() % 10;
        afile << "\t\t\t\t" << "// Possible type " << i << endl;
        break;
    case 2:  
        afile << rand() % 10 << "\t\t" << rand() % 10 << "\t\t" << rand() % 10;
        afile << "\t\t" << "// Possible type " << i << endl;
    }
}

for(int i=1;i为什么不在
案例
语句中添加一些额外的制表符呢?您正在数字之间添加两个制表符……但不是在数字丢失时……为案例0添加
“\t\t\t”
,为案例1添加
“\t\t”