Code:
#include<fstream>
#include<iostream>
using namespace std;
int main()
{
char data[100];
ofstream outfile;
outfile.open("file.dat");
cout<<"writing to the file\n";
cout<<"Enter the name:\n ";
cin.getline(data,100);
outfile<<data<<"\n";
cout<<"\nEnter the age:\n";
cin.getline(data,100);
cin.ignore();
outfile<<data<<"\n";
outfile.close();
ifstream infile;
infile.open("file.dat");
cout<<"Reading from file\n";
infile>>data;
cout<<data<<"\n";
infile>>data;
cout<<data<<"\n";
infile.close();
return 0;
}
The provided code is a C++ program that demonstrates basic file input/output operations. Here's a breakdown of what the code does:
It includes the necessary header files for file input/output operations (<fstream>) and for standard input/output (<iostream>).
In the main() function:
It declares a character array data of size 100 to store input data.
It declares an ofstream object named outfile for writing to a file.
It opens a file named "file.dat" for writing.
It prompts the user to enter a name, reads it using cin.getline() (which reads a line of text including whitespaces), and writes it to the file.
It prompts the user to enter an age, reads it using cin.getline(), and writes it to the file.
It closes the output file stream.
It then declares an ifstream object named infile for reading from a file.
It opens the same file "file.dat" for reading.
It displays a message indicating that it's reading from the file.
It reads the name from the file using infile >> data (which reads until whitespace is encountered), and then outputs it to the console.
It reads the age from the file using infile >> data, and then outputs it to the console.
Finally, it closes the input file stream and returns 0, indicating successful execution.
In summary, this code prompts the user to input a name and an age, writes them to a file named "file.dat", reads the data back from the file, and then displays it on the console.
Algorithm:
- Include necessary header files: Include <fstream> for file input/output operations and <iostream> for standard input/output.
- Declare main function: Begin with the declaration of the main() function.
- Declare variables: Declare an array data of characters to store input data.
- Create an output file stream: Declare an ofstream object named outfile to write data to a file. Open the file named "file.dat" using outfile.open() in write mode.
- Write data to the file: Prompt the user to enter a name and read it using cin.getline() into the data array. Write the name to the file using outfile<<data<<"\n";. Repeat the process for entering age.
- Close the output file stream: Close the output file stream using outfile.close().
- Create an input file stream: Declare an ifstream object named infile to read data from the file. Open the file named "file.dat" using infile.open() in read mode.
- Read data from the file: Read the name from the file using infile >> data; and display it using cout << data << "\n";. Repeat the process for reading and displaying age.
- Close the input file stream: Close the input file stream using infile.close().
- End of main function: End the main() function and return 0.