Course Hero Logo

Exit1 printfenter proverbs of less than 80 characters

Course Hero uses AI to attempt to automatically extract content from documents to surface to you and others so you can study better, e.g., in search results, to enrich docs, and more. This preview shows page 505 - 508 out of 638 pages.

exit(1);}printf("Enter proverbs of less than 80 characters or press Enter to end:\n");while(true){fgets(more, LENGTH, stdin);/* Read a proverb*/if(more[0] == '\n')/* If its empty line*/break;/* end input operation*/fputs(more, pfile);/* Write the new proverb*/}fclose(pfile);/* Close the file*/if(!(pfile = fopen(filename, "r")))/* Open the file to read it */{printf("Error opening %s for writing. Program terminated.", filename);exit(1);}/* Read and output the file contents */printf("The proverbs in the file are:\n\n");while(fgets(more, LENGTH, pfile))/* Read a proverb */printf("%s", more);/* and display it */
CHAPTER 12WORKING WITH FILES479fclose(pfile);/* Close the file */remove(filename);/* and remove it*/return 0;}Here is some sample output from this program:Enter proverbs of less than 80 characters or press Enter to end:Least said, soonest mended.A nod is as good as a wink to a blind horse.The proverbs in the file are:Many a mickle makes a muckle.Too many cooks spoil the broth.He who laughs last didn't get the joke in the first place.Least said, soonest mended.A nod is as good as a wink to a blind horse.How It WorksYou initialize the array of pointers,proverbs[], in the following statement:char *proverbs[] ={"Many a mickle makes a muckle.\n","Too many cooks spoil the broth.\n","He who laughs last didn't get the joke in"" the first place.\n"};You specify the three sayings as initial values for the array elements, and this causes the compiler to allocatethe space necessary to store each string.You have a further declaration of an array that will store a proverb that will be read from the keyboard:char more[LENGTH];/* Stores a new proverb */This initializes a conventionalchararray with another proverb. You also include'\n'at the end for the samereason as before.After creating and opening a file on drive C for writing, the program writes the initial three proverbs to the filein a loop:int count = sizeof proverbs/sizeof proverbs[0];for(int i = 0 ; i < count ; i++)fputs(proverbs[i], pfile);The contents of each of the memory areas pointed to by elements of theproverbs[]array are written to thefile in theforloop using the functionfputs(). This function is extremely easy to use; it just requires a pointer tothe string as the first argument and a pointer to the file as the second.The number of proverbs in the array is calculated by the following expression:sizeof proverbs/sizeof proverbs[0]The expressionsizeof proverbswill evaluate to the total number of bytes occupied by the complete array,andsizeof proverbs[0]will result in the number of bytes required to store a single pointer in one element ofthe array. Therefore, the whole expression will evaluate to the number of elements in the pointer array. You could
480CHAPTER 12WORKING WITH FILEShave manually counted how many initializing strings you supplied, of course, but doing it this way means that thecorrect number of iterations is determined automatically, and this expression will still be correct even if the arraydimension is changed by adding more initializing strings.

Upload your study docs or become a

Course Hero member to access this document

Upload your study docs or become a

Course Hero member to access this document

End of preview. Want to read all 638 pages?

Upload your study docs or become a

Course Hero member to access this document

Term
Summer
Professor
NoProfessor
Tags
IVOR HORTON

Newly uploaded documents

Show More

  • Left Quote Icon

    Student Picture

  • Left Quote Icon

    Student Picture

  • Left Quote Icon

    Student Picture