Go Back   Forums > Community Chatterbox > Tech Corner
Memberlist Forum Rules Search Today's Posts Mark Forums Read
Search Forums:
Click here to use Advanced Search

Reply
 
Thread Tools Display Modes
Old 04-09-2013, 07:02 AM   #21
Japo
Autonomous human
 
Japo's Avatar


 
Join Date: Mar 2006
Location: ,
Posts: 4,565
Default

wchar_t is defined even in C89, but you have to include <stdlib.h> or <stddef.h>.

http://en.wikipedia.org/wiki/Wide_character#C.2FC.2B.2B
Quote:
The wide characters are defined using datatype wchar_t, which in the original C90 standard was defined as
"an integral type whose range of values can represent distinct codes for all members of the largest extended character set specified among the supported locales" (ISO 9899:1990 4.1.5)
I've even tried it in TurboC++ 1.01 for DOS (not the Unicode program above, only one with a wchar_t instead of char, and literals preceded with L. Of course the Unicode part can be platform dependent, but I think nowadays it'll be standard.

Why a teacher would make someone who's starting to program deal with this, is beyond me...
__________________
Life starts every day anew. Prospects not so good...

Last edited by Japo; 04-09-2013 at 07:20 AM.
Japo is offline                         Send a private message to Japo
Reply With Quote
Old 05-09-2013, 09:02 PM   #22
jonh_sabugs
Abandonia nerd

 
Join Date: May 2010
Location: Brazil
Posts: 91
Default

You are right, I stand corrected.
jonh_sabugs is offline                         Send a private message to jonh_sabugs
Reply With Quote
Old 05-09-2013, 10:29 PM   #23
Japo
Autonomous human
 
Japo's Avatar


 
Join Date: Mar 2006
Location: ,
Posts: 4,565
Default

Yep I had no idea myself, I just researched it on the go, so I was learning too. I've tried enabling the "compile as C" (as opposed to C++) in Visual Studio and this code works, I think it should be portable to any Unicode-enabled platform:

Code:
#include <stdio.h>
#include <stddef.h>
#include <io.h>
#include <fcntl.h>

int main(void)
{	
	_setmode(_fileno(stdout), _O_U16TEXT);
	wprintf(L"Hell\u00f8 wrld!\n");
	return 0;
}
__________________
Life starts every day anew. Prospects not so good...
Japo is offline                         Send a private message to Japo
Reply With Quote
Old 05-09-2013, 11:14 PM   #24
jonh_sabugs
Abandonia nerd

 
Join Date: May 2010
Location: Brazil
Posts: 91
Default

Well, we are always learning. I researched a bit more and it seems wchar_t has no defined size, and could be only 8 bits wide, so it doesn't look portable.
jonh_sabugs is offline                         Send a private message to jonh_sabugs
Reply With Quote
Old 05-09-2013, 11:35 PM   #25
Japo
Autonomous human
 
Japo's Avatar


 
Join Date: Mar 2006
Location: ,
Posts: 4,565
Default

As portable as it gets in this case, I think. In practice I think in most (all non embedded) implementations wchar_t will be wider than 8-bit, but the bigger risk is no Unicode support (probably compile time error that _O_U16TEXT isn't defined, like I get in TC++ 1.01). That's why I like C# or D in which regular strings are always already Unicode. Also standards are as useful as they are but not more (ask SQL...)
__________________
Life starts every day anew. Prospects not so good...

Last edited by Japo; 06-09-2013 at 12:04 AM.
Japo is offline                         Send a private message to Japo
Reply With Quote
Old 02-07-2014, 09:45 PM   #26
Mighty Midget
Pox Vobiscum
 
Mighty Midget's Avatar


 
Join Date: Mar 2006
Location: Krakeroy, Norway
Posts: 3,014
Default

I need to ask again:

This time, I'm trying to write a code (C) that will generate prime numbers. Thing is, all the sample code I've seen checks each number up against all other, lower, natural numbers but the point is, that is not necessary and in fact will save me a LOT of time as the primes get larger if I only check up against the lower primes I generate.

The idea is to write the primes to a txt file (since 10,000,000,000 primes can take up a lot of memory), and read this list to check the candidate number.

Problem is: I use fscanf and it really isn't playing ball. Either it reads the entire list as one number or it stops reading as soon as it hits a space or new line.

Is there a neat way of reading each number in the list while allowing for space and new line?

Also, is there a way to jump straight to the last number in the list instead of having to go through the entire nightmare everytime I want to resume the job?

The idea here is, when I want to resume the job after a break, I want the program to pick the last prime found + 1 as the new candidate.
__________________
Je Suis Charlie

Last edited by Mighty Midget; 02-07-2014 at 09:49 PM.
Mighty Midget is offline                         Send a private message to Mighty Midget
Reply With Quote
Old 05-07-2014, 02:04 AM   #27
Japo
Autonomous human
 
Japo's Avatar


 
Join Date: Mar 2006
Location: ,
Posts: 4,565
Default

The problem is that if you don't want to read the whole file in one go, you need the FILE pointer to be moved from the start of the file to where you finished the last read.

Use fread(), it moves the pointer automatically, and allows you to read any number of numbers in each call. I think binary mode will be better than text.

http://msdn.microsoft.com/en-us/library/kt0etdcs.aspx
Quote:
The file pointer associated with stream (if there is one) is increased by the number of bytes actually read.
__________________
Life starts every day anew. Prospects not so good...
Japo is offline                         Send a private message to Japo
Reply With Quote
Reply


Similar Threads
Thread Thread Starter Forum Replies Last Post
EoF Corner Eagle of Fire Offers 6 03-02-2010 06:01 PM
The end of the file corner Eagle of Fire Music, Art, Movies 8 08-12-2009 01:09 AM
Spoonman's Art Corner Spoonman Music, Art, Movies 122 15-01-2009 10:35 PM
Spoonman's Music Corner Spoonman Music, Art, Movies 3 25-06-2005 06:18 PM

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump
 


The current time is 11:25 AM (GMT)

 
Powered by vBulletin® Version 3.7.1
Copyright ©2000 - 2021, Jelsoft Enterprises Ltd.