Thread: Unicode - Some Example Code

  1. #1
    Chad Johnson
    Join Date
    May 2004
    Posts
    154

    Unicode - Some Example Code

    Can somebody give me some simple, basic example code that would write the first 5000 Unicode characters to a file? I'm using MingW (GNU GCC) and I absolutely refuse to learn Windows programming (not going to explain why), so it's gotta work with GCC.

    I tried this, and I think it's just writing ASCII:
    Code:
    #include <stdio.h>
    #include <tchar.h>
    
    int main()
    {
        wchar_t wc;
        FILE *fp;
    	
        fp = fopen("test.txt", "w");
    
        for (int i=0; i<5000; i++)
    	{
        	wc = i;
        	fputwc(wc, fp);
    	}
    
    	fclose(fp);
    	   
        return 0;
    }

  2. #2
    S Sang-drax's Avatar
    Join Date
    May 2002
    Location
    Göteborg, Sweden
    Posts
    2,072
    Quote Originally Posted by ChadJohnson
    Can somebody give me some simple, basic example code that would write the first 5000 Unicode characters to a file
    Here's a good guess:

    Code:
    int main()
    {
       wofstream file("out.txt");
       for (wchar_t ch = 0; ch < 5000; ++ch)
         file << ch;
    }
    Last edited by Sang-drax : Tomorrow at 02:21 AM. Reason: Time travelling

  3. #3
    Chad Johnson
    Join Date
    May 2004
    Posts
    154
    Thanks, but it's a no-go. First, it says wofstream is undeclared (I included iostream, fstream, and tchar). Second, the actual numbers 12345678910111213...) get written to the file.

  4. #4
    S Sang-drax's Avatar
    Join Date
    May 2002
    Location
    Göteborg, Sweden
    Posts
    2,072
    How can the program output anything at all if you get a compiler error?

    If you changed wofstream to ofstream then numbers will get written to the file. If your compiler doesn't have wofstream then there's nothing to do.

    The example compiles for me (with fstream) and I get an out.txt file.
    Last edited by Sang-drax : Tomorrow at 02:21 AM. Reason: Time travelling

  5. #5
    Chad Johnson
    Join Date
    May 2004
    Posts
    154
    haha...yea, I changed it to ofstream to make it compile.

    Are you using gcc (MingW)? With it, I get `wofstream' undeclared (first use this function). I'm including iostream, fstream, and tchar.
    Last edited by ChadJohnson; 02-28-2005 at 12:06 AM.

  6. #6
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    What version of gcc are you using? I had to upgrade mine to make wide streams work.

  7. #7
    Chad Johnson
    Join Date
    May 2004
    Posts
    154
    3.4.2

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. <string> to LPCSTR? Also, character encoding: UNICODE vs ?
    By Kurisu33 in forum C++ Programming
    Replies: 7
    Last Post: 10-09-2006, 12:48 AM
  2. Explain this C code in english
    By soadlink in forum C Programming
    Replies: 16
    Last Post: 08-31-2006, 12:48 AM
  3. Reading a file with Courier New characters
    By Noam in forum C Programming
    Replies: 3
    Last Post: 07-07-2006, 09:29 AM
  4. Updated sound engine code
    By VirtualAce in forum Game Programming
    Replies: 8
    Last Post: 11-18-2004, 12:38 PM
  5. Interface Question
    By smog890 in forum C Programming
    Replies: 11
    Last Post: 06-03-2002, 05:06 PM