You need to use fstream for input / output files. The syntax is simple EX:
#include <fstream>
// ...
std::ofstream flux ; // to open a file in ouput mode
flux.open("myfile.whatever") ;
ios::app for to start to write at the end of the fileOr
ios::trunc for if the file already exist replace deletethe old content and replace by new#include <fstream>
// ...
std::ifstream flux ; // open a file in input mode
flux.open("myfile.whatever") ;
ios::ate- position the file pointer "at the end" for input/outputEx: ifstream flux ;Other actions are possible on your file: append, binary, trunc, etc. You would use the same syntax as above instead we put std::ios::(flags), for instance:
flux.open("myfile.whatever", ios::binary) ;using namespace std ;int input ;ios::outfor output operationios::infor input operationios::binaryfor binary (raw byte) IO operation, instead of character-basedios::appfor to start to write at the end of the fileios::truncfor if the file already exist replace delete the old content and replace by newios::ate- position the file pointer "at the end" for input/output
Ex:
int Apple_Instock ;#include <fstream>
// ...
std::ifstream flux ;
flux.open("myfile.whatever" , ios::binary) ;
int Eat_Apple ;Here is a more complete but simple example.
int Apple ;int main()#include <iostream>
#include <fstream>
using namespace std ;
int input ;
int New_Apple ;
int Apple_Instock ;
int Eat_Apple ;
int Apple ;
int main()
{
bool shouldQuit = false;
New_Apple = 0 ;
Apple_Instock = 0 ;
Eat_Apple = 0 ;
while( !shouldQuit )
{
cout << "------------------------------------- /n";
cout << "1) add some apple " << endl ;
cout << "2) check apple in stock " << endl ;
cout << "3) eat some apple " << endl ;
cout << "4) quit " << endl ;
cout << "------------------------------------- /n";
cin >> input ;
switch (input)
{
case 1 :
{
system("cls") ;
cout << "------------------------------------ /n";
cout << " how much apple do you want to add /n";
cout << "------------------------------------ /n";
cin >> New_Apple ;
ifstream apple_checker ;
apple_checker.open("apple.apl") ;
apple_checker >> Apple_Instock ;
apple_checker.close() ;
Apple = New_Apple + Apple_Instock ;
ofstream apple_adder ;
apple_adder.open("apple.apl") ;
apple_adder << Apple ;
apple_adder.close() ;
cout << "------------------------------------ /n";
cout << New_Apple << " Apple has been added ! /n";
cout << "------------------------------------ /n";
break;
}
case 2 :
{
system("cls") ;
ifstream apple_checker ;
apple_checker.open("apple.apl") ;
apple_checker >> Apple_Instock ;
apple_checker.close() ;
cout << "------------------------------------ /n";
cout << " there is " << Apple_Instock ;
cout << "apple in stock /n" ;
cout << "------------------------------------ /n";
break;
}
case 3 :
{
system("cls") ;
cout << "------------------------------------ /n";
cout << "How many apple do you want to eat /n" ;
cout << "------------------------------------ /n";
cin >> Eat_Apple ;
ifstream apple_checker ;
apple_checker.open("apple.apl") ;
apple_checker >> Apple_Instock ;
apple_checker.close() ;
Apple = Apple_Instock - Eat_Apple ;
ofstream apple_eater ;
apple_eater.open("apple.apl") ;
apple_eater << Apple ;
apple_eater.close() ;
cout << "----------------------------------- /n";
cout << Eat_Apple ;
cout << " Apple has been eated! /n";
cout << "----------------------------------- /n";
cout << Apple << " Apple left in stock /n";
cout << "----------------------------------- /n";
break;
}
case 4 :
{
shouldQuit = true;
break;
}
default :
{
system("cls") ;
cout << "------------------------------------ /n";
cout << " invalide choice ! /n";
cout << "------------------------------------ /n";
break;
}
}
}
return 0;
}
{main:New_Apple = 0 ;Apple_Instock = 0 ;Eat_Apple = 0 ;cout > input ;switch(input){case 1:{system("cls") ;cout > New_Apple ;ifstream apple_checker ;apple_checker.open("apple.apl") ;apple_checker >> Apple_Instock ;apple_checker.close() ;Apple = New_Apple + Apple_Instock ;ofstream apple_adder ;apple_adder.open("apple.apl") ;apple_adder > Apple_Instock ;apple_checker.close() ;cout > Eat_Apple ;ifstream apple_checker ;apple_checker.open("apple.apl") ;apple_checker >> Apple_Instock ;apple_checker.close() ;Apple = Apple_Instock- Eat_Apple ;ofstream apple_eater ;apple_eater.open("apple.apl") ;apple_eater