wchar_t file I/O [was: please review the code...]


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 10 of 10

Thread: wchar_t file I/O [was: please review the code...]

  1. #1
    Join Date
    May 2005
    Posts
    58

    Question wchar_t file I/O [was: please review the code...]

    please review my code
    i want to get individual parameters and then write in the file "abc"

    i am sending the three pointer to the function writelog
    *starttime is pointing to start time it is of the form
    12.10.23.100(hr.min.sec.milisec)

    similarly endtime is pointing towards end time

    and cmdname is pointing to the name.

    function writelog(wchar_t* cmdname, wchar_t*starttime,wchar_t*endtime)
    {
    min = wstrtok(starttime + 1,L'.');
    sec = wstrtok(null,L'.');
    milisec = wstrtok(null,L'.');

    logstream = Xnew ofstream("abc.log",log:append)

    abc<<"min"<<min<<"sec"<<sec<<"milisec"<<milisec
    abc<<"componenetname"<<"\t"<<cmdname<<endl;
    abc<<"starttime"<<"\t"<<starttime<<endl;
    abc<<"endtime"<<"\t"<<endtime<<endl;

    }

    this program is right syntactically as well as logically?

    2>
    int stratmin = _wtoi( const wchar_t min );
    will thi statement convert wchar_t type min to int type min?

  2. #2
    Join Date
    Nov 2003
    Posts
    4,118
    The code isn't correct because you have to use the wide file stream objects: wofstream, wifstrean etc, when you write wchar_t characters to them.
    Danny Kalev

  3. #3
    Join Date
    May 2005
    Posts
    58
    i have done few modification please check the syntax as well as logic ..will it work?

    void writelog(wchar_t* cmdname, wchar_t*starttime,wchar_t*endtime)
    {
    wchar_t *min = wstrtok(starttime + 1,L'.');
    wchar_t *sec = wstrtok(null,L'.');
    wchar_t * milisec = wstrtok(null,L'.');
    // will above statements separate out min , sec and milisec from the string?

    here L specifier works with '.' .i have read that on one website.
    well my wchar_t (string ) is like 11.12.23.100(hr.min.sec.milisec)
    now strattime is pointing to this string
    i want the pointer to each component viz min sec mili
    so above i am using wstrtok.if nul doesnt work then what should i use to get the refrence?



    wofstream a_file ( "test.txt", ios::app );

    a_file<<"componenetname"<<"\t"<<cmdname<<endl;
    a_file<<"starttime"<<"\t"<<starttime<<endl;
    a_file<<"endtime"<<"\t"<<endtime<<endl;
    }
    will this work now?i want to write the wchar_t (string) to the file test.txt

  4. #4
    Join Date
    Nov 2003
    Posts
    4,118
    You're almost there. However, the following statement and those that follow it are wrong:

    Code:
    a_file<<"componenetname"<<"\t"<<cmdname<<endl;
    You're using a quoted char string instead of a wchar_t string:

    a_file<< L"componenetname" << L"\t" <<cmdname<<endl;

    I wouldn't use endl either, I think should work properply but you can use a more explicit delimter such as L'\n' or L','.

    Of course, the easiest way to confirm that everything's working correctly is running the program and then testing the files (you want to write another routine that opens that file and reads the data therein, just to make sure that it contains the correct data).
    Last edited by Danny; 07-06-2005 at 08:18 AM.
    Danny Kalev

  5. #5
    Join Date
    May 2005
    Posts
    58
    Thanx a lot .....

    swapnil.

  6. #6
    Join Date
    May 2005
    Posts
    58

    Question error while compiling

    hi

    when i started compiling the program
    wofstream a_file ( "test.txt", ios::app );

    a_file<< L"componenetname" << L"\t" <<cmdname<<L'\n' ;
    a_file<<L"componenetname"<<L"\t"<<starttime<<L'\n' ;
    a_file<<L"starttime"<<L"\t"<<endtime<<L'\n' ;


    i got the error :a_file was not defined .

    now how to construct the file and define it ....i guess error may be bcoz of i am not using constructor for wofstream and as well as for file......
    but i donno how to do that.....
    plezz help me by providing the syntax/code for the above error.

    thanx.
    swapnil.

  7. #7
    Join Date
    May 2005
    Posts
    58

    Question

    i think this can be solution for my above problem plezzz check this
    i want to write the data in file abc.txt


    static wostream *logstream;
    logstream = Xnew wofstream("abc.txt",ios::app);
    logstream<< L"componenetname" << L"\t" <<cmdname<<L'\n' ;
    logstream<<L"starttime"<<L"\t"<<starttime<<L'\n' ;
    logstream<<L"endtime"<<L"\t"<<endtime<<L'\n' ;

    here i am declaring logstream as pointer of type wostream

    then using logstream i am trying to append the data at the end of the file.

    the file abc.txt doesnt exist already ....willl wofstream constructor will create the file ?

    else need i write the code to create the file for first time?

    thank you?

  8. #8
    Join Date
    Nov 2003
    Posts
    4,118
    Did you #include <stream> in the program? And did you use a using declaration such as

    Code:
    using std::wofstream;
    ?
    Danny Kalev

  9. #9
    Join Date
    May 2005
    Posts
    58

    Question

    no i didnt use that std::wofstream...

  10. #10
    Join Date
    Nov 2003
    Posts
    4,118
    I meant <fstream>. You need to #include it and refer to std::wofstream to make this code compile.
    Danny Kalev

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center