Vous êtes sur la page 1sur 3

#include<iostream>

#include<conio.h>
#include<string>
#include<fstream>
#include<vector>
#include<iomanip>
using namespace std;

void main()
{
/* vector<double> condt;
vector<double> col;
vector<double> ambt;
vector<double> windv; */
string line, file;
vector<string> DataArray;
vector<string> myline;
vector<double>condt;
vector<double>col;
// vector<string> QueryArray;
double condtemp, ctwo;

//--------------------------Open and write


file---------------------------------

cout<<"Enter the name of file"<<endl;


cin>>file;
ofstream out((file+".dat").c_str());
if(!out)
{
cerr<<"File could not be opened!"<<endl;
_getch();
exit(1);
}
else
{
cout<<"File opened successfully"<<endl;
}
out<<"How the fuck are you?"<<endl;
out<<"I dont know"<<endl;
out<<"I am fine, I guess"<<endl;
out<<right<<setw(10)<<"X"<<right<<setw(10)<<"Z"<<endl;
out<<right<<setw(10)<<"10"<<right<<setw(10)<<"20"<<endl;
out<<right<<setw(10)<<"30"<<right<<setw(10)<<"40"<<endl;
out.close();
out.clear();
cout<<"File written successfully"<<endl;
system("pause");

//---------------------------------Read files and put them into


vectors----------------------------------

cout<<"Opening the file to put lines into array"<<endl;


system("pause");
ifstream in((file+".dat").c_str());
if(!in)
{
cout<<"File could not be opened"<<endl;
_getch();
exit(1);
}
for(int i=1; i<=6; i++)
{
if(i<5)
{
in.ignore(256,'\n');
}
else
{
in>>condtemp;
condt.push_back(condtemp);
in>>ctwo;
col.push_back(ctwo);
// ind>>cthree;
// ambt.push_back(cthree);
// ind>>cfour;
// windv.push_back(cfour);
}
}
for(size_t i=0; i<condt.size(); i++)
{
cout<<"Element in condt and col"<<endl;
cout<<condt[i]<<" "<<col[i]<<endl;
}
system("pause");
in.close();
in.clear();
cout<<"File closed successfully"<<endl;
system("pause");
cout<<"Again opening the file to read into vector strings"<<endl;
system("pause");
in.open((file+".dat").c_str());
if(!in)
{
cout<<"File could not be opened"<<endl;
_getch();
exit(1);
}
while (getline(in, line))
{
DataArray.push_back(line);
}
cout<<"The 5th element is"<<endl;
cout<<DataArray[5]<<std::endl;
for(size_t i=0; i<DataArray.size(); i++)
{
cout<<DataArray[i]<<endl;
}
in.close();
in.clear();
cout<<"File closed successfully"<<endl;
system("pause");

//--------------------------------Get lines to be inserted by creating the


insert vector-----------------------------
cout<<"Enter the line you want to insert"<<endl;
cin.ignore(256,'\n');
getline(cin,line);
cout<<"You wrote - "<<endl;
cout<<line<<endl;
system("pause");
myline.push_back(line);
cout<<"Enter another line you want to insert"<<endl;
// cin.ignore(256,'\n'); Remember: When you have already used getline in
preceding line then you do not need to use ignore statement.
getline(cin,line);
cout<<"You wrote - "<<endl;
cout<<line<<endl;
system("pause");
myline.push_back(line);

// -------------------------Now insert the insert vector into the file


vector------------------------------------

vector<string>::iterator it;
it = DataArray.begin();
// DataArray.insert(it+1,line);
DataArray.insert(it+1, myline.begin(), myline.end());
cout<<"Line(s) added to vector string"<<endl;
system("pause");
for(size_t i=0; i<DataArray.size(); i++)
{
cout<<DataArray[i]<<endl;
}

//----------------------Update the orginal


file-----------------------------------

cout<<"Now we will update "<<file<<".dat"<<endl;


system("pause");
out.open((file+".dat").c_str());
if(!out)
{
cerr<<"File could not be opened!"<<endl;
_getch();
exit(1);
}
else
{
cout<<"File opened successfully"<<endl;
}
for(size_t i=0; i<DataArray.size(); i++)
{
out<<DataArray[i]<<endl;
}
cout<<"Your file is updated"<<endl;
system("pause");
}

Vous aimerez peut-être aussi