Skip to main content

File Handling Code

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:

  1. Include necessary header files: Include <fstream> for file input/output operations and <iostream> for standard input/output.
  2. Declare main function: Begin with the declaration of the main() function.
  3. Declare variables: Declare an array data of characters to store input data.
  4. 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.
  5. 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.
  6. Close the output file stream: Close the output file stream using outfile.close().
  7. 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.
  8. 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.
  9. Close the input file stream: Close the input file stream using infile.close().
  10. End of main function: End the main() function and return 0.




Popular posts from this blog

krushnaaa

  import java . awt .* ; import java . awt . event .* ; public class SimpleAWTExample {     public static void main ( String [] args ) {         // Create a Frame         Frame frame = new Frame ( "Simple AWT Example" );         // Create a Button         Button button = new Button ( "Click Me!" );         // Set layout for the Frame         frame . setLayout ( new FlowLayout ());         // Add the Button to the Frame         frame . add ( button );         // Set size of the Frame         frame . setSize ( 300 , 200 ); // Width: 300 pixels, Height: 200 pixels         // Make the Frame visible         frame . setVisible ( true );         // Add a WindowListener to handle closing event     ...

Write a program to create a class Student2 along with two method getData (), printData () to get the value through argument and display the data in printData. Create the two objects s1, s2 to declare and access the values from class STtest.

 CODE: import java.util.Scanner; class Student2 {     private String name;     private int age;          // Method to set data     public void getData(String name, int age) {         this.name = name;         this.age = age;     }          // Method to print data     public void printData() {         System.out.println("Name: " + name);         System.out.println("Age: " + age);     } } public class STtest {     public static void main(String[] args) {         Scanner scanner = new Scanner(System.in);                  // Creating objects of Student2 class         Student2 s1 = new Student2();         Student2 s2 = new Student2();              ...

Create an in-order threaded binary search tree and perform the traversals.(Double Thread)

 CODE: #include <iostream> using namespace std; struct Node {     int data;     Node* left;     Node* right;     bool leftThread;     bool rightThread; }; Node* createThreadedBST(int arr[], int n) {     Node* root = nullptr;     for (int i = 0; i < n; ++i) {         Node* newNode = new Node;         newNode->data = arr[i];         newNode->left = newNode->right = nullptr;         newNode->leftThread = newNode->rightThread = true;         if (root == nullptr) {             root = newNode;         } else {             Node* current = root;             Node* parent = nullptr;             while (true) {                ...