473,480 Members | 1,864 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Trouble with ofstream

29 New Member
I have a set of values that need to be output to a text file. The output includes both text and numbers so I'd like to line up the floating point numbers on the list so it is easy to read. The output to the file is handled through a std::ofstream. The output comes from a 3 x 3 matrix and for testing purposes, each floating point value in the matrix is preceded by the string "value = " when it is output to the stream.

As you can see below from the code sample, the string is aligned to the left of the field and the floating point value is aligned to the right of its field. So my desired output should look something like this:

Expand|Select|Wrap|Line Numbers
  1. value =       -4.000 value =       -4.000 value =       -4.000
  2. value =       -4.000 value =       -3.000 value =       -2.000
  3. value =       -4.000 value =       -2.000 value =        0.000
  4.  
However, for a reason beyond my comprehension, it seems that after telling the stream to align on the right side when I pass it the first value (at [0, 0]) the stream stays "stuck" on it and the command to align the text to the left gets ignored. So my output ends up looking like this:

Expand|Select|Wrap|Line Numbers
  1. value =       -4.000     value =  -4.000     value =  -4.000
  2.      value =  -4.000     value =  -3.000     value =  -2.000
  3.      value =  -4.000     value =  -2.000     value =   0.000
  4.  
Here is the sample code I am using for this:

Expand|Select|Wrap|Line Numbers
  1. // In File Parser.h
  2. #include <iostream>
  3. #include <fstream>
  4. #include <vector>
  5. #include <sstream>
  6. #include <string>
  7. #include <dirent.h>
  8. #include <iomanip>
  9.  
  10. // Note, I am including only relevant parts of this class, it's kind of large....
  11.  
  12. class Parser
  13. {
  14.    public:
  15.       Parser(std::string FileName, std::ios_base::openmode Mode);
  16.       ~Parser();
  17.       bool isOpen(){ return(mOpen); }
  18.  
  19.       void SetLineWriterPrecision(int p){ mLineWriter.precision(p); }
  20.       void SetLineWriterFlags(std::ios_base::fmtflags f){ mLineWriter.setf(f); }
  21.       void SetLineWriterFlags(std::ios_base::fmtflags f, std::ios_base::fmtflags m)
  22.       { mLineWriter.setf(f, m); }
  23.  
  24.       void WriteLine(string line)
  25.       {
  26.          mLineWriter << line << std::endl;
  27.       }
  28.  
  29.       template <typename T>
  30.       void WriteW(T item, int w, std::ios_base::fmtflags flag)
  31.       {
  32.          mLineWriter.width(w);
  33.          mLineWriter.setf(flag);
  34.          mLineWriter << item;
  35.       }
  36.  
  37.  
  38.    private:
  39.       std::ofstream mLineWriter;
  40.       std::ios_base::openmode mMode;
  41.       bool mOpen;
  42. };
  43.  
  44. // In StreamTest.cpp
  45. #include "Parser.h"
  46. #include "Matrix.h"
  47.  
  48. using namespace std;
  49.  
  50. int main(int argc, char *argv[])
  51. {
  52.    Parser parser("outStream.txt", ios::out);
  53.  
  54.    int s = 3;
  55.    if(parser.isOpen())
  56.    {
  57.       parser.SetLineWriterFlags(ios::fixed, ios::floatfield);
  58.       parser.SetLineWriterFlags(ios::showpoint);
  59.       parser.SetLineWriterPrecision(3);
  60.       //parser.SetLineWriterFlags(ios::right);
  61.       Matrix mat(s, s);
  62.       mat.SetIdentity();
  63.       for(int i = 0; i < s; ++i)
  64.       {
  65.          for(int j = 0; j < s; ++j)
  66.          {
  67.             mat(i, j) = (i * j) - (s*s)/2;
  68.             string o = "value =";
  69.             parser.WriteW(o, 12, ios::left);
  70.             parser.WriteW(mat(i, j), 8, ios::right);
  71.          }
  72.          parser.WriteLine("");
  73.       }
  74.       parser.Close();
  75.    }
  76.  
  77.    return(0);
  78. }
  79.  
So if anyone has an idea of what I might be doing wrong I would appreciate some feedback in correcting the alignment issue.

Thanks!
Feb 25 '09 #1
1 2593
meLlamanJefe
29 New Member
Ah, love it when I answer my own questions.... The answer by the way is buried within this page:

http://www.cyberiapc.com/master/clesson7.htm

many thanks to the author for pointing out one more of the non-intuitive "features" of the stl.....
Feb 26 '09 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

1
11349
by: red floyd | last post by:
Is there any way to retrieve the filename given to a std::ofstream (passed in constructor or in ofstream::open())? Or, should I derive from ofstream (should probably be a template to handle...
3
7801
by: Chase Bradford | last post by:
Hey all I have a class Foo, and I'm trying to overload the << operator for both the ostream and ofstream for it. This way I should have two seperate formats for output, one for files and another...
2
2972
by: Marina | last post by:
I get an "access violation" when I use someting like this: @@@@@@@@@@@@@@ string tempo; const char *output; vector <ofstream> outs(3); .... .... open_output=(const char *)tempo.c_str();...
2
448
by: slyphiad | last post by:
i'm kinda new at c++ so be patient ^_^ i was just wondering if u guys could help me to solve this problem that i had. i'm trying to create 5 sequential files using ofstream. this is what i...
5
18500
by: Squid Seven | last post by:
I'm trying to use a pointer to an ofstream object and having problems: ofstream *sessionFile = NULL; if( directory == "" ) sessionFile = new ofstream( fileName.c_str(), ios::out ); else {
1
4924
by: khaleel.alyasini | last post by:
anyone could point me out where did i do wrong? it seems that i can't get back the original Lena image after the IDCT(inverse discrete cosine transform) process. the output raw image is nothing...
5
3750
by: Gary Wessle | last post by:
Hi I have a map<string, doublem_temperatures which gets updated often. I need to save the data to files corresponding to each string each time the map is updated, I am expecting about 80 files...
5
11340
by: Joe Hesse | last post by:
Hi, I have a C++ function that writes to an ofstream object. I would like to sometimes use it to write to cout. I realize that cout is of type ostream which is not ofstream. Since cout is "kind...
15
5968
by: aaragon | last post by:
Hello, does anyone have a clue about this error? and how to solve it? It seems to be trivial to me, but not for the compiler. I'm using g++ 4.2 on an Ubuntu Linux system: // main() .......
0
7055
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7059
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7103
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
6758
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
4499
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3011
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3003
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1311
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
572
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.