Arenlor
June 29th, 2008, 09:01 PM
This program is quite simple, it reads in part of a given file and after creating a new entry rewrites the file with the new entry. It works great up until 4/5 entries when it stops writing it correctly.
#include <iostream>
#include <fstream>
#include <string>
#include <new>
using namespace std;
ifstream fin;
ofstream fout;
int f_exists;
struct human{
string first_name;
string last_name;
};
human create (void) {
human person;
if(f_exists!=1)cin.ignore();
cout << "What is their first name: ";
getline(cin,person.first_name);
cout << "What is their last name: ";
getline(cin,person.last_name);
return(person);
}
int verify(human hu){
char valid;
while(valid != 'y' && valid != 'Y'){
cout << "First name: " << hu.first_name << endl;
cout << "Correct so far? (y/n) ";
cin >> valid;
if(valid == 'n' || valid == 'N'){
return(0);
}
}
do{
cout << "Last name: " << hu.last_name << endl;
cout << "Correct so far? (y/n) ";
cin >> valid;
if(valid == 'n' || valid == 'N'){
return(0);
}
}while(valid != 'y' && valid != 'Y');
return(1);
}
void write(human hu){
fout.write("\n\t<person>\n",11);
fout.write("\t\t<name>\n",9);
fout.write("\t\t\t<first_name>",15);
fout.write(hu.first_name.c_str(),hu.first_name.length());
fout.write("</first_name>\n",14);
fout.write("\t\t\t<last_name>",14);
fout.write(hu.last_name.c_str(),hu.last_name.length());
fout.write("</last_name>\n",13);
fout.write("\t\t</name>\n",10);
fout.write("\t</person>",10);
}
int main (void){
string line;
int nl = 0;
char f_conf,start;
char * file;
int size,correct;
string header,footer,f_name;
human new_entry;
while(start != 'y' && start != 'Y'){
cout << "If you don't finish this your file will be destroyed.\nI'm serious, the file will go bye-bye so make sure you want to run it first.\nDo you want to start? (y/n) ";
cin >> start;
if(start == 'n' || start == 'N')return 0;
}
cin.ignore();
header = "<?xml version=\"1.0\" encoding=\"UFT-8\"?>\n<!DOCTYPE contacts SYSTEM \"http://www.arenlor.com/contacts/contacts.dtd\">\n<contacts>\n";
footer = "\n</contacts>";
while(f_conf != 'y' && f_conf != 'Y' && f_conf != 'n' && f_conf != 'N'){
cout << "Do you already have a file? (y/n)";
cin >> f_conf;
if(f_conf == 'n' || f_conf == 'N') f_name = "contacts.xml";
else if(f_conf == 'y' || f_conf == 'Y'){
cin.ignore();
cout << "What is its name: ";
getline(cin,f_name);
f_exists = 1;
}
}
fin.open(f_name.c_str());
fin.seekg(0,ios::end);
size = fin.tellg();
if(size>141)size -= 141;
fin.seekg(0,ios::beg);
while(getline(fin,line)){
nl++;
}
fin.clear();
nl -= 6;
size -= nl;
file = new char[size];
fin.seekg(127,ios::beg);
fin.readsome(file,size);
fin.close();
fout.open(f_name.c_str());
fout.write(header.c_str(),124);
fout.write(file,size);
delete[] file;
do{
cin.clear();
new_entry = create();
correct = verify(new_entry);
}while(correct == 0);
write(new_entry);
fout.write(footer.c_str(),12);
fout.close();
return 0;
}
An example of how it stops working:
<?xml version="1.0" encoding="UFT-8"?>
<!DOCTYPE contacts SYSTEM "http://www.arenlor.com/contacts/contacts.dtd">
<contacts>
<person>
<name>
<first_name>Arenlor</first_name>
<last_name>BloodLeaf</last_name>
</name>
</person>
<person>
<name>
<first_name>Dove</first_name>
<last_name>Ashby</last_name>
</name>
</person>
<person>
<name>
<first_name>Jordan</first_name>
<last_name>Morris</last_name>
</name>
</person>
<person>
<name>
<first_name>Nanci</first_name>
<last_name>Lycett</last_name>
</name>
</person>
<person>
<name>
<first_name>Faith</first_name>
<last_name>Kelly</last_name>
</name>
</person>
</contacts>
<?xml version="1.0" encoding="UFT-8"?>
<!DOCTYPE contacts SYSTEM "http://www.arenlor.com/contacts/contacts.dtd">
<contacts>
<person>
<name>
<first_name>Arenlor</first_name>
<last_name>BloodLeaf</last_name>
</name>
</person>
<person>
<name>
<first_name>Dove</first_name>
<last_name>Ashby</last_name>
</name>
</person>
<person>
<name>
<first_name>Jordan</first_name>
<last_name>Morris</last_name>
</name>
</person>
<person>
<name>
<first_name>Nanci</first_name>
<last_name>Lycett</last_name>
</name>
</person>
<person>
<name>
<first_name>Faith</first_name>
<last_name>Kelly</last
<last_name>Kelly</last
<person>
<name>
<first_name>Jerod</first_name>
<last_name>Lycett</last_name>
</name>
</person>
</contacts>
This is how it always breaks where it stops at the _ in last_name, creates two newlines, repeats the broken last_name line and then correctly adds the new entry. I've tried stepping through Dev-C++'s debugger with no results of where it's going wrong.
#include <iostream>
#include <fstream>
#include <string>
#include <new>
using namespace std;
ifstream fin;
ofstream fout;
int f_exists;
struct human{
string first_name;
string last_name;
};
human create (void) {
human person;
if(f_exists!=1)cin.ignore();
cout << "What is their first name: ";
getline(cin,person.first_name);
cout << "What is their last name: ";
getline(cin,person.last_name);
return(person);
}
int verify(human hu){
char valid;
while(valid != 'y' && valid != 'Y'){
cout << "First name: " << hu.first_name << endl;
cout << "Correct so far? (y/n) ";
cin >> valid;
if(valid == 'n' || valid == 'N'){
return(0);
}
}
do{
cout << "Last name: " << hu.last_name << endl;
cout << "Correct so far? (y/n) ";
cin >> valid;
if(valid == 'n' || valid == 'N'){
return(0);
}
}while(valid != 'y' && valid != 'Y');
return(1);
}
void write(human hu){
fout.write("\n\t<person>\n",11);
fout.write("\t\t<name>\n",9);
fout.write("\t\t\t<first_name>",15);
fout.write(hu.first_name.c_str(),hu.first_name.length());
fout.write("</first_name>\n",14);
fout.write("\t\t\t<last_name>",14);
fout.write(hu.last_name.c_str(),hu.last_name.length());
fout.write("</last_name>\n",13);
fout.write("\t\t</name>\n",10);
fout.write("\t</person>",10);
}
int main (void){
string line;
int nl = 0;
char f_conf,start;
char * file;
int size,correct;
string header,footer,f_name;
human new_entry;
while(start != 'y' && start != 'Y'){
cout << "If you don't finish this your file will be destroyed.\nI'm serious, the file will go bye-bye so make sure you want to run it first.\nDo you want to start? (y/n) ";
cin >> start;
if(start == 'n' || start == 'N')return 0;
}
cin.ignore();
header = "<?xml version=\"1.0\" encoding=\"UFT-8\"?>\n<!DOCTYPE contacts SYSTEM \"http://www.arenlor.com/contacts/contacts.dtd\">\n<contacts>\n";
footer = "\n</contacts>";
while(f_conf != 'y' && f_conf != 'Y' && f_conf != 'n' && f_conf != 'N'){
cout << "Do you already have a file? (y/n)";
cin >> f_conf;
if(f_conf == 'n' || f_conf == 'N') f_name = "contacts.xml";
else if(f_conf == 'y' || f_conf == 'Y'){
cin.ignore();
cout << "What is its name: ";
getline(cin,f_name);
f_exists = 1;
}
}
fin.open(f_name.c_str());
fin.seekg(0,ios::end);
size = fin.tellg();
if(size>141)size -= 141;
fin.seekg(0,ios::beg);
while(getline(fin,line)){
nl++;
}
fin.clear();
nl -= 6;
size -= nl;
file = new char[size];
fin.seekg(127,ios::beg);
fin.readsome(file,size);
fin.close();
fout.open(f_name.c_str());
fout.write(header.c_str(),124);
fout.write(file,size);
delete[] file;
do{
cin.clear();
new_entry = create();
correct = verify(new_entry);
}while(correct == 0);
write(new_entry);
fout.write(footer.c_str(),12);
fout.close();
return 0;
}
An example of how it stops working:
<?xml version="1.0" encoding="UFT-8"?>
<!DOCTYPE contacts SYSTEM "http://www.arenlor.com/contacts/contacts.dtd">
<contacts>
<person>
<name>
<first_name>Arenlor</first_name>
<last_name>BloodLeaf</last_name>
</name>
</person>
<person>
<name>
<first_name>Dove</first_name>
<last_name>Ashby</last_name>
</name>
</person>
<person>
<name>
<first_name>Jordan</first_name>
<last_name>Morris</last_name>
</name>
</person>
<person>
<name>
<first_name>Nanci</first_name>
<last_name>Lycett</last_name>
</name>
</person>
<person>
<name>
<first_name>Faith</first_name>
<last_name>Kelly</last_name>
</name>
</person>
</contacts>
<?xml version="1.0" encoding="UFT-8"?>
<!DOCTYPE contacts SYSTEM "http://www.arenlor.com/contacts/contacts.dtd">
<contacts>
<person>
<name>
<first_name>Arenlor</first_name>
<last_name>BloodLeaf</last_name>
</name>
</person>
<person>
<name>
<first_name>Dove</first_name>
<last_name>Ashby</last_name>
</name>
</person>
<person>
<name>
<first_name>Jordan</first_name>
<last_name>Morris</last_name>
</name>
</person>
<person>
<name>
<first_name>Nanci</first_name>
<last_name>Lycett</last_name>
</name>
</person>
<person>
<name>
<first_name>Faith</first_name>
<last_name>Kelly</last
<last_name>Kelly</last
<person>
<name>
<first_name>Jerod</first_name>
<last_name>Lycett</last_name>
</name>
</person>
</contacts>
This is how it always breaks where it stops at the _ in last_name, creates two newlines, repeats the broken last_name line and then correctly adds the new entry. I've tried stepping through Dev-C++'s debugger with no results of where it's going wrong.