ITS ALL THE WAY AT THE BOTTOM OF MY IMPLEMENTATION FILE.
MAIN
#include <iostream>
#include <fstream>
#include "line_test.h"
using namespace std;
int main()
{
//open up file to be read
string input;
ifstream in;
cout<<"enter file name which needs to be opened\n";
cin>>input;
in.open(input.c_str());
while(!in)
{
in.close();
cout<<"the input textfile failed please reenter the file\n";
in.clear();
cin>>input;
in.open(input.c_str());
}
cout<<"the file opened\n";
//open up file to copy to
string output;
ofstream out;
cout<<"enter file name which to copy to\n";
cin>>output;
out.open(output.c_str());
while(!out)
{
out.close();
cout<<"output file opening failed. Please reenter output file name\n";
out.clear();
cin>>output;
out.open(output.c_str());
}
//processing of file
line_test a;
out<<a.getfirst3(in);
//string inputline;
while(!in.eof())
{
a.iscomment(in,out);
a.islabel(in,out);
a.isequal(in,out);
a.isvalidname(in,out);
}
in.close();
out.close();
return 0;
}
IMPLEMENTATION
#include <iostream>
#include <fstream>
#include "line_test.h"
using namespace std;
//retrieve first 3 lines
string line_test::getfirst3(ifstream& input)
{
string newline="\n";
getline(input,title_comments);
getline(input,title_comments1);
getline(input,title_comments2);
title_comments=title_comments+newline;
title_comments1=title_comments1+newline;
title_comments2=title_comments2+newline;
first_three_lines=title_comments+title_comments1+title_comments2;
return first_three_lines;
}
//function used to check if there are comments
void line_test::iscomment(ifstream& input,ofstream& output)
{
getline(input,inputline);
inputline2=inputline;
if(inputline.find('#')==0)
{
int length=inputline.length();
inputline.erase(0,length);
inputline2.erase(0,length);
input.ignore(0,length);
}
else
{
//output<<inputline<<"\n";
}
}
//checks if data is labeled correctly
void line_test::islabel(ifstream& input,ofstream& output)
{
if(inputline.find("name")==0)
{
inputline.erase(0,4);
input.ignore(0,4);
//output<<inputline<<"\n";
//cout<<"name"<<inputline2<<"\n";
}
else if(inputline.find("ID")==0)
{
inputline.erase(0,2);
input.ignore(0,2);
//output<<inputline<<"\n";
//cout<<"id"<<inputline2<<"\n";
}
else if(inputline.find("GPA")==0)
{
inputline.erase(0,3);
input.ignore(0,3);
//output<<inputline<<"\n";
//cout<<"gpa"<<inputline2<<"\n";
}
else if(inputline.find("gender")==0)
{
inputline.erase(0,6);
input.ignore(0,6);
//output<<inputline<<"\n";
//cout<<"gender"<<inputline2<<"\n";
}
else
{
int length=inputline.length();
inputline.erase(0,length);
//inputline2.erase(0,length);
input.ignore(0,length);
//output<<"";
}
}
//check if their an equal sign
void line_test::isequal(ifstream& input,ofstream& output)
{
while(inputline.find(' ')==0||inputline.find('=')==0) //I changed inputline2 to inputline and it worked fine.
{
inputline.erase(0,1);
input.ignore(0,1);
}
output<<inputline<<"\n";
}
void line_test::isvalidname(ifstream& input,ofstream& output)
{
if(input>>first_name>>ws>>last_name)
{
output<<"name "<<inputline2<<"\n";//"name "<<inputline2<<"\n";
}
else if(input>>idnumber)
{
output<<"id "<<inputline2<<"\n";//"id "<<inputline2<<"\n";
}
else if(input>>gpa)
{
output<<"gpa "<<input<<inputline2;//"gpa "<<inputline2<<"\n";
}
else if(input>>gender)
{
output<<"gender "<<inputline2<<"\n";//"gender "<<inputline2<<"\n";
}
else
{
output<<"none"<<"\n";
}
}
INTERFACE
#ifndef LINE_TEST_H_INCLUDED
#define LINE_TEST_H_INCLUDED
#include <fstream>
#include <iostream>
using namespace std;
class line_test
{
public:
//void iscomment(ifstream& input,ofstream& output);
//string inputline;
//string inputline1;
//void islabel(ifstream& input,ofstream& output);
//first line of file
string title_comments;
//second line of file
string title_comments1;
//third line of file
string title_comments2;
//first three lines of code
string first_three_lines;
string getfirst3(ifstream& input);
void iscomment(ifstream& input,ofstream& output);
void islabel(ifstream& input,ofstream& output);
void isequal(ifstream& input,ofstream& output);
void isvalidname(ifstream& input,ofstream& output);
string inputline;
//string inputline3;
string inputline2;
string first_name;
char ws;
string last_name;
long idnumber;
double gpa;
char gender;
};
#endif // LINE_TEST_H_INCLUDED