Presentation is loading. Please wait.

Presentation is loading. Please wait.

Text Files.

Similar presentations


Presentation on theme: "Text Files."— Presentation transcript:

1 Text Files

2 #include <iostream. h> #include <fstream
#include <iostream.h> #include <fstream.h> #include <string.h> void main () { string STRING; ifstream infile; infile.open ("names.txt"); while(!infile.eof()) // To get you all the lines. getline(infile,STRING); // Saves the line in STRING. cout<<STRING; // Prints our STRING. } infile.close();

3 // basic file operations #include <iostream
// basic file operations #include <iostream.h> #include <fstream.h> int main () { ofstream myfile; myfile.open ("example.txt"); myfile << "Writing this to a file.\n"; myfile.close(); return 0; }

4 // reading a text file #include <iostream
// reading a text file #include <iostream.h> #include <fstream.h> #include <string.h> int main () { string line; ifstream myfile ("example.txt"); if (myfile.is_open()) { while ( !infile.eof() ) getline (myfile,line); cout << line << endl; } myfile.close(); else cout << "Unable to open file"; return 0;

5 // writing on a text file #include <iostream
// writing on a text file #include <iostream.h> #include <fstream.h> int main () { ofstream myfile ("example.txt"); if (myfile.is_open()) { myfile << "This is a line.\n"; myfile << "This is another line.\n"; myfile.close(); } else cout << "Unable to open file"; return 0;


Download ppt "Text Files."

Similar presentations


Ads by Google