Unformatted text preview:

IS 12 1 IS12 – Introduction to ProgrammingDate: April 2, 2003FUNCTIONS for char/string and File processingIS 12 2Functions getchar() and putchar()The getchar() function gets a single character from the keyboard. getchar() buffer is not released until enter key is pressed. Use an extra getchar() to get unwanted enter keys as inprintf(“What are your three intitals?\n”);firstInit = getchar();newLine1 = getchar();midInit = getchar();newLine2 = getchar();lastInit = getchar();newLine3 = getchar();You can also code it asprintf(“What are your two intials?\n”);firstInit = getchar();getchar();midInit = getchar();getchar();lastInit = getchar();getchar();putchar() puts a single character to the screen.The getchar() and putchar() functions require that #include stdio.h be included in the program. Functions gets() and puts()The gets() function reads data from the keyboard until the user presses the enter key (the newline key) or EOF is encountered. The newline character is not stored in the array but a null character is appended to the array as a terminator.The puts() function writes a string to the standard output. puts() adds a newline to the screen when the output is display.The gets() and the puts() function require that #include <stdio.h> header file to be included in the program.IS 12 3Example Programs#include <stdio.h>#define MAXCHARS 81void main(void){ char message[MAXCHARS]; printf("Enter a string:\n"); gets(message); printf("The string just entered is:\n"); puts(message);}Sample program using a user-defined copy function#include <stdio.h>#define MAXCHARS 81void main(void){ char message[MAXCHARS]; char new_mess[MAXCHARS]; int i; void strcopy(char [], char []); printf("Enter a sentence: "); gets(message); strcopy(new_mess,message); puts(new_mess);}void strcopy(char string1[], char string2[]) { int i = 0; while (string2[i] != '\0') { string1[i] = string2[i]; i++; } string1[i] = '\0'; return;}IS 12 4Sample Program to Count Characters#include <stdio.h>#define MAXNUM 1000void main(void){ char message[MAXNUM]; int numchar; int countchar(char []); printf("\nType in any number of characters: "); gets(message); numchar = countchar(message); printf("The number of characters just entered is %d\n", numchar); }int countchar(char list[]){ int i, count = 0; for(i = 0; list[i] != '\0'; i++) count++; return(count);}Sample Program to Count Words#include <stdio.h>#define MAXNUM 1000void main(void){ char message[MAXNUM]; int numchar; int countword(char []); printf("\nType in any number of words: "); gets(message); numchar = countword(message); printf("The number of words just entered is %d\n", numchar); }int countword(char list[])#define YES 1#define NO 0{ int i, inaword, count = 0; inaword = NO; for(i = 0; list[i] != '\0'; i++) {IS 12 5 if (list[i] == ' ') inaword = NO; else if (inaword == NO) { inaword = YES; count++; } } return(count);}Sample Program showing pointer changing#include <stdio.h>#define NUMSEASONS 4void main(void){ int n; char *seasons[] = {"Winter", "Spring", "Summer", "Fall"}; for( n = 0; n < NUMSEASONS; n++) printf("\nThe season is %s.",seasons[n]); printf("\n");}Sample program to determine if a character is alphabetic, numeric, or other#include <stdio.h>#include <ctype.h>void main(void){ char character; printf("Type character for analysis:"); character = getchar(); if (isalpha(character)) printf("That character is alphabetic.\n"); else if (isdigit(character)) printf("That character is numeric.\n"); else printf("That character is not alphanumeric.\n");}IS 12 6Sample program to count letters in a word#include <stdio.h>#include <ctype.h>void main(void){ char NextChar; int LetterCount; do { NextChar = getchar(); } while (!isalpha(NextChar)); LetterCount = 0; do { LetterCount++; NextChar = getchar(); } while (isalpha(NextChar)); printf("No. of letters: %i\n", LetterCount);}Data Files Input and OutputWith opening a file, it returns a pointer to a FILE structure which is defined in stdio.h We must open and closes files that we are going to work with. We can alos indicate the way that the file is going to be opend by strings of modes, including, r (reading), w (writing), a (appending), r+ (open file for reading or writing), w+ (creates a file for reading or writing) a+ (opens a file for appending), rb (opens a binary file for reading),wb(creates a binary file for writing), ab (opens a binary file for appending),r+b (opens a binary file for reading and writing), w+b (creates a binary for reading or writing), a+b (opens or creates a binary for appending). There are many functions available when working with filesfopen (null pointer is returned if problems with file)fclose (null pointer is returned if problems with file)fgetc() fetches the next character from the streamfputc character is written and pointer is advancedfgets() reads a line and includes the newline in the array fputs() writes a line (character array must include null character) and does not include thenewline characterIS 12 7Sample program using fgetc and fputc#include <stdio.h>enum {SUCCESS, FAIL};void CharReadWrite(FILE *fin, FILE *fout);main (void){FILE *fptr1, *fptr2;char filename1[]="outfile.txt";char filename2[]="inputfile.txt";int reval = SUCCESS;if ((fptr1 = fopen(filename1,"w")) == NULL){printf("Cannot open %s.\n", filename1);reval = FAIL;}elseif ((fptr2 = fopen(filename2, "r")) == NULL){printf("Cannot open %s.\n", filename2);reval = FAIL;}else {CharReadWrite(fptr2, fptr1);fclose(fptr1);fclose(fptr2);}return reval;}void CharReadWrite(FILE *fin, FILE *fout){int c;while ((c=fgetc(fin)) !=EOF){fputc(c,fout);putchar (c);}}Sample file inputfile.txt contentToday is the firstDay of the rest of your lifeIS 12 8Creating a Sequential File Program#include <stdio.h>int main(){ int account; char name[ 30 ]; double balance; FILE *cfPtr; /* cfPtr = clients.dat file pointer */ if ( ( cfPtr = fopen( "clients.dat", "w" ) ) == NULL ) printf( "File could not be opened\n" ); else { printf( "Enter the account, name, and balance.\n" ); printf( "Enter EOF to end input.\n" ); printf( "? " ); scanf( "%d%s%lf", &account, name, &balance ); while ( !feof( stdin ) ) { fprintf( cfPtr, "%d %s %.2f\n", account, name, balance ); printf( "? " ); scanf(


View Full Document

Pitt INFSCI 0012 - LECTURE NOTES

Download LECTURE NOTES
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view LECTURE NOTES and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view LECTURE NOTES 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?