file input in hic

22
File Input in HiC Run menu Set Input File… Load Input Input (Interactive) (Batch) • OK We want to know how to do it ourselves, right? 1

Upload: rory

Post on 23-Jan-2016

48 views

Category:

Documents


0 download

DESCRIPTION

File Input in HiC. Run menu Set Input File… Load Input Input (Interactive) (Batch) OK We want to know how to do it ourselves, right?. Review: Standard Input/Output Stream. // Header file #include int size; float avg; cin >> size; // cin: Standard input stream - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: File Input in HiC

File Input in HiC

• Run menu• Set Input File… • Load Input• Input (Interactive)• (Batch)• OK

We want to know how to do it ourselves, right?

1

Page 2: File Input in HiC

Review: Standard Input/Output Stream

// Header file#include <iostream>

int size; float avg;

cin >> size;// cin: Standard input stream // >>: input operator

cout << "Average is " << avg; // cout: standard output stream// <<: output operator

2

Page 3: File Input in HiC

Header File <iostream>

// Input streamclass istream{private: // Members:

public: bool eof(); void get(char& x); void getline(char s[], int size, char endChar); …};

istream cin;// Variable of istream// connecting cin to keyboard

3

Page 4: File Input in HiC

Header File <iostream>

// Output streamclass ostream{private: // Members

public: bool good(); bool fail(); …};

ostream cout;// Variable of ostream// connecting cout to monitor

4

Page 5: File Input in HiC

Standard Error Stream

cerr << “Error message.”;

// cerr: Standard error stream

// always go to monitor

// Not working in HiC

5

Page 6: File Input in HiC

Header File for File I/O

// File Stream#include <fstream>

// Input File Streamclass ifstream{ …};

// Output File Streamclass ofstream{ …};

6

Page 7: File Input in HiC

Class ifstream// File Stream#include <fstream>

class ifstream{private: . . .public: void open(const char fileName[]); // HiC cannot use C++ string for fileName bool good(); bool fail(); void close(); bool eof(); void get(char& x); …}; 7

Page 8: File Input in HiC

Class ofstream

// File Stream#include <fstream>

class ofstream{private: . . .public: void open(const char fileName[]); // Not C++ string void close(); bool good(); bool fail(); …};

8

Page 9: File Input in HiC

#include <fstream>// Defines class ifstream and ofstream

int main(){ // use any identifiers you want ifstream MyInput; ofstream yourOutFile; ifstream p7_File, cin_redirect; ofstream P6Out, cout_redirect;

. . .

return 0;}

File Input/Output

9

Page 10: File Input in HiC

#include <fstream>

int main(){ ifstream MyInput;

// Open file // void open(const char fileName[]); MyInput.open(“P6.IN"); // P6.IN is in the same folder as the source file

MyInput.open(“J:\\P6.IN"); // P6.IN could be any where // Escape char ‘\’

// Do work

MyInput.close(); // void close(); // No parameter!

return 0;}

File Input/Output

10

Page 11: File Input in HiC

#include <iostream> #include <fstream>

int main(){ ifstream MyInput;

MyInput.open(“P6.IN");

// Check open operation if (!MyInput.good()) cout << "Error: Cannot open input file";

return 0;}

Checking Open Operation

11

Page 12: File Input in HiC

#include <iostream> #include <fstream>

int main(){ ifstream MyInput;

MyInput.open(“P6.IN");

// Check open operation if (MyInput.fail()) { cout << "Error: Cannot open input file"; return 1; }

// Do work

return 0;}

Checking Open Operation

12

Page 13: File Input in HiC

#include <iostream> #include <fstream>

int main(){ ifstream MyInput; ofstream yourOutFile;

MyInput.open(“P6.IN"); if (MyInput.fail()) { cout << "Error: Cannot open input file!"; return 1; }

yourOutFile.open(“p6.out”); if (!yourOutFile.good()) { cout << "Error: Cannot open output file!"; return 1; }

return 0;}

File Input/Output

13

Page 14: File Input in HiC

#include <iostream> #include <fstream>

int main(){ ifstream MyInput; ofstream yourOutFile;

MyInput.open(“P6.IN"); yourOutFile.open(“P6.out”);

// Check open file

int x; MyInput >> x; // cin >> x; while (!MyInput.eof()) // cin.eof() { // process data x ++; // output to file yourOutFile << x; // cout << x;

// read the next value MyInput >> x; } return 0;}

File Input/Output

14

Page 15: File Input in HiC

#include <iostream>#include <fstream>

int main(){ ifstream MyInput; ofstream yourOutFile; int x;

MyInput.open(“P6.IN"); yourOutFile.open(“p6.out”); if (MyInput.fail() || !yourOutFile.good()) { cout << "Error: Cannot open files!"; return 1; }

MyInput >> x; while (!MyInput.eof()) { x ++; yourOutFile << x;

MyInput >> x; }

MyInput.close(); yourOutFile.close();

return 0;}

File Input/Output

15

Page 16: File Input in HiC

// DO_01: Include header file for file I/O

int main(){ // DO_02: declare variables for file I/O

// DO_03: open file

// DO_04: Check open operation

// DO_05: Read value from a file

// DO_06: Fill condition: read until the end of file while ( ) { // DO_07: Output value to a file }

// DO_08: Close files

}

File Input/Output

16

Page 17: File Input in HiC

int main(){ char x;

cin.get(x); while(!cin.eof()) { if (x == ' ') // process a space else if (x == '\n') // process a new line char else // process a regular char

cin.get(x); }

return 0;}// cin >> x will skip all white spaces.// cin.get() will read in white spaces.

Function get()

17

Page 18: File Input in HiC

int main(){ char x; ifstream MyInput(“P6.in");

MyInput.get(x); while(!MyInput.eof()) { if (x == ' ') // process a space else if (x == '\n') // process a new line char else // process a regular char

MyInput.get(x); }

return 0;}// MyInput >> x will skip all white spaces.// MyInput.get() will read in white spaces.

Function get()

18

Page 19: File Input in HiC

#include <iostream>#include <fstream>

int main(){ char file_name[21]; // Cannot use data type string for file_name ifstream MyInput; cout << “Enter input file name: ”; cin >> file_name;

MyInput.open(file_name); // MyInput.open(“P6.IN"); // MyInput.open(“file_name”); NO!

// Check open operation if (!MyInput.good()) { cout << "Error: Cannot open input file!"; return 1; }

// Do work

MyInput.close();

return 0;}

Input File Name

19

Page 20: File Input in HiC

int main(){ ifstream MyInput;

if (!OpenFile(MyInput)) return 1;

// Do work

return 0;}

bool OpenFile(ifstream& inFile){ char file_name[21]; cin >> file_name; inFile.open(file_name);

if (!inFile.good()) { cout << "Error: Cannot open input file"; return false; } else return true;}

File Stream as Function Parameter

20

Page 21: File Input in HiC

Schedule

Quiz9–3

9 AM, Monday

Test 3

Next Monday

Final Exam

7:00-8:52 pm, Thursday, May 19, 2011, Doudna 103

21

Page 22: File Input in HiC

Test 3

• Classes

data members (fields)

constructors

methods• Selection Sorting• File I/O• Enumeration data type• C String (Array of char )

22