C ++打印输出文件内容会破坏文件中的格式

时间:2013-09-23 22:23:43

标签: c++ iostream

我有一个文本文件,格式如下:

fa, ha

ID                  Item                Price               Amount              Cost                
2310                3-Ring Binder       6.99                3                   20.97               

                                              Sales Tax:                1.63
                                               Shipping:                5.00

                                                  Total:               27.60

                                            Grand Total:               27.60
Thank you for shopping at the E-Store!

这就是我填充文件的方式:

orderFile  << setw(20) << left << "ID" << setw(20) <<  "Item"  << setw(20) <<  "Price" << setw(20) << "Amount "<< setw(20) <<  "Cost"<<"\n";
orderFile  << setw(20) <<  id << setw(20) <<   description  << setw(20) <<  price << setw(20) << amount<< setw(20) <<  cost<<"\n"<<"\n";
orderFile << setw(60) << right << "Sales Tax:" << setw(20) <<  fixed << setprecision(2) << saleTax << "\n";
orderFile << setw(60) <<  "Shipping:" << setw(20) << fixed << setprecision(2) << shipping << "\n"<<"\n";
orderFile << setw(60) <<  "Total:" << setw(20) <<  fixed << setprecision(2) << total << "\n"<< "\n";

使用以下代码打印出文件内容后:

string line;
while(getline(orderFile,line))
    cout << line;

控制台中的输出与文件格式不同,打印内容时会忽略一些新行

0 个答案:

没有答案