C__DOCUME~1_MAXWID~1_LOCALS~1_Temp_plugtmp-27_lecture12-fileIO

C__DOCUME~1_MAXWID~1_LOCALS~1_Temp_plugtmp-27_lecture12-fileIO

Info iconThis preview shows pages 1–5. Sign up to view the full content.

View Full Document Right Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: 1 ee312 Spring 2008 Lecture 13 1 Announcements Lecture 12 Read chapter 22 (file IO) Review 13 (arrays and recursion) Topic for today File I/O ee312 Spring 2008 Lecture 13 2 What do we do with Strings? Input and output them Make a bigger String out of little ones Break big Strings into smaller ones Do comparisons (like in chars) Extremely useful in any application that manipulates text (e.g. translators, word processors, language puzzles, etc.) 2 ee312 Spring 2008 Lecture 13 3 String Variables A string variable is just a one-dimensional array of characters: #define STR_LEN 80 char str [STR_LEN+1]; The array should be one character longer than the string it will hold, to leave space for the null character at the end. Leave room for the null character when using string- handling functions in the C library. A string variable can be initialized: char date1[8] = "June 14"; A string initializer need not completely fill the array: char date2[9] = "June 14"; The leftover array elements are filled with null characters: If the length of the array is omitted, the compiler will compute it: char date3 = "June 14"; /* date3 is 8 characters long */ ee312 Spring 2008 Lecture 13 4 Reading and Writing Strings (1) To read or write a string, use scanf or printf with the %s conversion specification: scanf("%s", str); printf("%s", str); scanf skips white space, then reads characters into str until it encounters a white-space character. Problem if you want more than 1 word. No ampersand is needed when using scanf to read into a string variable. Since a string variable is an array, the name of the variable is already a pointer (to the beginning of the array). A faster alternative: Use gets and puts instead of scanf and printf: gets(str);gets reads characters into str until it encounters a new-line character. puts(str); puts prints str, followed by a new-line character. 3 ee312 Spring 2008 Lecture 13 5 Reading and Writing Strings (2) scanf and gets automatically put a null at the end of the input string. printf and puts assume that the output string ends with a null. Both scanf and gets assume that the string variable is large enough to contain the input string (including the null at the end). Failing to make the variable long enough will have unpredictable results. To make scanf safer, use the conversion specification %ns , where n specifies the maximum number of characters to be read. Use fgets instead of gets for greater safety. ee312 Spring 2008 Lecture 13 6 String Functions Summary all the standard array algorithms for: searching, adding, removing, sorting the char [ ] various input and output functions strcpy strcat strcmp strlen number to/from string conversion functions 4 ee312 Spring 2008 Lecture 13 7 Create a Password Program int main ( ) { char [ ] firstName = harold"; char [ ] middleName = joseph"; char [ ] lastName = hacker";...
View Full Document

Page1 / 17

C__DOCUME~1_MAXWID~1_LOCALS~1_Temp_plugtmp-27_lecture12-fileIO

This preview shows document pages 1 - 5. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online