fstreams into one
good morning everyone.. Pls help.. How Can I combine fstreams or txt.file??
example
KIM
JAMES
--------------------
| Kim |
| James |
| |
| |
| |
| |
--------------------
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62
|
ofstream kim;
string filename;
cout<<"Enter Your Name:"<<endl;
cin>>filename;
kim.open(filename.c_str());
if (!kim)
{
kim << "File could not be opened." << endl;
exit(1);
}
{
kim<<"\n***************"<<endl;
kim<<"* TRAVEL INFO *"<<endl;
kim<<"***************"<<endl;
kim<<"Departure: "<<departure<<endl;
kim<<"Arrival: "<<arrival<<endl;
kim<<"Departure Date: "<<month<<"/"<<day<<"/"<<year<<"("<<days<<")"<<endl;
kim<<"Departure Time: "<<time_departure<<endl;
kim<<"Shipping Lines: "<<ship<<endl;
kim<<"Shiiping Vessel: "<<vessel<<endl;
kim<<"\n******************"<<endl;
kim<<"* PASSENGER INFO *"<<endl;
kim<<"******************"<<endl;
kim<<"List Of Passenger/s: "<<passenger<<endl;
for(int i=1;i<passenger+1;i++)
{
kim<<"\n"<<endl;
kim<<"Here are The Pass Info"<<endl;
kim<<"Name: "<<passengers.fname[i]<<" "<<passengers.lname[i]<<endl;
kim<<"Age: "<<passengers.age<<endl;
kim<<"Gender: "<<passengers.sex[i]<<endl;
kim<<"City: "<<passengers.address[i]<<endl;
}
kim<<"------------------------------------------------------"<<endl;
kim<<"Classification of Car: "<<car<<endl;
kim<<"Number of Student Passenger/s: "<<student<<endl;
kim<<"Number of Senior Passenger/s: "<<senior<<endl;
kim<<"Number of Children (7yrs old - below) Passenger/s: "<<children<<endl;
kim<<"Number of Remaining Passenger: "<<remainpass<<endl;
kim<<"\n******************"<<endl;
kim<<"* PRICE INFO *"<<endl;
kim<<"******************"<<endl;
kim<<"Car price: "<<vehicle_price<<endl;
kim<<"Senior Price: "<<sen<<endl;
kim<<"Student Price: "<<stud<<endl;
kim<<"Children Price: "<<child<<endl;
kim<<"No Discount Passenger Price: "<<pass<<endl;
kim.close();
}
string line;
ifstream kims(filename.c_str());
if (kims.is_open())
{
while ( kims.good() )
{
getline(kims,line);
cout << line << endl;
}
kims.close();
}
|
This is the wrong way to write the loop:
1 2 3 4 5 6
|
while ( kims.good() )
{
getline( kims, line ); // *** this will fail when we try to read past end of file
cout << line << endl;
}
|
This is the right way (check for input failure after attempted input):
while( getline( kims, line ) ) std::cout << line << '\n' ;
Combine files:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
|
#include <iostream>
#include <fstream>
#include <string>
#include <iomanip>
bool cat( std::string file_one, std::string file_two )
{ return bool( std::ofstream( file_one, std::ios_base::app ) << std::ifstream(file_two).rdbuf() ) ; }
int main()
{
const std::string my_file_name = "myfile.txt" ;
{ std::ofstream(my_file_name) << "///////////////////////\n// concatenate files //\n///////////////////////\n\n" ; }
if( cat( my_file_name, __FILE__ ) )
{
std::ifstream myfile(my_file_name) ;
int line_number = 0 ;
std::string line ;
while( std::getline( myfile, line ) ) std::cout << std::setw(2) << ++line_number << ". " << line << '\n' ;
}
}
|
http://coliru.stacked-crooked.com/a/cdb85002a231a184
why after the users input/.. it loops??
and std::setw(2)
doesn't recognize
Topic archived. No new replies allowed.