C++ help
Posted: Sun Apr 06, 2008 10:25 pm
ok, this is a program that is meant to make and save a binary file from a text file, but it makes an empty 600mb+ file... wtf:
//this is just o convert files from txt to binary and save it as. dat
#include <iostream>
#include <fstream>
using namespace std;
const int MAX = 35;
const int DEP = 12;
const int TIT = 5;
const int PH = 4;
const int LI = 10;
struct staff
{
char givenName[MAX];
char surname[MAX];
char title[TIT];
char position[MAX];
char department[DEP];
char roomNum[DEP];
char phoneNum[PH];
char username[LI];
};
int main()
{
ifstream src;
ofstream dest;
staff buffer;
src.open("staff.txt", ios::in);
dest.open("staff.dat", ios::out);
if (src.fail() || dest.fail())
{
cout << "Could not open one of the files for conversion - terminating... " << endl;
return 0;
}
// read and write records
while (!src.eof())
{
src.getline(buffer.givenName, MAX, '\n');
cout <<buffer.givenName << endl; // cout is used for debug, but no output is displayed
src.getline(buffer.surname, MAX, '\n');
cout <<buffer.surname << endl;
src.getline(buffer.roomNum, DEP, '\n');
cout <<buffer.roomNum << endl;
src.getline(buffer.phoneNum, PH, '\n');
cout <<buffer.phoneNum << endl;
src.getline(buffer.title, TIT, ' '); //title is followed by space then is position
cout <<buffer.title << endl;
src.getline(buffer.position, MAX, '\n');
cout <<buffer.position << endl;
src.getline(buffer.department, DEP, '\n');
cout <<buffer.department << endl;
src.getline(buffer.username, LI, '\n');
cout <<buffer.username << endl;
dest.write((char*)&buffer, sizeof(staff));
}
src.close();
dest.close();
return 0;
}
and here is the text file:
Jo
Abrantes
17.212
3872
Dr Manager
ITS
jo
Carole
Alcock
36.220
3884
Dr Associate Professor
Vice Chancellors Unit
carole
Gene
Awyzio
36.205
4090
Ms Director
Media Unit
gene
David
Bomba
39.9
4879
Dr Staffer
Engineering
bomba
Bob
Brown
39.109
3121
Mr Dean
Engineering
bobbrown
I spent at least 3 hours tweaking and whatnot, but still cannot find teh probem
//this is just o convert files from txt to binary and save it as. dat
#include <iostream>
#include <fstream>
using namespace std;
const int MAX = 35;
const int DEP = 12;
const int TIT = 5;
const int PH = 4;
const int LI = 10;
struct staff
{
char givenName[MAX];
char surname[MAX];
char title[TIT];
char position[MAX];
char department[DEP];
char roomNum[DEP];
char phoneNum[PH];
char username[LI];
};
int main()
{
ifstream src;
ofstream dest;
staff buffer;
src.open("staff.txt", ios::in);
dest.open("staff.dat", ios::out);
if (src.fail() || dest.fail())
{
cout << "Could not open one of the files for conversion - terminating... " << endl;
return 0;
}
// read and write records
while (!src.eof())
{
src.getline(buffer.givenName, MAX, '\n');
cout <<buffer.givenName << endl; // cout is used for debug, but no output is displayed
src.getline(buffer.surname, MAX, '\n');
cout <<buffer.surname << endl;
src.getline(buffer.roomNum, DEP, '\n');
cout <<buffer.roomNum << endl;
src.getline(buffer.phoneNum, PH, '\n');
cout <<buffer.phoneNum << endl;
src.getline(buffer.title, TIT, ' '); //title is followed by space then is position
cout <<buffer.title << endl;
src.getline(buffer.position, MAX, '\n');
cout <<buffer.position << endl;
src.getline(buffer.department, DEP, '\n');
cout <<buffer.department << endl;
src.getline(buffer.username, LI, '\n');
cout <<buffer.username << endl;
dest.write((char*)&buffer, sizeof(staff));
}
src.close();
dest.close();
return 0;
}
and here is the text file:
Jo
Abrantes
17.212
3872
Dr Manager
ITS
jo
Carole
Alcock
36.220
3884
Dr Associate Professor
Vice Chancellors Unit
carole
Gene
Awyzio
36.205
4090
Ms Director
Media Unit
gene
David
Bomba
39.9
4879
Dr Staffer
Engineering
bomba
Bob
Brown
39.109
3121
Mr Dean
Engineering
bobbrown
I spent at least 3 hours tweaking and whatnot, but still cannot find teh probem