Answers for "file handing in c++"

C++
17

file handling in c++

// basic file operations
#include <iostream>
#include <fstream>
using namespace std;

int main () {
  ofstream myfile;
  myfile.open ("example.txt");
  myfile << "Writing this to a file.\n";
  myfile.close();
  return 0;
}
Posted by: Guest on November-09-2019
1

file handling in c++

#include <iostream>
#include <fstream>
#include <cstring>
#include <process.h>
using namespace std;
int main()
{
    char name[999]; //Used to store data
    ofstream writeMode; //Created object of ofstream
    writeMode.open("name.dat"); //Opened the file in write mode
    cout<<"******** Writing into file ********"<<endl;
    cout<<"Enter your name: ";
    cin.getline(name, 999); //Accepts string with spaces and after spaces eg ____ ____
    writeMode<<name<<endl; //Putted data inside the file
    cout<<"Enter your age: ";
    cin>>name;
    cin.ignore(); //Wrote because number is accepted :P, may be
    writeMode<<name<<endl; //Again putted data inside the file
    writeMode.close(); //Closed the write mode
    ifstream readMode; //Created object of ifstream
    readMode.open("name.dat"); //Opened the file in read mode.
    cout<<"******** Reading into file ********"<<endl;
    readMode>>name;
    cout<<name<<endl; //Write the data to the screen
    readMode>>name; //again read the data from the file and display it
    cout<<name<<endl;
    readMode.close(); //Closed the read mode
    system("pause");
    return 0;
}
Posted by: Guest on August-18-2021

Browse Popular Code Answers by Language