input streams ifstream xin; // declares an input stream object (for reading files)

2
Copyright © 2002, Department of Systems and Computer Engineering, Carleton University 1 es an input stream object (for reading files) taches the input stream object to the file specified ::ws); // turns off whitespace skipping when reading characters _array) >> string; // prevents overflow when reading strings es true if the stream is in the failed state (something has gone wrong) s true if the program has tried to read past the end of the file he failure flags // discards input characters until “count” characters have been discarded // or character “ch” has been discarded (whichever comes first) ; // copies input characters into character array “array” until “size – 1” // characters have been copied or character “ch” is seen. in the second // case character “ch” is not read. next input character ; // copies the next line of input (everything up to the next ‘\n’) into the // character array supplied. the ‘\n’ which ends the input process is // discarded. at most “size – 1” characters will get read (if the line is // too long to be read, the extra characters are just left in the pipeline) res an output stream object (for reading files) ::fixed | ios::showpoint); // forces use of non-scientific notation and the display // of digits to the right of the d // this option remains in effect un ue); // selects the number of digits to be displayed to the right of the decimal point // when outputting double values. the choiice remains in effect unt << … // indicates that the next value output is to occupy the specified number of // columns. a one shot deal – affects only the next value outpu / selects the fill character to be used when padding output values to a specified width. // the choice remains in effect until changed. changed to false and vice versa) may only be used with integral (int or long) values an operator is equivalent to a = a <op> (b) st, then use the new value alue, then increment afterwards uble) { ouble d is long) { ong nt

Upload: theodore-todd

Post on 30-Dec-2015

46 views

Category:

Documents


0 download

DESCRIPTION

INPUT STREAMS ifstream xin; // declares an input stream object (for reading files) xin.open(string); // attaches the input stream object to the file specified - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: INPUT STREAMS ifstream xin;  // declares an input stream object (for reading files)

91.166 Copyright © 2002, Department of Systems and Computer Engineering, Carleton University 1

INPUT STREAMS

ifstream xin; // declares an input stream object (for reading files)

xin.open(string); // attaches the input stream object to the file specified

xin >> resetiosflags(ios::ws); // turns off whitespace skipping when reading charactersxin >> setw(size_of_char_array) >> string; // prevents overflow when reading strings

xin.fail() // produces true if the stream is in the failed state (something has gone wrong)xin.eof() // produces true if the program has tried to read past the end of the filexin.clear() // resets the failure flags

xin.ignore (count, ch); // discards input characters until “count” characters have been discarded // or character “ch” has been discarded (whichever comes first)

xin.get(array, size, ch); // copies input characters into character array “array” until “size – 1” // characters have been copied or character “ch” is seen. in the second // case character “ch” is not read. xin.get(); // gets the next input characterxin.getline(array, size); // copies the next line of input (everything up to the next ‘\n’) into the // character array supplied. the ‘\n’ which ends the input process is // discarded. at most “size – 1” characters will get read (if the line is // too long to be read, the extra characters are just left in the pipeline)

OUTPUT STREAMS

ofstream xout; // declares an output stream object (for reading files)

xout << setiosflags (ios::fixed | ios::showpoint); // forces use of non-scientific notation and the display // of digits to the right of the decimal point. // this option remains in effect until changed.

xout << setprecision(value); // selects the number of digits to be displayed to the right of the decimal point // when outputting double values. the choiice remains in effect until changed.

xout << setwidth (value) << … // indicates that the next value output is to occupy the specified number of // columns. a one shot deal – affects only the next value output

xout << setfill(ch); // selects the fill character to be used when padding output values to a specified width. // the choice remains in effect until changed.

OPERATORS

! = logical NOT (true is changed to false and vice versa)% = modulus (remainder). may only be used with integral (int or long) valuesa <op>= b, where <op> is an operator is equivalent to a = a <op> (b)++a means increment first, then use the new value a++ means use the old value, then increment afterwards

if (either operand is double) { the result will be double} else (if either operand is long) { the result will be long} else { the result will be int}

Page 2: INPUT STREAMS ifstream xin;  // declares an input stream object (for reading files)

91.166 Copyright © 2002, Department of Systems and Computer Engineering, Carleton University 2

LIBRARY FUNCTIONS

Mathematical

double fabs (double x); returns the absolute value of “x” int abs (int x); same but returns an integer result

double sin (double x); returns the sine of “x” (note: “x” is in radians) double cos (double x); returns the cosine of “x” (note: “x” is in radians)

double asin (double x); returns the inverse sin of “x” (in radians) double acos (double x); returns the inverse cosine of “x” (in radians)

String:

int strlen (const char str[]); returns length of string (not counting the termination)

void strcpy (char tostr[], const char fromstr[]); copies “fromstr” into “tostr”

int strcmp (const char str1[], const char str2[]); compares “str1” to “str2” if str1 > str2, returns a value greater than zero if str1 == str2, returns zero if str1 < str2, returns a value less than zero int stricmp (const char str1[], const char str2[]); as for “strcmp” but ignores case

double atof (const char str[]); converts a string to a floating point value int atoi (const char str[]); converts a styring to an integer value

Miscellanous

int rand(void); returns a “randomly” chosen value between 0 and RAND_MAX – 1 void randomize (void); Borland only – intended to be called once before making use of “rand”. not really necessary.

Character:

bool isalnum(char ch); returns true if the character is alphanumeric (letter or digit) bool isdigit(char ch); returns true if the character is a digit (one of ‘0’ through ‘9’) char toupper(char ch); returns the uppercase equivalent of a character

ASCII CODES

‘a’ = 97 ….. ‘z’ = 122 ‘A’ = 65 …..’Z’ = 90 ‘0’ = 48…..’9’ = 57

MISCELLANOUS

Array elements are numbered starting with 0. If an array contains “n” values, the last value is stored at position “n – 1”.

Sample structure definition: struct point { double lat; double lng;

}; // *** note the semi-colon

Global constants (and structure definitions) are OK. Global variables are forbidden.