Trouble with ofstream

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • meLlamanJefe
    New Member
    • Mar 2008
    • 29

    #1

    Trouble with ofstream

    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:

    Code:
    value =       -4.000 value =       -4.000 value =       -4.000
    value =       -4.000 value =       -3.000 value =       -2.000
    value =       -4.000 value =       -2.000 value =        0.000
    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:

    Code:
    value =       -4.000     value =  -4.000     value =  -4.000
         value =  -4.000     value =  -3.000     value =  -2.000
         value =  -4.000     value =  -2.000     value =   0.000
    Here is the sample code I am using for this:

    Code:
    // In File Parser.h
    #include <iostream>
    #include <fstream>
    #include <vector>
    #include <sstream>
    #include <string>
    #include <dirent.h>
    #include <iomanip>
    
    // Note, I am including only relevant parts of this class, it's kind of large....
    
    class Parser
    {
       public:
          Parser(std::string FileName, std::ios_base::openmode Mode);
          ~Parser();
          bool isOpen(){ return(mOpen); }
    
          void SetLineWriterPrecision(int p){ mLineWriter.precision(p); }
          void SetLineWriterFlags(std::ios_base::fmtflags f){ mLineWriter.setf(f); }
          void SetLineWriterFlags(std::ios_base::fmtflags f, std::ios_base::fmtflags m)
          { mLineWriter.setf(f, m); }
    
          void WriteLine(string line)
          {
             mLineWriter << line << std::endl;
          }
    
          template <typename T>
          void WriteW(T item, int w, std::ios_base::fmtflags flag)
          {
             mLineWriter.width(w);
             mLineWriter.setf(flag);
             mLineWriter << item;
          }
    
    
       private:
          std::ofstream mLineWriter;
          std::ios_base::openmode mMode;
          bool mOpen;
    };
    
    // In StreamTest.cpp
    #include "Parser.h"
    #include "Matrix.h"
    
    using namespace std;
    
    int main(int argc, char *argv[])
    {
       Parser parser("outStream.txt", ios::out);
    
       int s = 3;
       if(parser.isOpen())
       {
          parser.SetLineWriterFlags(ios::fixed, ios::floatfield);
          parser.SetLineWriterFlags(ios::showpoint);
          parser.SetLineWriterPrecision(3);
          //parser.SetLineWriterFlags(ios::right);
          Matrix mat(s, s);
          mat.SetIdentity();
          for(int i = 0; i < s; ++i)
          {
             for(int j = 0; j < s; ++j)
             {
                mat(i, j) = (i * j) - (s*s)/2;
                string o = "value =";
                parser.WriteW(o, 12, ios::left);
                parser.WriteW(mat(i, j), 8, ios::right);
             }
             parser.WriteLine("");
          }
          parser.Close();
       }
    
       return(0);
    }
    So if anyone has an idea of what I might be doing wrong I would appreciate some feedback in correcting the alignment issue.

    Thanks!
  • meLlamanJefe
    New Member
    • Mar 2008
    • 29

    #2
    Ah, love it when I answer my own questions.... The answer by the way is buried within this page:



    many thanks to the author for pointing out one more of the non-intuitive "features" of the stl.....

    Comment

    Working...