10 Replies - 3510 Views - Last Post: 10 June 2005 - 05:22 PM Rate Topic: -----

#1 uman_dude2  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 35
  • Joined: 31-March 05

Storing data inputted into a struct to a file

Posted 04 June 2005 - 07:47 PM

Hey, I've got a question regarding:

"storing data inputted into a struct to a file".

if I have for instance: a struct of type employee,

struct employee
{
char name;
char addr;
}

Will I need to have an array to count the number of employees stored to file(ie if unsure of number of employees)! Not quite sure how to keep count of # of employees dat will b stored 2 file

Example:
____________________________________________________________
int vWriteFile ()
{
    fstream employeeFile;
    int iCount = 0;
    
    //employeFile.open ("employee.bin", ios::in|ios::out|ios::binary);
    
    if (employeeFile)
    {
        //unsure how to get
        total # of employees
        wen #of employees
        is unknown
        for (int i = 0; i < total employees??; i++)
        {
            // only write active employees to file
            if (EmployeeArray[i].iStatus == ACTIVE)
            {
                questionFile << employeeArray[i].name << endl
                << employeeArray[i].addr << endl;
                
                iCount++;
            }
        }
    }
    else
    {
        cout << "Error!" << "employeefile fail 2 open!";
    }
    employeeFile.close();
    
    return iCount;
} 


____________________________________________________________

Can some1 explain to me exactly, how I will be able to store employee details to file, when Im unsure how many employees will be inputted?
(Any number of employees can be inputted!)

Refer to bold comments for clarification!

Cheers

OH! and I 4Got, do I need an array? becos Im saving to a binary file, so Im unsure if an ARRAY is necessary!
Thanx for your help!

This post has been edited by skyhawk133: 06 June 2005 - 04:57 PM


Is This A Good Question/Topic? 0
  • +

Replies To: Storing data inputted into a struct to a file

#2 Amadeus  Icon User is offline

  • g+ + -o drink whiskey.cpp
  • member icon

Reputation: 248
  • View blog
  • Posts: 13,506
  • Joined: 12-July 02

Re: Storing data inputted into a struct to a file

Posted 04 June 2005 - 07:54 PM

Two of the most obvious ways to get the number of employees is to also store that number in the file, say on the first line. When you add another employee to the file, increment the number. The other would be to insert a blank line between each employee...when you read the file, increment a counter for each blank line you encounter. Yet another way would be to increment a counter for each two lines you read in (or however many lines made up the information for one employee.

As far as writing to a file...it does not matter how many employees are written to the file...each employee is simply appended to the file.
Was This Post Helpful? 0
  • +
  • -

#3 uman_dude2  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 35
  • Joined: 31-March 05

Re: Storing data inputted into a struct to a file

Posted 04 June 2005 - 08:02 PM

Cheers Armadeus!

Thanx for your help!
Was This Post Helpful? 0
  • +
  • -

#4 Amadeus  Icon User is offline

  • g+ + -o drink whiskey.cpp
  • member icon

Reputation: 248
  • View blog
  • Posts: 13,506
  • Joined: 12-July 02

Re: Storing data inputted into a struct to a file

Posted 04 June 2005 - 08:04 PM

No problem sir...post if you need anything else.
Was This Post Helpful? 0
  • +
  • -

#5 uman_dude2  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 35
  • Joined: 31-March 05

Re: Storing data inputted into a struct to a file

Posted 05 June 2005 - 10:19 AM

Question regarding retrieving employee data stored in file:

I've decided to use the '\n' character after each data entry written 2 file for employee!

How do you, read the file, to get the required employee data required?

e.g! not sure if this is correct!

employee_file.read( (char *) ptemp, sizeof(Employee)); ????

How do you read the "new line character" \n? Just confused how the whole idea of inserting a blank line character, using a counter!

I guess will hav to use a loop?

Cheers

This post has been edited by uman_dude2: 05 June 2005 - 10:21 AM

Was This Post Helpful? 0
  • +
  • -

#6 Amadeus  Icon User is offline

  • g+ + -o drink whiskey.cpp
  • member icon

Reputation: 248
  • View blog
  • Posts: 13,506
  • Joined: 12-July 02

Re: Storing data inputted into a struct to a file

Posted 05 June 2005 - 11:20 AM

Is all the information for one employee on one line, or is it on multiple lines? If one line, I assume you'll have to separate the various parts of the employee information to load it in the struct. Can you provide an example of what the file will look like when it's got say 5 employees in it?
Was This Post Helpful? 0
  • +
  • -

#7 uman_dude2  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 35
  • Joined: 31-March 05

Re: Storing data inputted into a struct to a file

Posted 05 June 2005 - 11:33 AM

This is wot I got!

{
userFile << stNewUser.cFirstName
<< "\n"
<< stNewUser.cLastName
<< "\n"
<< stNewUser.cUsername
<< "\n"
<< stNewUser.cPassword
<< "\n";
}

files are on one line! stored to binary file !

Is this wot u meant?
Was This Post Helpful? 0
  • +
  • -

#8 Amadeus  Icon User is offline

  • g+ + -o drink whiskey.cpp
  • member icon

Reputation: 248
  • View blog
  • Posts: 13,506
  • Joined: 12-July 02

Re: Storing data inputted into a struct to a file

Posted 05 June 2005 - 11:36 AM

More or less, but that is information about one emplyee stored on several lines. Not that it matters...if you know how many lines each emplyee will encompass, you simply load a struct based on that number of lines. Number of lines in file divided by the number of lines for each employee gives the number of employees in a file, and your loop control number. You can declare an array of structs of that size, and loop through it.
Was This Post Helpful? 0
  • +
  • -

#9 uman_dude2  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 35
  • Joined: 31-March 05

Re: Storing data inputted into a struct to a file

Posted 05 June 2005 - 11:40 AM

This is ugly...haha but its d content of 4 users written 2 d binary file!


___________________________________________________________

John
Campbell
A
ΓΈ
123
Jade
Pinkett
A
ΓΈ
Jade
Justin
Timberlake
A
ΓΈ
Justin
Janet
Jackson
A
ΓΈ
Janet123

___________________________________________________________

Wow It came up readable on here! Not in Note Pad...haha...Yep this is how it looks in the binary file...
Firstname, lastname, A - (Admin status), & password....

This post has been edited by uman_dude2: 05 June 2005 - 11:42 AM

Was This Post Helpful? 0
  • +
  • -

#10 dorknexus  Icon User is offline

  • or something bad...real bad.
  • member icon

Reputation: 1255
  • View blog
  • Posts: 4,618
  • Joined: 02-May 04

Re: Storing data inputted into a struct to a file

Posted 06 June 2005 - 01:45 AM

if you write it to a binary file, it typically won't be readable, but no worries, the program can read it, are you encountering problems when you actually run the program to read in the data?

This post has been edited by Dark_Nexus: 06 June 2005 - 06:45 AM

Was This Post Helpful? 0
  • +
  • -

#11 uman_dude2  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 35
  • Joined: 31-March 05

Re: Storing data inputted into a struct to a file

Posted 10 June 2005 - 05:22 PM

Thanx guys figured out wot 2 do....Runnin now!

Thanx all for ur help!

Cheers
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1