LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 02-05-2003, 06:33 AM   #1
aditya
Member
 
Registered: Aug 2002
Distribution: RH 7.x
Posts: 71

Rep: Reputation: 15
Unhappy C : Can't type right arrow


Hi,
I got this funny problem. I've written this code:

printf("%c",26);

26 is the ascii code for the right arrow key, i.e., '->' in its smaller form you can say.
But on running the above line I get nothing at all on the screen .
I'm able to type the left, up and down keys with ascii values something between 24 and 27.

And guess what??, whenever I use fgetc function to read that right arrow from a data file, the return value of this function is -1 instead of 26.

Could anybody enlighten me with his guidance.

Thanks in advance.
 
Old 02-05-2003, 07:08 AM   #2
squeeze
LQ Newbie
 
Registered: Dec 2002
Distribution: red hat 7.x
Posts: 6

Rep: Reputation: 0
not so funny... ALL of the ascii characters with code number under 32 are control characters... ;-)
chr 26 is the traditional End Of File character... are you working under linux?? This seems a file opened in text mode under dos/windows... as Linux doesn't make any difference between binary and text files... :-) Try to open the file in binary mode ("wb" for writing an "rb" for reading..).

Bye.
 
Old 02-06-2003, 12:38 AM   #3
aditya
Member
 
Registered: Aug 2002
Distribution: RH 7.x
Posts: 71

Original Poster
Rep: Reputation: 15
Smile

Great!!!

It worked. What a lightening fast reply it was. WOW. But I really don't know how. Please also clarify the following problems:

1) What difference does it make in the return value of fgetc function if the file is opened in ascii and in binary.

2) I thought '/0' with ascii value 0 is used to denote EOF, because it's used to denote end of arrsys and strings. Pleae elaborate on the ascii value 26 being used as EOF in files. ;-)

3) I made a blank file. And ran the following code on it:

while(!feof(file))
{
int ascii = fgetc(file);
printf("%c",ascii);
}

I expected the program not to print anything because it's a blank file. But it prints a "/" on the screen. Why? And what should I do to avoid it.

4) How do I printf the right arrow using it's ascii value 26. ;-))
 
Old 02-06-2003, 06:54 PM   #4
squeeze
LQ Newbie
 
Registered: Dec 2002
Distribution: red hat 7.x
Posts: 6

Rep: Reputation: 0
Please, I don't want to be unfriendly, but I have Just ONE question... are you sure that you are using gcc under linux??.. ;-) I'm not a moderator at all anyway this is not a C forum.. this is for linux and u are making some c-only questions... I think you'd better read the C FAQ before (for example at url: http://www.eskimo.com/~scs/C-faq/top.html ) And/Or buy the K&R 2nd edition... And/Or read some newsgroups' archives like the one from comp.lang.c

Please, forgive me Moderator.
For the last time (about this argument):

1) Under linux? None. Under some other os? CR (\r)+LF(\n) = LF(\n) (reading) and LF=CR+LF (writing). \x1a = means EOF (but usually u can keep on reading after this kind of EOF).

2) READ AT LEAST K&R ... C Programming Language, it DOES worth the reading. '\0' is used as a "flag" to terminate arrays (null-terminated strings are only arrays).

3) feof is set only after the first reading that reaches the end of file condition... try this kind of code:

void function()
{
FILE *fp=fopen("yourfile","rb"); /*just to be sure*/
int c;
while((c=fgetc(fp))!= EOF) {
printf("%c", c);
}
fclose(fp);
}

4) U don't .. and u have to tell WERE you want to print it.. On a text only-terminal, in a X-window application, etc. If u are using text (under linux) I suppose that you have to understand something like ncurses. BTW under c (use #include <ctype.h> if i'm not wrong) there is a function called isprint(c) which tells you what is printable and what is not printable..

Addio (for this argument).
 
Old 02-15-2003, 08:45 PM   #5
Wondre
LQ Newbie
 
Registered: Jan 2003
Location: SoCal
Distribution: SlackWare 10
Posts: 17

Rep: Reputation: 0
The note on this forum says questions don't have to be directly related to Linux. It's good to try to keep the rules, though.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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 Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Shortcut arrow thort Mandriva 0 11-04-2004 04:28 PM
VIM and the arrow keys Buto Linux - Software 1 09-16-2004 08:38 PM
Arrow keys in nethack Levitate Linux - Games 3 08-22-2004 12:44 PM
Arrow Key mapping lazlom AIX 5 06-21-2004 04:32 PM
arrow symbols (<->) with latex? realos Linux - Software 2 08-25-2003 06:19 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 05:37 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration