C++ help

A forum for computer hardware and software issues
Post Reply
mr_s
Villun
Villun
Posts: 5102
Joined: Sat Dec 22, 2007 9:27 am

Ville Awards

C++ help

Post by mr_s » 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

MrBlah
Villun
Villun
User avatar
Posts: 6469
Joined: Sat Dec 30, 2006 2:20 am
Location: Colorado. There's like, mountains and stuff.

Games Played

Ville Awards

Mxtress Blah’s avatar
Loading…

Post by MrBlah » Sun Apr 06, 2008 10:52 pm

DOG!?!?!?!?!?!
Image
Eater of Potatoes, since 2008.


I am driven by two main philosophies: know more today about the world than I knew yesterday and lessen the suffering of others. You'd be surprised how far that gets you.
― Neil deGrasse Tyson

YevGenie
Villun
Villun
User avatar
Posts: 1760
Joined: Wed Nov 07, 2007 10:12 pm
Location: San Mateo, California

Games Played

Ville Awards

Post by YevGenie » Sun Apr 06, 2008 10:55 pm

Damn that looks confusing...*sigh*
<----envy people that can do that stuff.
Image

mr_s
Villun
Villun
Posts: 5102
Joined: Sat Dec 22, 2007 9:27 am

Ville Awards

Post by mr_s » Sun Apr 06, 2008 10:58 pm

[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

Gamepro65
Villun
Villun
User avatar
Posts: 47
Joined: Tue Oct 24, 2006 10:49 am

Ville Awards

Re: C++ help

Post by Gamepro65 » Sun Apr 06, 2008 11:09 pm

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.

mr_s
Villun
Villun
Posts: 5102
Joined: Sat Dec 22, 2007 9:27 am

Ville Awards

Post by mr_s » Sun Apr 06, 2008 11:18 pm

[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));

gator
Retired Admin
Retired Admin
User avatar
Posts: 2225
Joined: Sun Oct 29, 2006 8:34 am

Games Played

Ville Awards

gator’s avatar
Loading…

Post by gator » Sun Apr 06, 2008 11:56 pm

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);

Post Reply

Who is online

Users browsing this forum: No registered users and 3 guests