CS1B – Introduction to Programming in C++Topic 3: String Data Type & Using FilesCS1B – Saddleback CollegeString 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 timetime• Or be optimistic String fixes these issuesIt handles memory allocation, and makes copying, or assigning values to strings easier2 of 15 Topic 3: Strings & FilesCS1B – Saddleback CollegeUsing Strings To use it you must the header file– #include <string> Declaring a stringstring name1;string name2; Assigning valuesname1 = “Jean”;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: n3 of 15CS1B – Saddleback CollegeAdditional 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 spacescout << name1.length(); OUTPUT: 13 cout << name1.size(); OUTPUT: 13 Instead of cin.getline()–For strings use getline(cin, stringName);4 of 15 Topic 3: Strings & FilesThis can also be an input fileCS1B – Introduction to Programming in C++Using Input / Output FilesChapter 9 in ShrinkwrapChapter 3 in MalikCS1B – Saddleback CollegeI/O Files Instead of using keyboard as input and the screen as output, we can use filesFile I/O is a 5-step process1. Include the header file fstream2. Declare the file stream variables3. Associate the file stream variables with the I/O sources4. Use the file stream variables with >>, << or other I/O functions5. Close the files6 of 15 Topic 3: Strings & FilesCS1B – Saddleback CollegeFile 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 file7 of 15Topic 3: Strings & FilesCS1B – Saddleback CollegeEXAMPLE#include <fstream>int main(){ …ifstream inFile;ofstream outFile;// opens the file named InputFile.txt as an input fileinFile.open(“InputFile.txt”); // opens the file named OutputFile.txt as an output fileoutFile.open(“OutputFile.txt”);outFile.open(“OutputFile.txt”);// reads a name in from inFile and puts the data in the variable namegetline(inFile,name); inFile >> id;// outputs the variable payrate to outDataoutFile << payRate << endl;// don’t forget to close your filesinFile.close();outFile.close();}8 of 15 Topic 3: Strings & FilesNOTE: Output manipulators can be used with files tooCS1B – Saddleback CollegeDynamically 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 terminated2 options2 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 & FilesCS1B – Saddleback CollegeDynamically Naming a File (2)Given:#include <fstream>…ifstream iFile;Example – using a c-stringchar inFileName[25];cout<< “Enter an Input File Name: ”cout<< “Enter an Input File Name: ”getline(cin, inFileName);iFile.open(inFileName);Example – using a stringstring inFileName;cout << “Enter an Input File Name: ”getline(cin, inFileName);iFile.open(inFileName.c_str());10 of 15 Topic 3: Strings & FilesCS1B – Saddleback CollegeCreate 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–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 file11 of 15 Topic 3: Strings & FilesCS1B – Saddleback CollegePassing 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 & FilesCS1B – Saddleback CollegeEXAMPLEvoid PrintHeaderToFile(ofstream &oFile,// output file - IN & OUTstring asName, // assignment Name - INchar asType, // assignment type // (LAB or ASSIGNMENT) // - INint asNum); // assignment number - INint main (){13 of 15 Topic 3: Strings & Files{ofstream outFile; // outFile – “output.txt”outFile.open("output.txt");// output header for this labPrintHeaderToFile(outFile, "Functions", 'A', 14);outFile << "I can output from here now too";outFile.close();return 0;}CS1B – Saddleback CollegeIncluding 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 runthe functions in that file to run14 of 15 Topic 3: Strings & FilesCS1B – Saddleback College#include<string>#include <iostream>#include <iomanip>#include <fstream>using namespace std;void PrintHeaderToFile(ofstream &oFile, // output file – IN & OUTstring asName, // assignment Name - INchar asType, // assignment type - INint asNum) // 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";This can be placed in a separate file15 of 15 Topic 3: Strings & FilesoFile<<"\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 <<
View Full Document