NOTE : -
THIS CONTENT IS NOT ORIGINAL
31. Brief examples of file I/O
Let's talk about input/output. In C++ that's a very broad subject.
Here is a program that writes to a file:
Here is a program that reads from a file:
Let's talk about input/output. In C++ that's a very broad subject.
Here is a program that writes to a file:
using namespace std;
#include
#include
int main ()
{
fstream f;
f.open("test.txt", ios::out);
f << "This is a text output to a file." << endl;
double a = 345;
f << "A number: " << a << endl;
f.close();
return 0;
}
Content of file test.txt |
| This is a text output to a file. A number: 345 |
Here is a program that reads from a file:
using namespace std;
#include
#include
int main ()
{
fstream f;
char c;
cout << "What's inside the test.txt file" << endl;
cout << endl;
f.open("test.txt", ios::in);
while (! f.eof() )
{
f.get(c); // Or c = f.get()
cout << c;
}
f.close();
return 0;
}
| Output |
| This is a text output to a file. A number: 345 |
BIBILOGRAPHY / REFERENCE : - http://www.4p8.com/eric.brasseur/cppcen.html

No comments:
Post a Comment