Page 1 of 1

C++ help

Posted: Sun Apr 06, 2008 10:25 pm
by mr_s
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

Posted: Sun Apr 06, 2008 10:52 pm
by MrBlah
DOG!?!?!?!?!?!

Posted: Sun Apr 06, 2008 10:55 pm
by YevGenie
Damn that looks confusing...*sigh*
<----envy people that can do that stuff.

Posted: Sun Apr 06, 2008 10:58 pm
by mr_s
[quote="MrBlah";p="83540"]
DOG!?!?!?!?!?!
[/quote]

[quote="YevGenie";p="83545"]
Damn that looks confusing...*sigh*
<----envy people that can do that stuff.
[/quote]

Rofl...

I am NOT looking for someone to do it for me, rather point out where I is wrong.


Yev, you dont want to do C++ or C for that matter.. trust me

Re: C++ help

Posted: Sun Apr 06, 2008 11:09 pm
by Gamepro65
I'm a java programmer so just from what im trying to convert over here it seems that your not ever taking the information from the getline and then writing it to the file. Correct me because i think i might be wrong but your doing the src.getline which is bringing in the data from the txt file, however your dest.write is not writing any information because your not calling each individual item, just a null item. When i make a txt in java its always dest.write(roomNum);dest.write(phoneNum); etc and they are all just held in either an array or arraylist depending on the type of information.

I would try reading all the staff records putting them inside an array/arraylist and then in another method do a write.

Just my two cents idk if it makes any sence.

Posted: Sun Apr 06, 2008 11:18 pm
by mr_s
[quote="Gamepro65";p="83558"]
I'm a java programmer so just from what im trying to convert over here it seems that your not ever taking the information from the getline and then writing it to the file. Correct me because i think i might be wrong but your doing the src.getline which is bringing in the data from the txt file, however your dest.write is not writing any information because your not calling each individual item, just a null item. When i make a txt in java its always dest.write(roomNum);dest.write(phoneNum); etc and they are all just held in either an array or arraylist depending on the type of information.

I would try reading all the staff records putting them inside an array/arraylist and then in another method do a write.

Just my two cents idk if it makes any sence.
[/quote]

this is what is writing to the file:

dest.write((char*)&buffer, sizeof(staff));

Posted: Sun Apr 06, 2008 11:56 pm
by gator
Couple things:

1) Hard to tell from the post, but it seems there was a trailing space at end of each line - could cause some abnormal behavior with the fields being as tight as they are. It did for me.
2) The field lengths aren't long enough for the data you're trying to read. For example, you try to read in a department whose field length is 20, but one of your entries has a department name longer than 20 characters (Vice Chancellors Unit). I increased it to 30 to work around it.
3) My recollection with character arrays is bad. After debugging, it seems you need to have phone number with length of 5, to acccount for the terminating NULL at end of char array.
4) You should probably return a nonzero number on error where it can't open file.

One thing you can do in the while loop while you're debugging is add in something to get a character from stdin, so you can see what's happening each loop. It would show that something went wrong during the first iteration (on the phone number).

For example:

Code: Select all

    char ch;
    cin.get(ch);