Processing
 

issue with setw() and ofstream

28/03/2012 22:35#1

Joined at: 7 months ago

Post: 1

Thank: 0

Thanked: 0

When I use setw() with ofstream, it's not writing to the file properly. It is skipping many lines of output and in Notepad++ the output file shows lots of NULLs. But when I try it with "\t" it works fine.

Can you please explain why?

#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; } 

Edit:

I tested a little more and figured out that the problem is specific to the following configuration. I'm using ubuntu guest on win7 using virtualbox and my program was attempting to write on a file on the host OS drive using virtualbox shared folder mounted on ubuntu.

When I tried to run the program inside ubuntu it worked fine. Also, I tried doing the same thing on my personal machine which also has win7 host and ubuntu guest. I tried I the same program on a mounted drive from win7 to ubuntu and it worked fine.

I'm wondering if it was because of the settings on my work computer. Anyway its not critical now. But more insights are welcome.

24/06/2016 09:27Top#2

J. Chomel

Member

Joined at: 10 months ago

Post: 7

Thank: 0

Thanked: 0

A solution might be to make sure you really use spaces (or other chars) to set the width between data:

#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; } 

gives

$ g++ a.cpp -std=c++0x -Wall -o a && ./a && cat test.txt

::::::::::::::0::::::::::::::0::::::::::10000:::::::::::6000 ::::::::::::::0::::::::::::::0::::::::::10000:::::::::::6000 ::::::::::::::0::::::::::::::0::::::::::10000:::::::::::6000 
Similar articles

Non-interactive method for dpkg-reconfigure tzdata

5 years ago - Reply: 3 - Views: 140

monit is not able to read mongod.lock

2 years ago - Reply: 3 - Views: 138

Ubuntu: hadoop command not found

2 years ago - Reply: 4 - Views: 244

C# Mono Console.ReadLine quitting

4 years ago - Reply: 1 - Views: 87

linking jsoncpp on Ubuntu

2 years ago - Reply: 1 - Views: 5

Build artifacts over ssh jenkins - PHP

2 years ago - Reply: 1 - Views: 37

Can't load Metasploit after installation

2 years ago - Reply: 5 - Views: 212

Selenium FirefoxDriver in Headless get error

2 years ago - Reply: 1 - Views: 96

Unable to start MongoDB. ERROR: addr already in use

2 years ago - Reply: 2 - Views: 118

ZSH Agnoster Theme showing machine name

2 years ago - Reply: 4 - Views: 248

Unable to start firefox browser with jenkins user

2 years ago - Reply: 2 - Views: 110

vé máy bay vé máy bay giá rẻ ve may bay ve may bay gia re vé máy bay vé máy bay giá rẻ ve may bay ve may bay gia re vé máy bay vé máy bay giá rẻ ve may bay ve may bay gia re vé máy bay vé máy bay giá rẻ ve may bay ve may bay gia re vé máy bay vé máy bay giá rẻ ve may bay ve may bay gia re vé máy bay vé máy bay giá rẻ ve may bay ve may bay gia re vé máy bay vé máy bay giá rẻ ve may bay ve may bay gia re