Guidance
指路人
g.yi.org
Guidance Forums / wxWidgets (wxWindows) in C++ / setfill and setw used with TextOutputStream

Register 
注册
Search 搜索
首页 
Home Home
Software
Upload

  
Forum List • Thread List • Refresh • New Topic • Search • Previous • Next First 1 Last
Message1. setfill and setw used with TextOutputStream
#3265
Posted by: econder 2004-01-24 01:02:21
Hello everyone,

I am using a TextOutputStream to output sequential numbers for a number generator I am building. The problem is that I want to use fixed-length fields on the output. However, when I try to use 'setfill' and 'setw', I get errors. Can anybody show me what I'm doing wrong?

Thanks in advance!

My Code:
int start_number = atoi(txtStartNumber->GetValue());
    int qty = atoi(txtQty->GetValue());
    const wxString *output_filename = new wxString(txtOutputFile->GetValue());
    int field_width = strlen(itoa(qty));
    
    wxFFileOutputStream output_file_stream( *output_filename );
    wxTextOutputStream data_output_stream( output_file_stream );
    
    for(int i = (start_number - 1); i < qty; i++) {
        srand(i+1);
        data_output_stream << i+1 << 'n';
    }


Post Edited (01-24-04 01:09)
Message2. Re: setfill and setw used with TextOutputStream
#3275
Posted by: upCASE 2004-01-27 16:58:57
Hi!
By now (version 2.4.2) wxWindows doesn't make full use of C++ streams. That's why I guess setw() won't work. With 2.6.0 or even with the CVS version that may change (can't comment on that).

Maybe try it with wxString::Format(). It's not that handy but should work.

upCASE
-----------------------------------
If it was hard to write, it should be hard to read!- Do. Or do not. There is no try!
Message3. Re: setfill and setw used with TextOutputStream
#3287
Posted by: econder 2004-01-31 01:22:48
Could you give me an example of how I would go about using the String::Format function?

Here is the code I have right now. It's just a basic sequential number generator that pulls the starting number, qty of numbers, and output filename from a text boxes.

void MyFrame::OnBtnStart(wxCommandEvent & event) {
    //--- use atoi() function to convert string to int. ---//
    int start_number = atoi(txtStartNumber->GetValue());
    int qty = atoi(txtQty->GetValue());
    const wxString *output_filename = new wxString(txtOutputFile->GetValue());
    
    //--- process sequential # and write data to file ---//
    wxFFileOutputStream output_file_stream( *output_filename );
    wxTextOutputStream data_output_stream( output_file_stream );
    for(int i = (start_number - 1); i < qty; i++) {
        srand(i+1);
        data_output_stream << i+1 << 'n';
    }
    wxMessageDialog dlgFinished(this, _("Number generation complete."), _("Finished"), wxICON_INFORMATION|wxOK, wxDefaultPosition);
    dlgFinished.ShowModal();
} // End MyFrame::OnBtnStart
Message4. Re: setfill and setw used with TextOutputStream
#3296
Posted by: upCASE 2004-02-01 03:16:57
Hi!
Although I don't really now what you're trying to do with your code (no random numbers are written to the output stream, just the random seed it set), I guess Format should work.
Basicly it work like printf() with it's formats. Use it like
dataOutputSteam << wxString::Format("%012.2lf", doubleVariable);
That way doubleVariable is formated so that it has a size of 12 chars max, with a precision of 2 and the empty spaces are filled up with '0'.
For Format("%012.2fn",15.7845); the output would look like 000000015.78

upCASE
-----------------------------------
If it was hard to write, it should be hard to read!- Do. Or do not. There is no try!
Forum List • Thread List • Refresh • New Topic • Search • Previous • Next First 1 Last
掌柜推荐
 
 
 
 
 
 
 
 
 
 
 
 
© Thu 2024-11-28  Guidance Laboratory Inc.
Email:webmaster1g.yi.org Hits:0