With this code and the attached input file, I was able to get the output below:
CODE
#include <iostream>
#include <fstream>
#include <cstdlib>
#include <string>
using namespace std;
int main()
{
string filename = "A5.txt";
string filename2 = "A5N.txt";
//char ch;
ifstream inFile;
ofstream outFile;
string line, name, address;
char temp[256]; //temp char array for strtod
int age, Socialnum;
inFile.open(filename.c_str(),ios::in);
if(inFile.is_open()){
while (!inFile.eof()) {
getline(inFile,name);
inFile.getline(temp,256);
age = (int)strtod(temp,NULL); //string to double and casting to int
inFile.getline(temp,256);
Socialnum = (int)strtod(temp,NULL);
getline(inFile,address);
cout << name << endl << age << endl << Socialnum << endl << address << endl;
}
}
else{
cout << "File Could not be opened!";
}
try
{
outFile.open(filename2.c_str(),ios::trunc);
if (outFile.fail()) throw filename;
outFile<<"Hello World!";
inFile.close();
outFile.close();
}
catch (string in)
{
cout<<"The input file"<<in<<"was not successfully opened."<<endl;
cout<<"The new file wasn't made";
exit(1);
}
cout<<"A successful backup of "<<filename<<" named "<<filename2<<" was successfully made."<<endl;
cin.ignore();cin.ignore();
return 0;
}
CODE
April,Joe
21
312033387
126 Freelane, Yourtown, State 88990
John, Doe
43
321353135
177 Broadway, New York City, New York 13203
A successful backup of A5.txt named A5N.txt was successfully made.
Press any key to continue
I hope you will get further with the changes in the file input code :)