DOC PREVIEW
Saddleback CS 1B - Topic 3: String Data Type & Using Files

This preview shows page 1-2-3 out of 8 pages.

Save
View full document
Premium Document
Do you want full access? Go Premium and unlock all 8 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

CS1B Introduction to Programming in C Topic 3 String Data Type Using Files CS1B Saddleback College String Data Type Problems with C strings They are static size can t be changed at runtime We have to know how long the string will be ahead of time Or be optimistic String fixes these issues It handles memory allocation and makes copying or assigning values to strings easier 2 of 15 Topic 3 Strings Files CS1B Saddleback College Using Strings To use it you must the header file include string Declaring a string string name1 string name2 Assigning values name1 Jean name2 name1 You can easily concatenate strings using name2 Rousseau name1 name1 name2 name1 name1 name2 cout name1 OUTPUT Jean Rousseau You can specify a value of a specific element of a string using the subscript operator cout name1 3 OUTPUT n 3 of 15 CS1B Saddleback College Additional String Functions Length Size Do the same thing get the length of the string Note this function RETURNS A VALUE so have a place to put it Doesn t include 0 but does include spaces cout name1 length OUTPUT 13 cout name1 size OUTPUT 13 Instead of cin getline For strings use getline cin stringName This can also be an input file 4 of 15 Topic 3 Strings Files Using Input Output Files CS1B Introduction to Programming in C Chapter 9 in Shrinkwrap Chapter 3 in Malik CS1B Saddleback College I O Files Instead of using keyboard as input and the screen as output we can use files File I O is a 5 step process 1 Include the header file fstream 2 Declare the file stream variables 3 Associate the file stream variables with the I O sources 4 Use the file stream variables with or other I O functions 5 Close the files 6 of 15 Topic 3 Strings Files CS1B Saddleback College File I O Details Include the fstream headerfile include fstream Declare the file stream variables ifstream inFile declares the input file stream ofstream outFile declares the output file stream Open the files inFile open inFileName txt opens the input file outFile open outFileName txt opens the output file Close the files when you are done with them inFile close closes the input file outFile close closes the output file 7 of 15 Topic 3 Strings Files CS1B Saddleback College EXAMPLE include fstream int main ifstream inFile ofstream outFile opens the file named InputFile txt as an input file inFile open InputFile txt opens the file named OutputFile txt as an output file outFile open OutputFile txt reads a name in from inFile and puts the data in the variable name getline inFile name inFile id outputs the variable payrate to outData outFile payRate endl don t forget to close your files inFile close outFile close NOTE Output manipulators can be used with files too 8 of 15 Topic 3 Strings Files CS1B Saddleback College Dynamically Naming a File To dynamically identify your input file take the filename in as input The string must be null terminated Data type string is not null terminated 2 options Declare a c string char fileName 25 Convert the string to a c string i e make it null terminated with c str string fileName fileName c str 9 of 15 Topic 3 Strings Files CS1B Saddleback College Dynamically Naming a File 2 Given include fstream ifstream iFile Example using a c string char inFileName 25 cout Enter an Input File Name getline cin inFileName iFile open inFileName Example using a string string inFileName cout Enter an Input File Name getline cin inFileName iFile open inFileName c str 10 of 15 Topic 3 Strings Files CS1B Saddleback College Create Your Input File First Go to File New File Make sure the files are in your project folder Output files will auto generate Input files won t Eclipse doesn t need these files to exist BUT if you want it to read input you need to identify it somewhere does need the input file 11 of 15 Topic 3 Strings Files CS1B Saddleback College Passing Files If you need to use an input file in two functions you need to pass as a parameter You can t just open and close the file Must be passed by reference use the 12 of 15 Topic 3 Strings Files CS1B Saddleback College EXAMPLE void PrintHeaderToFile ofstream oFile output file IN OUT string asName assignment Name IN char asType assignment type LAB or ASSIGNMENT IN int asNum assignment number IN int main ofstream outFile outFile open output txt outFile output txt output header for this lab PrintHeaderToFile outFile Functions A 14 outFile I can output from here now too outFile close return 0 13 of 15 Topic 3 Strings Files CS1B Saddleback College Including code in another file Create a cpp file Ensure it is contained in the same folder Include whatever preprocessor directives you need for the functions in that file to run 14 of 15 Topic 3 Strings Files include string CS1B Saddleback College include iostream include iomanip include fstream using namespace std void PrintHeaderToFile ofstream oFile string asName char asType int asNum This can be placed in a separate file output file IN OUT assignment Name IN assignment type IN assignment number IN oFile n oFile Programmed by Michele Rousseau n oFile n setw 14 Student ID 7502312 oFile n setw 14 Class CS1B MW 6p 7 30p if toupper asType L oFile LAB setw 9 else oFile ASSIGNMENT setw 2 oFile asNum asName oFile n n n oFile right 15 of 15 Topic 3 Strings Files


View Full Document

Saddleback CS 1B - Topic 3: String Data Type & Using Files

Download Topic 3: String Data Type & Using Files
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 Topic 3: String Data Type & Using Files 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 Topic 3: String Data Type & Using Files 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?