25 November 2016, 18:09 | #1 |
Registered User
Join Date: Jun 2016
Location: Stoke-On-Trent, England
Posts: 450
|
Need Help - The C Programming Language 2nd ed K&R Book
I just bought the book and I have started to read through it.
For the time being I am using vbcc on WINUAE but it's setup the same as my 90mhz 060 A4000 (BetterWB). I will start compiling on my A4000 shortly but I am really scratching my head so soon into this book. I did the temp conversions really ok reversing the Fahrenheit to Celsius code ok and adding in headers to describe what the columns were. I thought I was an 31337 hacker hahaha I am now on section 1.5.1 File Copying I understand the concept that what I type into the shell is displayed. But I cant get my head around exercises: Exercise 1-6. Verify that the expression getchar () I= EOF is 0 or 1. Exercise 1-7. Write a program to print the value of EOF. The given code is: #include <stdio.h> I have had a google and I am not the only one confused, there is a post on Stackoverflow but that didn't help much either I know I must trigger an EOF but don't know how and would it display EOF or just exit the program? Thanks in advance for any help given. |
25 November 2016, 18:55 | #2 |
Registered User
Join Date: Dec 2010
Location: Athens/Greece
Age: 53
Posts: 719
|
Ok, let's start with the keyboard. "Ctrl \" I think is what sends EOF in Amiga Shell. "Ctrl d" is on linux. So the key sequence varies from OS to OS.
But you can redirect input from a file. Say you have a file foo.txt (make one, write a sentence in it and save it). Say your executable is called filecopy. Then you can do filecopy <foo.txt That will read the foo.txt and when the last byte has been read then the next call in getchar() will return EOF. Now, onto your last question. The snippet you posted will just exit when EOF is seen. EOF is really an integer. If you want to print it's value, try this: printf("EOF's value is %d\n", EOF); |
26 November 2016, 09:40 | #3 |
Registered User
Join Date: Jun 2016
Location: Stoke-On-Trent, England
Posts: 450
|
Thanks for the help.
I couldn't get filecopy to work kept getting: unknown identifier <filecopy> I wasn't far off printing EOF then I was missing the "%d" part. I noticed a few odd things when you type in special characters. If i typed in say "@blablabla" and pressed Return (minus the quotes) It would display: @blablabla @blablabla and the cursor would be hear. @ seems to repeat everything on a line. If I did "Ctrl 2" I would get an inverted @ and when pressing Return the cursor would drop down two lines. If I try "Ctrl 2" on my PC I either switch tab on a browser and If I try to do it in say notepad nothing is displayed. Last edited by kgc210; 26 November 2016 at 09:45. |
26 November 2016, 16:41 | #4 | |||
Natteravn
Join Date: Nov 2009
Location: Herford / Germany
Posts: 2,496
|
Quote:
Quote:
You have to know that getchar() reads characters from the file stdin, and stdin is usually the console, as long as you don't redirect it with "<" (see above). The console is not a normal file, like a file on disk. It is an interactive TTY, which is line-buffered. So your getchar() call is blocked until another line-buffer is complete, which happens after you pressed the Return key. Then it returns all typed characters from that line, one after another, until the '\n' Character (decimal 10 = Newline). Then it blocks again, waiting for the next line-buffer. From the two lines you are seeing the first line is the echo of your typed characters in the shell (your program is still blocked in the first getchar() call). The second line is actually generated by your program with getchar()/putchar(). Quote:
|
|||
26 November 2016, 18:06 | #5 |
Registered User
Join Date: Jun 2016
Location: Stoke-On-Trent, England
Posts: 450
|
I am sorry but I can't figure out how to get getchar() to read <foo.txt
I have tried it in the brackets out side them. Deleted the whole lot and just had <foo.txt Not sure if to carry on reading this book or look for a more simple "Retards guide to C" book |
26 November 2016, 18:40 | #6 |
Registered User
Join Date: Jul 2014
Location: Finland
Posts: 1,176
|
the stdin redirection suggestion is more a native unix thing, i'm not sure it actually works on AmigaOS' shell.
|
26 November 2016, 21:24 | #7 |
Registered User
Join Date: Dec 2010
Location: Athens/Greece
Age: 53
Posts: 719
|
|
26 November 2016, 21:25 | #8 |
Registered User
Join Date: Dec 2010
Location: Athens/Greece
Age: 53
Posts: 719
|
what's the name of your executable that is produced after you compile the example source code?
|
27 November 2016, 01:44 | #9 |
Registered User
Join Date: Jun 2016
Location: Stoke-On-Trent, England
Posts: 450
|
Right I get it now, I have to type the <foo.txt into the shell.
I thought I was trying to get "getchar()" to read it with in the program it's self. |
27 November 2016, 06:18 | #10 | |
Registered User
Join Date: Jun 2016
Location: Stoke-On-Trent, England
Posts: 450
|
Well I'm onto Exercise 1-8
Quote:
When I tried \s (also tried escape \e) I get the error "illegal escape-sequence in string" when trying to compile the code. Is this due to vbcc blocking \s or something Amiga specific? Anyway I changed \s to backspace \\ and got it to compile. #include <stdio.h> It counts newlines ok but if I press tab or backspace it doesn't record them. I also created a txt file with ed to have some new lines and tabs in, but again it didn't see them. I also tried to write \\, \t in the txt file. Is this due to the way Amiga handles tab and backspace? Or because ed can't encode them in a file? On the attached image you can see where I tried to press Tab in the shell, it displayed inverted I Thanks Will |
|
27 November 2016, 10:59 | #11 | |
Registered User
Join Date: Dec 2010
Location: Athens/Greece
Age: 53
Posts: 719
|
Ok, lets break it down.
Quote:
tabs are \t newlines are \n Backspace is not blank, so you should not count it. Even if you needed to count backspace you couldn't, because backspace doesn't leave a char behind, it just erases a char left from cursor. Also, the '\\' is not backspace is the char '\' (backslash) The backspace is '\b', which again you'll never be able to get. (Technicaly you could if you put the console in raw mode, but that's way advanced for this exercise) Bottom line, if you changed the if (c == '\\') to if (c == ' ') then I think you'll cover what the exercise asked you to do. Emm, just saw the screenshot. You forgot the braces in your while. Enclose all ifs in {} like Code:
while(...) { if... if... if... } |
|
27 November 2016, 11:21 | #12 | |
Natteravn
Join Date: Nov 2009
Location: Herford / Germany
Posts: 2,496
|
Quote:
https://en.wikipedia.org/wiki/Escape_sequences_in_C |
|
27 November 2016, 23:01 | #13 |
Registered User
Join Date: Jun 2016
Location: Stoke-On-Trent, England
Posts: 450
|
Thanks alkis and phx I owe you both a beer.
I have just rebuilt my WINUAE setup on the laptop, I forgot to copy the hard file before going on my travels to Rheda-Wiedenbruk I will give the coding a go tomorrow evening when I arrive, I have the C book in the car. I will keep you both posted. Do you think all this ASCII stuff and Escape sequences were better known when the book was written or do the authors assume too much with their strong UNIX background? |
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
Thread Tools | |
Similar Threads | ||||
Thread | Thread Starter | Forum | Replies | Last Post |
What programming language to write a commodity? | Sim085 | support.Other | 18 | 02 April 2015 23:56 |
WTB: Abacus Book - Amiga Machine Language | christopherpm | MarketPlace | 2 | 08 July 2013 22:07 |
Amiga C programming language | Kenan | support.Apps | 3 | 25 June 2013 18:50 |
Graphics Programming Black Book | Amiga Forever | Coders. Language | 2 | 20 April 2012 07:23 |
Most common programming language in Amiga games | manicx | Amiga scene | 38 | 10 March 2004 14:20 |
|
|