Wide Character Writing

This is a discussion on Wide Character Writing within the C++ Programming forums, part of the General Programming Boards category; I'm used to serializing data to disk using narrow character streams. However, now I need to serialize data using a ...

  1. #1
    Anti-Poster
    Join Date
    Feb 2002
    Posts
    1,399

    Wide Character Writing

    I'm used to serializing data to disk using narrow character streams. However, now I need to serialize data using a wide character stream.
    Code:
    #include <iostream>
    #include <string>
    
    using std::cout;
    using std::endl;
    
    enum
    {
        ver1 = 0xE0000001,
        ver2 = 0xE0000003,
        ver3 = 0xE0000004,
        ver4 = 0xE0000001,
        ver5 = 0xE0000003,
        ver6 = 0xE0000001
    };
    
    int main()
    {
    	std::wofstream outfile("awesome.txt");
    	
    	long ulVersion[6];
    	ulVersion[0] = ver1;
    	ulVersion[1] = ver2;
    	ulVersion[2] = ver3;
    	ulVersion[3] = ver4;
    	ulVersion[4] = ver5;
    	ulVersion[5] = ver6;
    	for(int i = 0; i < 6; i++)
    		cout << ulVersion[i] << endl;
    	outfile.write((wchar_t*)ulVersion,sizeof(long)*6);
    	if(!outfile)
    		cout << "Problem?" << endl;
    	outfile.close();
    
    	cout << endl;
    
    	std::wifstream infile("awesome.txt");
    	infile.read((wchar_t*)ulVersion,sizeof(long)*6);
    	for(int i = 0; i < 6; i++)
    		cout << ulVersion[i] << endl;
    	if(!infile)
    		cout << "Problem?" << endl;
    	infile.close();
    }
    When using narrow character streams, the lines in bold can be written as:
    Code:
    outfile.write((char*)ulVersion,sizeof(long)*6);
    infile.read((char*)ulVersion,sizeof(long)*6);
    I'm not sure what changes I need to make in wide character world; I haven't found any good references. Obviously, the streams are in a bad state after the read/write operation. I'm not sure why, though.
    If I did your homework for you, then you might pass your class without learning how to write a program like this. Then you might graduate and get your degree without learning how to write a program like this. You might become a professional programmer without knowing how to write a program like this. Someday you might work on a project with me without knowing how to write a program like this. Then I would have to do you serious bodily harm. - Jack Klein

  2. #2
    Registered User
    Join Date
    Nov 2002
    Posts
    491
    YOu arn't serializign data though, you are simply writing it as a stream of bytes. And technically, not even bytes, as wchar's. Are you sure you want to do this.

  3. #3
    Anti-Poster
    Join Date
    Feb 2002
    Posts
    1,399
    Yes, I'm sure I want to do this. Like I said, it worked with narrow streams.
    If I did your homework for you, then you might pass your class without learning how to write a program like this. Then you might graduate and get your degree without learning how to write a program like this. You might become a professional programmer without knowing how to write a program like this. Someday you might work on a project with me without knowing how to write a program like this. Then I would have to do you serious bodily harm. - Jack Klein

  4. #4
    Registered User
    Join Date
    Nov 2002
    Posts
    491
    Why are you attempting to write the data out as wchar_t's though? It does not make much sense.

  5. #5
    Anti-Poster
    Join Date
    Feb 2002
    Posts
    1,399
    Well, I'm open to analyzing that. The text data is stored in wstrings. It's a simple matter to serialize a wstring with wide streams. I suppose I could go back to using byte streams and converting wstrings to strings before writing.

    However, I'd still like to know how I'm tripping the fail bit in my code.
    If I did your homework for you, then you might pass your class without learning how to write a program like this. Then you might graduate and get your degree without learning how to write a program like this. You might become a professional programmer without knowing how to write a program like this. Someday you might work on a project with me without knowing how to write a program like this. Then I would have to do you serious bodily harm. - Jack Klein

  6. #6
    Registered User
    Join Date
    Nov 2002
    Posts
    491
    What text data are you talking about? And casting isn't serialization what you are doing, at best, is implementation defined behavior. Also, does a wide character write/read want to know how many *bytes* to read and write or how many *wchar_t*'s to read and write? If it's the first, how does it handle when the number of bytes is not aligned to the size of a wchar_t?

  7. #7
    Anti-Poster
    Join Date
    Feb 2002
    Posts
    1,399
    orbitz, the above code snippet is not a complete project, but rather something that demonstrates the point of my question. Further, writing text data out is a simple matter since they are already wchar_t's. Writing numeric data out to a wide stream is a bit more difficult. I see that I have failed to answer some of your questions.
    Quote Originally Posted by orbitz
    Why are you attempting to write the data out as wchar_t's though?
    Because the write method for the wofstream takes a wchar_t* as a parameter to the buffer to write. I haven't found a way write char* data to a wofstream.
    Quote Originally Posted by orbitz
    Also, does a wide character write/read want to know how many *bytes* to read and write or how many *wchar_t*'s to read and write?
    I don't know. As I said earlier, I haven't found any good documentation on wide streams. I suspect the second, but even accounting for that in the count parameter still led to fail bits in the wide streams.
    Quote Originally Posted by orbitz
    If it's the first, how does it handle when the number of bytes is not aligned to the size of a wchar_t?
    I don't particularly care about the implementation of the wide streams.
    Quote Originally Posted by orbitz
    And casting isn't serialization what you are doing, at best, is implementation defined behavior.
    In the narrow stream world, I think it's fine. Consider my original code example with narrow streams. Then suggest to me a better way to serialize a series of numbers.
    If I did your homework for you, then you might pass your class without learning how to write a program like this. Then you might graduate and get your degree without learning how to write a program like this. You might become a professional programmer without knowing how to write a program like this. Someday you might work on a project with me without knowing how to write a program like this. Then I would have to do you serious bodily harm. - Jack Klein

  8. #8
    Registered User
    Join Date
    Nov 2002
    Posts
    491


    Quote:
    Originally Posted by orbitz
    If it's the first, how does it handle when the number of bytes is not aligned to the size of a wchar_t?
    I don't particularly care about the implementation of the wide streams.
    You should since it directly affects your code. You are trying to output a series of longs but what if the number of bytes you are trying to output (the size of your longs) is not aligned to the size of the wchar_t's? Is it going to output undefined amount of data or what? And what around reading it back in? Will it read in more data than your longs? In which case it's going to go out of bounds in your array and now you've commited undefined behavior.
    Why do you have to output your other data to the same wide stream as your wchar_t's?
    A better way to serialize numbers is to convert them to some intermediate format for writing. This can be making 123 a string "123" and writing that. It depends. The C++ FAQ LITE has an entire section on serialization, you should probably read it.
    Casting does not qualify quite qualify as serialization mostly because it does not take into account any implementation defined features of your types. Do you plan on reading this data back in soley on that same machine? If so then I would not quite call that serliaztion since that term generally refers to making data exportable to other machines. However even if you just want to read the data back on that same machine, if you switch compilers the data may or may not still be readable to you due to changes in the compiler.
    You also might consider this. If your form of serialization 'works' for you with narrow streams, why don't youjust 'serialize' your wchar_t's via casting to char* and output it in yoru narrow stream?

  9. #9
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,701
    A wofstream does not put wide characters into files. All wchar_t's are narrow'd before going into the file. That goes for both formatted and unformatted output. This means that you can't use wofstream to write binary data, unless you do some fancy stream programming.

    gg

  10. #10
    Anti-Poster
    Join Date
    Feb 2002
    Posts
    1,399
    Aye, I went back to using narrow streams. It's simpler that way, and converting from a wstring was easier than I first thought.
    If I did your homework for you, then you might pass your class without learning how to write a program like this. Then you might graduate and get your degree without learning how to write a program like this. You might become a professional programmer without knowing how to write a program like this. Someday you might work on a project with me without knowing how to write a program like this. Then I would have to do you serious bodily harm. - Jack Klein

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. sequential file program
    By needhelpbad in forum C Programming
    Replies: 80
    Last Post: 06-08-2008, 02:04 PM
  2. Interpreter.c
    By moussa in forum C Programming
    Replies: 4
    Last Post: 05-28-2008, 06:59 PM
  3. Remove character from string
    By nooksooncau in forum C Programming
    Replies: 11
    Last Post: 06-05-2006, 10:37 AM
  4. about wide character and multiple byte character
    By George2 in forum C Programming
    Replies: 3
    Last Post: 05-22-2006, 09:11 PM
  5. pipe
    By smart girl in forum C Programming
    Replies: 4
    Last Post: 04-30-2006, 10:17 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21