Search
 
SCRIPT & CODE EXAMPLE
 

CPP

c++ files

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

int main () {
  ofstream myfile;
  myfile.open ("example.txt");
  myfile << "Writing this to a file.
";
  myfile.close();
  return 0;
}
Comment

c++ open file

// Reading a file

#include <iostream>
#include <fstream>
#include <string>

int main() 
{
    std::string line;

    std::ifstream file("example.txt");
    if(file.is_open())
    {
        while(getline(file, line))
        {
            std::cout << line << '
';
        }
        file.close();
    }
    else std::cout << "Unable to open file";

    return 0;
}
Comment

file c++

#include <iostream>
#include <string>
#include <fstream> //write and read
//#include <ifstream> //read
//#include <ofstream> //write

int main () {
  std::string line;
  std::ofstream myfileWrite;
  std::ifstream myfileRead;
  myfileWrite.open("example.txt");
  myfileRead.open("example.txt");
  myfileWrite << "Writing this to a file.
";
  while (getline(myfileRead,line)){
    std::cout << line << '
';
  }
  myfileWrite.close();
  myfileRead.close();
  return 0;
}
Comment

C++ files

#include <fstream> 

/*
ofstream 	Creates and writes to files
ifstream 	Reads from files
fstream 	A combination of ofstream and ifstream: creates, reads, and writes to files
*/

int main() {
  // Create and open a text file
  ofstream MyFile("filename.txt");

  // Write to the file
  MyFile << "Files can be tricky, but it is fun enough!";

  // Close the file
  MyFile.close();
} 
Comment

files in c++

#include <iostream>
#include <fstream>
using namespace std;
class person{
public:
    string wife , son , mother;
    int brothers_num , sisters_num;
};

int main()
{
    person Ahmed;
    Ahmed.wife = "Aya";
    Ahmed.son = "Badr";
    Ahmed.mother = "Fatma";
    Ahmed.brothers_num = 2;
    Ahmed.sisters_num = 2;
    fstream new_file;
    new_file.open("database.txt" , ios::out );
    if(new_file.fail()){
        cout<<"can't open the files
";
    }
    new_file.write((char*)&Ahmed , sizeof(Ahmed));
    new_file.close();

    person Khaled;
    new_file.open("database.txt" , ios::in);
    new_file.read((char*)&Khaled , sizeof(Khaled));
    new_file.close();

    cout<<Khaled.wife;

    return 0;
}
Comment

C++ Files

#include <iostream>
#include <fstream>
Comment

files c++

fstream  afile;
afile.open("file.dat", ios::out | ios::in );
Comment

PREVIOUS NEXT
Code Example
Cpp :: largest subarray with zero sum 
Cpp :: Maximum Pairwise Modular Sum codechef solution in c++ 
Cpp :: function for reversing an array c++ stl 
Cpp :: heapsort 
Cpp :: Write a C++ program to Computing Mean and Median Using Arrays 
Cpp :: lap trinh file explorer c++ 
Cpp :: how to scan vector in c++ 
Cpp :: how to read rotary encoder c++ 
Cpp :: how to use printf with <cinttypes c++ 
Cpp :: full pyramid in c++ 
Cpp :: point in polygon 
Cpp :: Mirror Inverse Program in c++ 
Cpp :: nand in cpp 
Cpp :: how to seek to the start of afile in c++ 
Cpp :: std::is_standard_layout 
Cpp :: coin change top-down 
Cpp :: pimpl c++ 
Cpp :: qpushbutton clicked connect c++ 
Cpp :: library management system project in c++ using array 
Cpp :: powershell script query mssql windows authentication 
Cpp :: vermífugo significado 
Cpp :: std::ifstream cant read file to large 
Cpp :: how to get max grade c++ 
Cpp :: how to check private messages on reddit 
Cpp :: pop back innstring 
Cpp :: Snake Procession codechef solution in c++ 
Cpp :: days in a year c++ 
Cpp :: c+ - Dormir en millisecondes 
Cpp :: python Difference Array | Range update query in O(1) 
Cpp :: split the array there is an array val of n integers . A good subarray is defined as 
ADD CONTENT
Topic
Content
Source link
Name
2+3 =