C and its simpleton I/O functions

Programming, for all ages and all languages.
Post Reply
User avatar
bubach
Member
Member
Posts: 1223
Joined: Sat Oct 23, 2004 11:00 pm
Location: Sweden
Contact:

C and its simpleton I/O functions

Post by bubach »

I did manage to avoid scanf() in a tiny mastermind game for school.. I prefered using getch(); so that I would be sure that the user didn't type anything else then five unique numbers.. :)
This school project led me to the conclusion that most C functions suck, and you can't even clear the screen in a standard way.. ;D
"Simplicity is the ultimate sophistication."
http://bos.asmhackers.net/ - GitHub
User avatar
Solar
Member
Member
Posts: 7615
Joined: Thu Nov 16, 2006 12:01 pm
Location: Germany
Contact:

Re:printing integers to screen without using an array

Post by Solar »

bubach wrote: This school project led me to the conclusion that most C functions suck...
About half of the standard functions are sub-par, I agree with that.
...and you can't even clear the screen in a standard way.. ;D
The eternal struggle between the concepts of language vs. library. Don't compare C (which has a real bare-bones library) with Perl / CPAN or Java. I think C/C++ did it the right way when they left all "fancy" stuff to third-party libraries. After all, how would you define "screen" in a portable way? ;)
Every good solution is obvious once you've found it.
User avatar
bubach
Member
Member
Posts: 1223
Joined: Sat Oct 23, 2004 11:00 pm
Location: Sweden
Contact:

Re:printing integers to screen without using an array

Post by bubach »

Solar wrote:After all, how would you define "screen" in a portable way? ;)
If you can write to the screen you should be able to do other things with it too, like clearing it or changing cursor position.
"Simplicity is the ultimate sophistication."
http://bos.asmhackers.net/ - GitHub
User avatar
Solar
Member
Member
Posts: 7615
Joined: Thu Nov 16, 2006 12:01 pm
Location: Germany
Contact:

Re:printing integers to screen without using an array

Post by Solar »

Standard C doesn't know about "screens". You cannot write to a "screen" with standard C, so it's natural you cannot clear it or change cursor position. The textual I/O primitive of standard C is the line - which might be in a file or via some other, potentially interactive device.

Everything beyond that is up to some external library. It might be portable or making full use of a specific platform. It might be geared for security, or efficiency, or good looks. It might be proprietary and coming with professional support, or it might be GPL.

This allows you to take your pick, instead of being shackled to how the language designers thought things ought to be done. I still consider this the biggest "pro" of C/C++, as opposed to e.g. Java.
Every good solution is obvious once you've found it.
User avatar
bubach
Member
Member
Posts: 1223
Joined: Sat Oct 23, 2004 11:00 pm
Location: Sweden
Contact:

Re:printing integers to screen without using an array

Post by bubach »

You can change position inside a file, right? So why not on the screen, if it's treated like a file? Doing for-loops with printf("\b"); gets ugly pretty fast.. :)
Anyway, this is pretty pointless and very OT.
"Simplicity is the ultimate sophistication."
http://bos.asmhackers.net/ - GitHub
User avatar
Solar
Member
Member
Posts: 7615
Joined: Thu Nov 16, 2006 12:01 pm
Location: Germany
Contact:

Re:printing integers to screen without using an array

Post by Solar »

bubach wrote: You can change position inside a file, right? So why not on the screen, if it's treated like a file?
It is not treated like a file. Because you cannot position on it. ;-)
Doing for-loops with printf("\b"); gets ugly pretty fast.. :)
Yes, because it's using the wrong tool for the right thing. Take (or write) a library that does the extended terminal stuff. You don't do for-loops of putchar() for printing lines, either. ;)
Anyway, this is pretty pointless and very OT.
Pointless - I don't think so. ;-) And the OT thing can be helped. ;-)
Every good solution is obvious once you've found it.
zloba

Re:C and its simpleton I/O functions

Post by zloba »

if you're on windows, you can use Console API.
on Unix, you can try Ncurses (which is a wrapper on top of a complete anarchy of terminal standards and their escape sequences, and the lack of an API).

i think there ought to be some general (cross-platform) standard for a Console API, but not as part of any language standard.
User avatar
Pype.Clicker
Member
Member
Posts: 5964
Joined: Wed Oct 18, 2006 2:31 am
Location: In a galaxy, far, far away
Contact:

Re:C and its simpleton I/O functions

Post by Pype.Clicker »

iirc, there should be ANSI escape codes for clearing, changing colors and position. They are (afaik) supported on Unix terminals, DOS command lines (with the proper ANSI.SYS driver) and probably windows aswel.
Post Reply