Software Layers 2 Introduction to unix 2



Download 0.58 Mb.
Page9/26
Date28.01.2017
Size0.58 Mb.
#10070
1   ...   5   6   7   8   9   10   11   12   ...   26

Program Input/Output


Week 5
4 kinds of Program I/O:

  • standard I/O: cin (keyboard), cout (monitor)

  • I/O redirection: unix operators <, >>, | (files)

  • command-line: using arguments in your C++ programs

  • file I/O: using files in your C++ programs


IO redirection:


You can redirect standard I/O from the unix prompt

  • cmd > filename  sends all output from the cmd to specified file (file is overwritten)

eg: myProgram > results.txt

  • cmd >> filename  results of the cmd are appended to file's current contents (file isn't overwritten)

eg: myProgram >> all_results.txt

  • cmd < filename  the cmd uses the data in the file instead of standard input

eg: myProgram < data.txt

  • cmd1 | cmd2  the results of cmd1 are used as the input to cmd2

eg: cat results.txt | wc

eg:

% cat data.txt

The answer to life, the universe, everything is 42.


QED.

% wc data.txt

4 10 59 data.txt

% cat data.txt | wc

4 10 59

%



C++ Command line Parameters:


The main function can contain two special formal parameters:

int argc  the number of cmd line arguments (including the program name, so argc >= 1)

char** argv  an array of strings for each argument

eg:

#include
int main(int argc, char** argv) {
cout << "argc is " << argc << endl;

for (int index=0; index < argc; index++) {


cout << "argv[" << index << "] is "

<< argv[index] << endl;

}

return 0;



}

note: because argv contains strings, you may need to do string conversions (using the routines like atof and atoi from stdlib.h)

C++ Files


Week 5

Introduction


    • stream = a communication mechanism for data transfer between a device and a program

    • input stream = data from a sender device (keyboard)

    • output stream = a way to send data to a receiver device (monitor)

    • streams can also be bi-directional, and random-access (however, we only consider either input or output streams, and not random-access)

  • Devices:

    • streams must be associated with a physical device (such as a text file on a floppy disk)

  • Stream ADT operations:

  • Stream ADT is implemented by various C++ classes




  • Streams

    • Standard I/O streams are defined in iostream.h

    • File streams are defined in fstream.h  this contains the classes: ofstream & ifstream.



Code:


  • Instance.open(FileName)  opens/renews file if possible.

  • Instance.close()  closes file (good programming)

  • Instance.fail()  determines if the operation has failed.




  • Instance.eof()  detects end of file (useful when looping through input)

  • Instance.get(VarChar)  reads next char into VarChar

  • Instance.getline(VarString,MaxLength)  reads next whole line (up to MaxLength) into VarString

  • OutputStreamInstance.put(VarChar)  writes single char to file

  • Instance << VarSting << endl;  this code writes VarString into file


eg:

int main(void) {

ifstream InputFile;

ofstream OutputFile;
if (OpenInputFile(InputFile) && OpenOutputFile(OutputFile)) {

CopyIntegers(InputFile, OutputFile);

InputFile.close();

OutputFile.close();

}

else


cout << "Error opening file" << endl;

return(0);

}

//----------------------------------------



CopyIntegers(ifstream &InputFile, ofstream &OutputFile) {

int Data;

InputFile >> Data;

while (!InputFile.eof())

{

OutputFile << Data << " ";



InputFile >> Data;

}

}



//----------------------------------------

boolean OpenInputFile(ifstream &InputFile) {

string InputFileName;

cout << "Enter the input file name: ";

cin >> InputFileName;
InputFile.open(InputFileName);

return(!InputFile.fail());

}

//----------------------------------------



boolean OpenOutputFile(ofstream &OutputFile) {

string OutputFileName;

cout << "Enter the output file name: ";

cin >> OutputFileName;


OutputFile.open(OutputFileName);

return(!OutputFile.fail());

}





Download 0.58 Mb.

Share with your friends:
1   ...   5   6   7   8   9   10   11   12   ...   26




The database is protected by copyright ©ininet.org 2024
send message

    Main page