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 :: c++ writing to file 
Cpp :: print linkedstack cpp 
Cpp :: vs code text in line 
Cpp :: initialize a pair 
Cpp :: replace character in a string c++ stack overflow 
Cpp :: tostring c++ 
Cpp :: c++ remove last element from vector 
Cpp :: creator of C++ 
Cpp :: how to speed up cin and cout 
Cpp :: access last element in vector in c++ 
Cpp :: fibonacci in c++ 
Cpp :: 2d vector initialization in cpp 
Cpp :: if not defined c++ 
Cpp :: Area of a Circle in C++ Programming 
Cpp :: #pragma once in main file what is it for 
Cpp :: c++ vector add only unique elements 
Cpp :: c++ check if file exits 
Cpp :: how to make a hello world program in c++ 
Cpp :: c++ main function 
Cpp :: c++ fibonacci 
Cpp :: c++ lcm 
Cpp :: what is the short cut way to find the max and min element in an array in c++ 
Cpp :: c++ merge sort 
Cpp :: copy array c++ 
Cpp :: http.begin not working 
Cpp :: c++ printf char as hex 
Cpp :: rand c++ 
Cpp :: fstring from float c++ ue4 
Cpp :: cpp Sieve algorithm 
Cpp :: c++ programming language 
ADD CONTENT
Topic
Content
Source link
Name
6+1 =