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

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 :: integer max value c++ 
Cpp :: tabeau pseudo dynamique sur c++ 
Cpp :: c++ include < vs "" 
Cpp :: find nth fibonacci number 
Cpp :: Accessing C++ Array Elements 
Cpp :: how to run cpp using gcc vscode 
Cpp :: cpp vector popback 
Cpp :: C++ Vector Operation Access Elements 
Cpp :: how to convert char to int in c++ 
Cpp :: c++ program to find gcd of 3 numbers 
Cpp :: free a pointer c++ 
Cpp :: copy vector c++ 
Cpp :: how to take input in 2d vector in c++ 
Cpp :: memset function in c++ 
Cpp :: How to see gateway on linux 
Cpp :: c++ char 
Cpp :: is there garbage collection in c++ 
Cpp :: c++ overloading by ref-qualifiers 
Cpp :: rc.local not running centos 6 
Cpp :: c++ Closest Pair of Points | O(nlogn) Implementation 
Cpp :: how to run cpp in visual studio 
Cpp :: c++ to c converter tool 
Cpp :: #include <iostream #include <stdio.h using namespace std; int main() { int a[5]; a[0]=12; a[1]=13; a[2]=14; a[3]=15; 
Cpp :: windows servis from console app 
Cpp :: pallindrome string 
Cpp :: how to measure cpp code performace 
Cpp :: c++ check if cin got the wrong type 
Cpp :: lru cache gfg 
Cpp :: 1822. Sign of the Product of an Array leetcode 
Cpp :: what type is this c++ 
ADD CONTENT
Topic
Content
Source link
Name
1+6 =