Reading a File, String Search

Download Report

Transcript Reading a File, String Search

CISC 130: Today’s Class
•
•
•
•
Recap
2D Array – A list of strings
Looking at Assignment 11
Splitting Lines
4/8/2016
R. Smith - University of St Thomas - Minnesota
1
Recap
•
•
•
•
Files, writing files
1D Array Recap
2D Arrays
Assignment 11
4/8/2016
R. Smith - University of St Thomas - Minnesota
2
Two dimensional arrays
• Declare 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 strings
– First index picks a string
– Second index is a character in that string
4/8/2016
R. Smith - University of St Thomas - Minnesota
3
2D Arrays and Functions
• Write a sample program that fills in a string
array
• Type in lines of text
• Save each line in a string in the array
• Then, print out the array.
• Break it into functions.
• Do it with an input file
4/8/2016
R. Smith - University of St Thomas - Minnesota
4
A practical look at #11
• We read data from a file containing 2 columns
• We split data from the 2 columns
– Each goes into a separate array: numbers and names
• Index values ‘match’ for the column
– If names[5] = “Joe” then numbers[5] = his phone number
• We search for names in ‘names’ array
– then we print out the corresponding name and number
4/8/2016
R. Smith - University of St Thomas - Minnesota
5
How the assignment works
• Call 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 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/names
• Create a file of numbers/names for next step
4/8/2016
R. Smith - University of St Thomas - Minnesota
6
Suggested Strategy
• Phase 1 (just finished)
– Write a function to read strings into an array
• A sentinel loop, looks for ‘null’ line
– Write a function to print strings from an array
– Call them from main
• Phase 2
– Write a ‘split’ function to split lines
• Phase 3
– Rewrite the ‘read’ function to call the ‘split’ function
• Phase 4
– The search and printout
4/8/2016
R. Smith - University of St Thomas - Minnesota
7
We just did Phase 1
• How does Phase 2 work?
• How do we split names from numbers?
• we need some help…
• Maybe we look up the Gnu C Library again?
4/8/2016
R. Smith - University of St Thomas - Minnesota
8
Standard string.h functions
• strlen() – length of a string
• strspn() – 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 destination
– Input1: destination
– Input2: source
– Input3: size of destination
4/8/2016
R. Smith - University of St Thomas - Minnesota
9
Writing the extract() function
• Local variables
– We need 2 array indices
– We need a variable for the ‘split’ index
• First, find the split point
– The end of the number
• Next, copy out the name and number
– Option: copy the number
– Option: copy the name
4/8/2016
R. Smith - University of St Thomas - Minnesota
10
An 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';
}
4/8/2016
R. Smith - University of St Thomas - Minnesota
11
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/bysa/3.0/us/ or send a letter to Creative
Commons, 171 Second Street, Suite 300, San
Francisco, California, 94105, USA.
4/8/2016
R. Smith - University of St Thomas - Minnesota
12