1 / 13

Functions, Projects, and C++ I/O Streams

Functions, Projects, and C++ I/O Streams. Dr. Nancy Warter-Perez June 4, 2003. Functions. < return_type > func_name ( arg1_typ arg1_name, …, argN_typ argN_name ) { function body } Func_name – name of the function main – all programs must start with this function

Download Presentation

Functions, Projects, and C++ I/O Streams

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Functions, Projects, and C++ I/O Streams Dr. Nancy Warter-Perez June 4, 2003

  2. Functions <return_type> func_name (arg1_typ arg1_name, …, argN_typ argN_name) { function body } • Func_name – name of the function • main – all programs must start with this function • Return_type – type of value returned by function • Arguments • call-by-value – arguments are inputs to function that can’t be modified by function • Function prototype (used in header files [*.h]) <return_type> func_name (arg1_typ, …, argN_typ); • Library functions – commonly used functions • iostream.h, string.h, stdlib.h, math.h (to name a few) Functions, Projects, and I/O Streams

  3. Projects Separate Compilation Library functions *.a Compile Link File1.cpp Executable *.exe … Compile FileN.cpp Object files *.obj Functions, Projects, and I/O Streams

  4. Steps to Creating a Visual C++ Project (Step 1) • To create a project • Under File, select New • Under Projects • Select Win32 Console Application • Assign a Project name and Location for your project • Select Create new workspace • When prompted for type of console application, select empty project • Click Finish • Click OK Functions, Projects, and I/O Streams

  5. Steps to Creating a Visual C++ Project (Step 2) • To add a C or C++ source file to the project • Under File, select New • Under Files • Select C++ Source File • Select Add to project (Project will be set to the current project) • Assign a File name (use the default location determined by the project) • Click OK (the source file will be displayed in the editor window) Functions, Projects, and I/O Streams

  6. Steps to Creating a Visual C++ Project (Step 3) • Enter your program in the editor • Notice that the editor has a color coding • Comments • Key words • Everything else • Also notice that it automatically indents • Don’t override!! • If doesn’t indent to proper location – indicates bug Functions, Projects, and I/O Streams

  7. Steps to Creating a Visual C++ Project (Step 4) • To build your program • Under Build • Select Build project_name.exe • In case of compile time errors or warnings, they will be listed in the bottom window (scroll up) • Double click on error or warning to find in program • After fixing error (bug), rebuild following same steps Functions, Projects, and I/O Streams

  8. Steps to Creating a Visual C++ Project (Step 5) • To execute your program • First, create any necessary input files • Under File, select New • Under Files, select Text File • Assign File name and Location (default ok) • It is OK to add to project (default) • Click OK • To run your program (can click ‘!’ icon, or) • Under Build, select Execute project_name.exe Functions, Projects, and I/O Streams

  9. C++ I/O streams - input • Standard I/O input stream: cin • Ex: int x; char c1, c2, c3; cin >> x >> c1 >> c2 >> c3; If the following input is typed: 23 a b c Then, x = 23, c1 = 'a', c2 = 'b', c3 = 'c' (will ignore white spaces) Functions, Projects, and I/O Streams

  10. C++ I/O streams - output • Standard I/O output stream: cout • Ex: int x = 454; char c1 = 'L'; cout << "Bioinformatics:\tCHEM " << x << c1 << endl; The following output is displayed: Bioinformatics: CHEM 454L Functions, Projects, and I/O Streams

  11. I/O Streams Usage • Must include iostream header file • #include <iostream> • There are ways to format the output to specify parameters such as the width of a field, the precision, and the output data type Functions, Projects, and I/O Streams

  12. File I/O – Fstream (1) • Preprocessor directive • #include <fstream> • Need a file object to keep track of file access information • fstream fin, fout; // can use any variable names • Need to open a file • fin.open("in_file.dat", ios::in); read-only • fout.open("out_file.dat", ios::out); write-only Functions, Projects, and I/O Streams

  13. File I/O – Fstream (2) • To read from a file: • int x; fin >> x; //will read an integer value from in_file.dat into x • To write to a file: fout << x; • To close a file: • fin.close(); • fout.close(); • To detect the end of a file: • fin >> x; while (!fin.eof()) // eof will be set when end-of-file reached fin >> x; Functions, Projects, and I/O Streams

More Related