By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
432,414 Members | 1,057 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 432,414 IT Pros & Developers. It's quick & easy.

Trouble with ofstream

P: 29
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
Share this Question
Share on Google+
1 Reply


P: 29
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

Post your reply

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