1 / 8

Effective Input Handling and Buffering in C Programming

This guide covers essential techniques for managing input and buffering in C programming. It explains how input is read from buffers, the behavior of end-of-file (EOF), and how to manage character input robustly. With practical examples, it shows how to ensure valid input, handle EOF conditions, and redirect standard input/output from files. Key functions like getchar() and scanf() are discussed, along with tips for maintaining program stability and providing users with opportunities to correct input errors.

Download Presentation

Effective Input Handling and Buffering in C Programming

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Validation

  2. Buffering • C input is read from a buffer • When the buffer is empty, • Characters are moved into it • For terminal, buffer is filled with a line • For a file, buffer is filled with a disk “block” • When no more data is available • EOF (end-of-file) is indicated

  3. getchar() • Removes a character from the buffer • Refilling the buffer, if necessary • Returns the character as an integer • With EOF indicating end-of-file

  4. Robust programming • Make sure the input is valid • If something is wrong, • Give another chance • Handle end-of-file • And missing files • Don’t bomb

  5. Skipping to the end of line • Getting to a new line • Provides a good starting point while((ch=getchar()) && ch != ’\n’) process character while((ch=getchar()) && ch != ’\n’ && ch != EOF) process character

  6. Testing input with scanf() • scanf returns the number of items read • Be sure that it does! if (scanf(”%d”, &x) != 1) something is wrong • It might be better to • Only read one item per scanf • Skip to the end-of-line for a fresh start • And give a second chance at input

  7. Redirection • Unix and DOS allows terminal I/O from files • cprog < file.in • Standard input read from file.in • cprog > file.out • Standard output written to file.out

  8. End-of-file on terminal • On Unix • Type a Ctrl+D • The ASCII EOT (end of transmission) • On DOS and Windows • Type a Ctrl+Z • Descends from pre-Unix DEC operating systems • Stored as the last character of a file • Not a character read by the program

More Related