Presentation is loading. Please wait.

Presentation is loading. Please wait.

9/21/2015 1R. Smith - University of St Thomas - Minnesota CISC 130: Today’s Class RecapRecap Files, writing filesFiles, writing files 1D Array Recap1D.

Similar presentations


Presentation on theme: "9/21/2015 1R. Smith - University of St Thomas - Minnesota CISC 130: Today’s Class RecapRecap Files, writing filesFiles, writing files 1D Array Recap1D."— Presentation transcript:

1 9/21/2015 1R. Smith - University of St Thomas - Minnesota CISC 130: Today’s Class RecapRecap Files, writing filesFiles, writing files 1D Array Recap1D Array Recap 2D Arrays2D Arrays Assignment 11Assignment 11 Searching for a stringSearching for a string

2 Recap Drawing in 2 dimensionsDrawing in 2 dimensions Vertical HistogramVertical Histogram Working with FilesWorking with Files 9/21/2015 2R. Smith - University of St Thomas - Minnesota

3 9/21/2015 3R. Smith - University of St Thomas - Minnesota Recap: Working with files Instead of getchar(), putchar(), printf()Instead of getchar(), putchar(), printf() –Use getc(), putc(), fprintf() “FILE” type variable with weird syntax“FILE” type variable with weird syntax Function fopen() opens a fileFunction fopen() opens a file –First argument: string with file name –Second argument: “r” for read, “w” for write –Result: “FILE pointer” if success, else “NULL” literal value Function fclose(fp) called when doneFunction fclose(fp) called when done

4 9/21/2015 4R. Smith - University of St Thomas - Minnesota Example: Writing a File FILE *fp;// file pointer char lin[LEN];// line buffer int i;// loop index printf("file name: "); getline(lin, LEN); fp = fopen(lin, "w"); for (i = 0; i < 10; i++) fprintf(fp, "Line %d\n", i); fclose(fp);

5 9/21/2015 5R. Smith - University of St Thomas - Minnesota Writing a File Writing a File Using putc(c, fp) to write charactersUsing putc(c, fp) to write characters –First argument: char; second argument: file pointer Asking for a file nameAsking for a file name –It goes to the same folder unless you type in a different folder –You should check for Bad file name – what happens?Bad file name – what happens? –Handling the error condition File name loopFile name loop –Write a do or while loop to repeat till the fopen() works Retrofitting an Existing ProgramRetrofitting an Existing Program –Convert ‘histogram’ to print to a file –Or, convert your paystub printer to go to a file

6 9/21/2015 6R. Smith - University of St Thomas - Minnesota Checklist for Side Effects Does it do input (from keyboard, for example)?Does it do input (from keyboard, for example)? Does it do output (to display, for example)?Does it do output (to display, for example)? Does it modify its arguments (arrays)?Does it modify its arguments (arrays)? Any YES to the above => side effectsAny YES to the above => side effects OK to “look” at arrays, but not to change themOK to “look” at arrays, but not to change them –Changing an array argument = Side Effect

7 Recap on 1D Array functions There’s the ‘maximum’ length and ‘real’ lengthThere’s the ‘maximum’ length and ‘real’ length –Maximum length = the most elements it can possibly hold –Real length = actual number of elements in the array When we DECLARE the functionWhen we DECLARE the function –The array argument: We specify the array Type and Size: int nums[NSIZE]We specify the array Type and Size: int nums[NSIZE] –We may have a separate argument for the ‘real’ length When we USE the functionWhen we USE the function –The array argument: We specify JUST THE ARRAY NAME, no size or indexWe specify JUST THE ARRAY NAME, no size or index –If we have a separate ‘length’ argument, it’s given as an integer 9/21/2015 7R. Smith - University of St Thomas - Minnesota

8 Using Arrays For 1D arrays, there are TWO Cases:For 1D arrays, there are TWO Cases: Case #1: doing something to an ELEMENTCase #1: doing something to an ELEMENT –We have a single item in the array we need to work on –We include its INDEX (in brackets: num[i]) to pick it out Case #2: working on the Whole ArrayCase #2: working on the Whole Array –Always done by a Function –We pass the Array Name Only to the function Just for old times’ sake, write a ‘sum’ functionJust for old times’ sake, write a ‘sum’ function 9/21/2015 8R. Smith - University of St Thomas - Minnesota

9 9/21/2015 9R. Smith - University of St Thomas - Minnesota Two dimensional arrays Declare with two indicesDeclare with two indices –First one selects the row –Second index selects the column –Square brackets around each index: a[1][2] For a list of stringsFor a list of strings –First index picks a string –Second index is a character in that string

10 9/21/2015 10R. Smith - University of St Thomas - Minnesota 2D Arrays and Functions Function doesn’t need size of first indexFunction doesn’t need size of first index Function does need size of other indicesFunction does need size of other indices –Must appear in argument declaration Write a sample program that fills in a string arrayWrite a sample program that fills in a string array

11 9/21/2015 11R. Smith - University of St Thomas - Minnesota Assignment 11 Number lookup programNumber lookup program FunctionsFunctions –Read a file of name/number pairs into arrays –Extract strings from strings –Search the array for a name typed in –Read a line of text from a file –Compare the “prefix” of a string –Main loop

12 Suggested Strategy Phase 1Phase 1 –Write a function to read strings into an array A sentinel loop, looks for ‘null’ lineA sentinel loop, looks for ‘null’ line –Write a function to print strings from an array Can takeCan take –Call them from main Phase 2Phase 2 –Write a ‘split’ function to split lines –Rewrite the ‘read’ function to call the ‘split’ function Phase 3Phase 3 –Have it read the data from a file 9/21/2015 12R. Smith - University of St Thomas - Minnesota

13 Let’s work on Phase 1 What variables?What variables? What functions?What functions? 9/21/2015 13R. Smith - University of St Thomas - Minnesota

14 9/21/2015 14R. Smith - University of St Thomas - Minnesota Standard string.h functions strcmp() – described yesterdaystrcmp() – described yesterday –Input: 2 strings –Output: neg, zero, pos = difference between mismatch strspn() – skips over a set of charsstrspn() – skips over a set of chars –Input1: string to check –Input2: string of chars to skip over –Output: offset to first char that’s not in Input2 strncpy() – copies a string to a destinationstrncpy() – copies a string to a destination –Input1: destination –Input2: source –Input3: size of destination strlen() – length of a stringstrlen() – length of a string

15 9/21/2015 15R. Smith - University of St Thomas - Minnesota Writing the extract() function We need 2 array indicesWe need 2 array indices We need a variable for the ‘split’ indexWe need a variable for the ‘split’ index First, find the split pointFirst, find the split point Next, copy the number and mark the endNext, copy the number and mark the end Finally, copy the name using the 2 indicesFinally, copy the name using the 2 indices

16 9/21/2015 16R. Smith - University of St Thomas - Minnesota How the assignment works Call a function to read the number/name strings into 2 separate arraysCall a function to read the number/name strings into 2 separate arrays –One keeps the name strings –One keeps the number strings –Name[i] is the person whose number is in number[i] Do a loop till a blank line is enteredDo a loop till a blank line is entered –Read a line from input –Look it up in the ‘names’ array; retrieve the index –If it’s a valid index, print out the name and number Start with typed-in numbers/namesStart with typed-in numbers/names Create a file of numbers/names for next stepCreate a file of numbers/names for next step

17 9/21/2015 17R. Smith - University of St Thomas - Minnesota The extract() function void extract (char str[], char name[], char number[]) { int i, j;// indices to copy name int split;// split point between name and number split = strspn(str, "01234567890- "); strncpy(number, str, SIZE); number[split] = '\0'; j = 0; i = split; while (str[i] != '\0') { name[j] = str[i]; j = j + 1; i = i + 1; } name[j] = '\0'; }

18 9/21/2015 18R. Smith - University of St Thomas - Minnesota Creative Commons License This work is licensed under the Creative Commons Attribution-Share Alike 3.0 United States License. To view a copy of this license, visit http://creativecommons.org/licenses/by- sa/3.0/us/ or send a letter to Creative Commons, 171 Second Street, Suite 300, San Francisco, California, 94105, USA.


Download ppt "9/21/2015 1R. Smith - University of St Thomas - Minnesota CISC 130: Today’s Class RecapRecap Files, writing filesFiles, writing files 1D Array Recap1D."

Similar presentations


Ads by Google