Presentation is loading. Please wait.

Presentation is loading. Please wait.

L which include file needs to be used for string manipulation? what using statement needs to be included fro string manipulation? l how is a string assigned.

Similar presentations


Presentation on theme: "L which include file needs to be used for string manipulation? what using statement needs to be included fro string manipulation? l how is a string assigned."— Presentation transcript:

1 l which include file needs to be used for string manipulation? what using statement needs to be included fro string manipulation? l how is a string assigned a value? Can a string be initialized at declaration? how is getline() different from extraction operator? what does this line do? string s1=”hi” + ’ ’ + ”there”; how are strings compared? Which one is greater? hell bell hello how can the size of a string be determined? Can string size be changed? What is the size of this string? string s2=”hello”; is there a problem with this statement? cout << s2[4]; assuming string s3=”C++ is fun”; what do the following functions do? s3.substr(4,2); s3.find(’+’); s3.rfind(’+’); s3.find_first_of(”is”); s3.find_last_of(”is”); s3.insert(3,2,’ ’); s3.replace(1,3,”--”); s3.append(”!!”); s3.erase(0,3); Review of Strings 1

2 Input/Output

3 l C++ uses streams for input and output l stream - is a sequence of data to be read (input stream) or a sequence of data generated by the program to be output (output stream) by default only standard streams are declared: cin (console input) cout (console output) l files are used to store information on permanent basis to do file input/output needs to be included as well as using std::ifstream; and using std::ofstream; l an input or output stream needs to be declared: ifstream fin; // input stream ofstream fout; // output stream l before C++ program can read from or write to a file stream, the stream needs to be connected to a file. This is referred to as opening the file: fin.open(”myfile.dat”); // opening file Streams 3

4 l a string variable can be used as file name c_str() function has to be applied to the string: string filename=”myfile.dat”; fin.open(filename.c_str()); l after stream variable has been connected to a file, extraction and insertion operators can be used with a stream: fin >> var1 >> var2 >> var3; l after the file is opened all the operations on the file is done through associated stream l before program finishes every stream needs to closed: fin.close(); // closing file function eof() returns true if end of input file has been reached: while(!fin.eof()){ … } Streams (Cont.) 4

5 function fail() returns true if previous stream operation failed l useful to check if file is opened successfully before performing operations on file useful function exit() - exists program immediately. To use exit, include exit() accepts one integer as argument: n convention: 0 - normal program termination; nonzero - abnormal l Example fin.open(”myfile.dat”); // opening file if (fin.fail()){ cout << ”Cannot open file myfile.dat”; exit(1); } I/O Error Checking 5

6 l streams can be passed as parameters l streams must be passed by reference void myfunc(ifstream &myinfile); Streams as Parameters 6


Download ppt "L which include file needs to be used for string manipulation? what using statement needs to be included fro string manipulation? l how is a string assigned."

Similar presentations


Ads by Google