-
fprintf vs fout
The following thread describes a couple ways to skin the cat.
http://cboard.cprogramming.com/showthread.php?t=21714
I started and got my program working using a simple logfile...
Code:
cout << "Log filename: ? ";
cin >> logName; // could put this in the main() and then echo to the logfile
ofstream fout(logName); // open for writing
fout << logName << endl << endl; //include date and time next
while(1) { fout << lots of data ...}
But now I want to align the columns of my output data, etc.
How do I go about getting the fprintf formatting in std:: ofstream?
I can't find the magic url with what I'm looking for.
Thanks
-
have a look at the iomanip header. you probably want std::setw and std::setfill.
I have to say that iostream formatting is pretty clumsy and verbose compared to printf (price you pay for type safety and extensibility, I guess)
-
-
I know C++ is significantly better than C in some areas. IMO, this isn't one of them. ;)
If I was really lazy or just wanted to get the project logic written out, I'd include <cstdio> and just use fprintf(), but that would require you to fopen() the file, so..... probably more work to switch them back and forth. Might as well just write it one way and leave it that way.
-
FYI - if you think of each line as a column, it helps in organizing code layout.
Code:
fout << dec << "Item: ";
fout.width(3); fout << left << j << " Counts: ";
fout.width(8); fout << data[j] << " Rate: ";
fout.width(8); fout << rate[j] << endl;
Dave.end