============================MODULE19======================================= | | | The program examples' source codes have been arranged in the same | | order that appeared in the Tutorial. This is unedited and unverified | | compilation. Published as is basis for educational, reacretional and | | brain teaser purposes. All trademarks, copyrights and IPs, wherever | | exist, are the sole property of their respective owner and/or | | holder. Any damage or loss by using the materials presented in this | | tutorial is USER responsibility. Part or full distribution, | | reproduction and modification is granted to any body. | | Copyright 2003-2005 © Tenouk, Inc. All rights reserved. | | Distributed through http://www.tenouk.com | | | | | =========================================================================== Originally programs compiled using Borland C++. Examples compiled using VC++/VC++ .Net and gcc or g++ are given at the end of every Module. For example if you want to compile C++ codes using VC++/VC++ .Net, change the header file accordingly. Just need some modification for the header files...: ------------------------------------------------- #include //for system() #include ... { C++ codes... } ------------------------------------------------- should be changed to: ------------------------------------------------- #include //use C++ wrapper to call C functions from C++ programs... #include using namespace std; ... { C++ codes... } ------------------------------------------------- In VC++/VC++ .Net the iostream.h (header with .h) is not valid anymore. It should be C++ header, so that it comply to the standard. In older Borland C++ compiler this still works, but not proper any more... and for standard C/C++ the portability should be no problem or better you read Module23 at http://www.tenouk.com/Module23.html to get the big picture...For C codes, they still C codes :o) ========================================================================= ========================================================================= ---------------------------sampleread.txt----------------------------- This is file sampleread.txt. This file will be opened for reading then its content will be written to another file and standard output i.e screen/console...after you have executed this C++ program, without error....this text should be output on your screen as well as written to the samplewrite.txt file. Don't forget to check the content of the samplewrite.txt. -------------------------------------------------------------------------------------------- //Reading from available file content //then writing the content to another //file. Firstly, create file for reading (can include path) //let says "C:\sampleread.txt", at root on C drive. //Type some text as shown… //Then executes this program. For VC++/VC++ .Net or other C++ standard compliance compiler //change the header files accordingly... //#include //#include //#include //using namespace std; #include #include #include //function definition, to open file for reading... void openinfile(ifstream &infile) { char filename[100]; cout<<"Enter the file name: "; //Enter the filename that you have created //(can include path). From the comment above //you have to enter "C:\sampleread.txt" //without the double quotes. cin>>filename; infile.open(filename); } void main(void) { //declare the input file stream ifstream inputfile; //declare the output file stream ofstream outputfile; char chs; //function call for opening file for reading... openinfile(inputfile); //create, if not exist and open it for writing outputfile.open("C:\\samplewrite.txt"); //test until the end of file while (!inputfile.eof()) { //read character until end of file inputfile.get(chs); if (!inputfile.eof()) { //output character by character (byte) on screen, standard output cout< #include #include void main(void) { char filename[50]; ifstream inputfile; char FirstLine[50]; char SecondLine[50]; char ThirdLine[50]; //prompt user for file name to be opened... cout<<"Enter the filename to be opened: "; cin>>filename; //test open file for reading... inputfile.open(filename); //if not the end of file, do... if(!inputfile.eof()) { cout<<"\nThe first line of text is: \n"; inputfile.getline(FirstLine, 50); cout< #include #include void main(void) { char filename[] = "C:\\testfileio.txt"; ifstream inputfile; inputfile.open(filename, ios::in); //test if fail to open fail for reading, do… if(inputfile.fail()) { cout<<"Opening "< #include #include void main(void) { char filename[100]; ifstream inputfile; //Prompting user for filename to be opened… //including the full path if necessary… //e.g. c:\testfileio.txt, c:\Windows\Temp\testfile.txt etc cout<<"Enter the file name to be opened: "; //store at an array filename... //Array without [] is a pointer to the //first array’s element... cin>>filename; //opened the file for input... inputfile.open(filename, ios::in); //test if fail to open file for reading, do… if(inputfile.fail()) { cout<<"Opening "< #include #include void main(void) { char filename[] = "C:\\testfileio1.txt"; ifstream inputfile; //-------opening input file for reading-------- inputfile.open(filename, ios::in); //test if fail to open the file, do… //error handling for file opening if(inputfile.fail()) { cout<<"Opening file "<>price; //test, if end of file not found, do the following... while(!inputfile.eof()) { //total = total + price total += price; count++; cout<<"Item price # "<>price; } cout<<"The total price for "< #include #include void main(void) { char filename[] = "C:\\testfileio2.txt"; ofstream outputfile; //----creating, opening and writing/appending data to file----- outputfile.open(filename, ios::out|ios::app); //simple error handling for file creating/opening for writing //test if fail to open the file, do… if(outputfile.fail()) { cout<<"Creating and opening file "< #include #include void main(void) { char filename[] = "C:\\testfileio3.txt"; fstream inputfile, outputfile; //--------create, open and write data to file-------- outputfile.open(filename, ios::out); //----write some text------- outputfile<<"This is just line of text."< #include #include void main(void) { char filename[] = "C:\\testfileio4.txt"; ofstream outputfile; //----creating, opening and writing data to file----- outputfile.open(filename, ios::out); //simple error handling for file creating/opening //test if fail to open, do... if(outputfile.fail()) { cout<<"Creating and opening "< #include int main () { char firstword, secondword; cout<<"Enter your first and last names: "; firstword = cin.get(); cin.ignore(30,' '); secondword = cin.get(); cout<<"The initials letters are: "< #include using namespace std; //function definition, to open file for reading... void openinfile(ifstream &infile) { char filename[100]; cout<<"Enter the file name: "; //Enter the filename that you have created //(can include path). From the comment above //you have to enter "C:\sampleread.txt" //without the double quotes. cin>>filename; infile.open(filename); } void main(void) { //declare the input file stream ifstream inputfile; //declare the output file stream ofstream outputfile; char chs; //function call for opening file for reading... openinfile(inputfile); //create, if does not exist and open it for writing outputfile.open("C:\\samplewrite.txt"); //test until the end of file while (!inputfile.eof()) { //read character until end of file inputfile.get(chs); if (!inputfile.eof()) { //output character by character (byte) on screen, standard output cout< #include using namespace std; int main(void) { char filename[] = "testfileio.txt"; ifstream inputfile; //-------opening input file for reading-------- inputfile.open(filename, ios::in); //test if fail to open the file, do... //error handling for file opening if(inputfile.fail()) { cout<<"Opening file "<>price; //test, if end of file not found, do the following... while(!inputfile.eof()) { //total = total + price total += price; count++; cout<<"Item price # "<>price; } cout<<"The total price for "<