#include <iostream>
#include <fstream>
#include <string>
usingnamespace std;
int main()
{
constchar filename[] = "data.txt";
int i = 123;
string text = "Hello";
ofstream outfile(filename); // open file for writing
if (outfile)
{
outfile << i << " " << text; // output something
}
outfile.close(); // we finished writing, so close it.
i = 0; // clear the data, to prove it works
text = "";
ifstream infile(filename); // open file for reading
if (infile)
{
infile >> i; // read the data from file
infile >> text;
}
infile.close();
// display the result
cout << " i: " << i << " text: " << text << endl;
return 0;
}
#include <iostream>
#include <fstream>
#include <string>
#include <conio.h>
usingnamespace std;
int i = 123;
string text = "Heloo";
int main()
{
char name[100];
cout<<"Enter the file name [ex = file.txt]: ";
fflush(stdin);gets(name);
ofstream outfile(name); // open file for writing
if (outfile)
{
outfile << i << " " << text; // output something
}
outfile.close(); // we finished writing, so close it.
i = 0; // clear the data, to prove it works
text = "";
ifstream infile(name); // open file for reading
if (infile)
{
infile >> i; // read the data from file
infile >> text;
}
infile.close();
// display the result
cout << " i: " << i << " text: " << text << endl;
getch();
}
Enter the file name [ex = file.txt]: test.txt
i=123 text=Heloo