Ubuntu setw()和流的问题

Ubuntu setw()和流的问题,ubuntu,c++,virtualbox,iostream,Ubuntu,C++,Virtualbox,Iostream,当我将setw()与流的一起使用时,它没有正确地写入文件。它跳过了许多输出行,在Notepad++中,输出文件显示了许多空值。但是当我尝试使用“\t”时,效果很好 你能解释一下原因吗 #include<fstream> #include<iomanip> using namespace std; int main(){ ofstream myfile; myfile.open ("test.txt"); for(int i=0;i<300

当我将
setw()
与流的
一起使用时,它没有正确地写入文件。它跳过了许多输出行,在Notepad++中,输出文件显示了许多空值。但是当我尝试使用
“\t”
时,效果很好

你能解释一下原因吗

#include<fstream>
#include<iomanip>
using namespace std;
int main(){

    ofstream myfile;
    myfile.open ("test.txt");

    for(int i=0;i<300;i++){
        myfile <<"\n"<<setw(15)<<0<<setw(15)<<0<<setw(15)<<10000<<setw(15)<<6000;
        //myfile <<"\n\t"<<0<<"\t"<<0<<"\t"<<10000<<"\t"<<6000;
    }
    return 0;
}
#包括
#包括
使用名称空间std;
int main(){
流文件;
myfile.open(“test.txt”);

对于(int i=0;i而言,解决方案可能是确保确实使用空格(或其他字符)来设置数据之间的宽度:

#include<fstream>
#include<iomanip>
using namespace std;
int main(){
    char a='î';
    int  b=15;
    ofstream myfile;
    myfile.open ("test.txt");

    for(int i=0;i<3;i++){
           myfile <<'\n'       <<setfill(a)<<setw(b)
                  <<0          <<setfill(a)<<setw(b)
                  <<0          <<setfill(a)<<setw(b)
                  <<10000      <<setfill(a)<<setw(b)
                  <<6000       ;
    }
    myfile <<'\n';
    return 0;
}

试着把
setfill(“”)
也放进去,它可能不会默认为空格。只是一个猜测。与我的MinGW g++配合很好。你的编译器和操作系统是什么?在我的Mac上作为命令行工具(使用g++编译)很好,在终端上运行。如果你只是“cp”,你会看到什么在您的Ubuntu VM上创建的好文件到主机上的安装文件夹中。换句话说,尝试解决问题是否出现在C++运行时或虚拟文件系统本身中。
::::::::::::::0::::::::::::::0::::::::::10000:::::::::::6000
::::::::::::::0::::::::::::::0::::::::::10000:::::::::::6000
::::::::::::::0::::::::::::::0::::::::::10000:::::::::::6000