Welcome~~~


Another blog:
http://fun-st.blogspot.com/

It is easier to write an incorrect program than understand a correct one.

Friday, February 11, 2011

Basic C++ ::: I/O and String

===== Interactive I/O ============  # include<iostream>
Output and Input using the "stream"

cin with ignore the blank, so if input : A M

cin >> a >> b;

a will get the A, and b will get the M,

Also, if input A B C
cin >> ch1 >> ch2;
cin >> ch3
cin will keep it in the stream, so finally, ch3 will also get C'

However, it will ignore the blank, so how to get the blank, use the member function in cin: cin.get()

cin >> ch1;    // ch1 will get A
cin.get(someChar);  // someChar is a char variable, it will get the blank
cin >> ch2;  // read the B


==> remove and discard characters from the input
cin.ignore(N,ch)   \\    skip (read and discard) up to N characters in the input, or until the character ch has been read and discard, whichever comes first.
cin.ignore(80,'\n')  --> skip the next 80 input characters or to skip characters until a newline character is read

===== File I/O ============ # include<fstream.h>

ifstream inStream;
ofstream outStream;

inStream.open("readme.dat")
outStream.open("writeme.dat")

inStream.close()


In file reading, the end of a file ais marked by a spectial character, call the end-of-file or EOF marker

A while loop is used to extract the data from the input stream until it fails.

Note well: a preliminary or priming read is used before the while loop in order to avoid an attempt to process data when the input stream is initially empty.

inStream.eof()  --> Boolean function that  returns true if the last input operation attempted to read the end of the file mark
ch = inStream.peek() --> provides ways to examine the next character in the input stream without removing it from the stream.
inStream.get(ch) --> is to get the next character in the input stream, and remove it from the stream
inStream.putback(ch)  --> to return the last character read to the input stream
in.Stream.file() --> bool, check the status of the last operation on the input stream, if fail, return true

File output rediractin
#include<iostream>
#include<fstream>
ofstream outfile;
outfile.open("outfile")
redirecto(outfile); // send output to the file
redirecto(cout); // send output to the display

void redirecto(ostram &ofile)
{
  ofile << "this is the output" << endl;
}


========= Formatting Output ========== # include<iomanip.h>

iomanip.h  --> this is the manipulator header file, 
notes that when using cout << "Great Day!" << endl;
endl is a manipulator which will output a new line

the manipulater here is : setw, and setprecision

setw--> one integer  parameter, the number of spaces which the values is displayed
--? set the precision, number of digits shown after the decimal point, one integer parameter

to activate them, include two lines

outStream.setf(ios::fixed, ios::floatefield);
outStream.setf(ios::showpoint);

those are flags.....

<pre class = "brush.ja">

outStream.setf(ios::left)
           //insert left justfied output statmetns here
outStream.fill('0');       // pad with zeros
outStream << setw(10) << setprecision(2) << salary;
outStream.fill(' ');       // reset padding
outStream.unsetf(ios::left)

</pre>

============  # include <string.h>  ==========
note that getline is in this head file:

getline(iFile, String1)
getline(iFile, String1, '\t')   // this is to read to the tab, 

you can use while(iFile) to iteratively read each line

some function:
string1.length()  --> return length int
string.empty() --> bool

MyGreetings = "I am showing " + Creating + "\n";   \\\ concatenation

s1.compare(s2)  --> int (neg, 0 or posi)
          - negative : if the first differing element in s1 compares less then the corresponding element in s2, or if s1 is a prefix of s2
         - zero: if they are same
        -- positive: otherwise

s1.at(3)     --- char at the position of the string.   = s1[3]

s1.insert(4, "fred");   -- to insert anther string (e.g. here "fred") to s1 at position 4

s1.insert(4, "fredainignidnd", 7, 3);    ------ to insert anther string to s1 at position 4, the inserted is a substring start from position 7 of 'fredainignidnd', and lend of 3

s1.substr(4, 9);        --- sub string from 4, total 9 char
s1.eras(4, 9) --- delete sub string from 4, total 9 char

s1.replac(0, 3, "fredainignidnd"); -- start from position 0. 3 characters will be replace by the 'fredainignidnd',

s1.find("be", 0); --- start from position 0, return the position that first find "be"
      eg. int loc = s1.find("be", 0);
           int newloc = s1.find("be", loc+1);   \\ find the next be, note to use loc + 1




♥ ¸¸.•*¨*•♫♪♪♫•*¨*•.¸¸♥

No comments:

Post a Comment