Welcome to Dream.In.Code
Getting C++ Help is Easy!

Join 136,061 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 1,584 people online right now. Registration is fast and FREE... Join Now!




seekp not holding position issue

 
Reply to this topicStart new topic

seekp not holding position issue

lockdown
14 Apr, 2008 - 02:21 AM
Post #1

D.I.C Regular
Group Icon

Joined: 29 Sep, 2007
Posts: 376



Thanked: 1 times
Expert In: PC, Support

My Contributions
So I have created a app that will allow a manager to create, update, delete, and save employee information. Employees information consists of 4 things (Name, Employee Number, Start Year, and Employee Status). Really the only important things are the Name and employee number because I uses that data in another program. All this data is written to a file.

So my issue comes about when building the update part of my application. I have it so my program will search for the employees name they want to update which works correctly. How ever when I try to set it so the the output stream will start before that name I just found, it keeps jumping to the end. Basically I need to have my program start back infront of the name I just found so I can write the updated name to the same space.

I am using tellp to find the location and then subtracting the sizeof that char array to determine the start position. ex. position is 60 and the char array is 20 so its start position would be 40 were the name would be written. But when I uses seekp to set this position it will hold it until I enter the new name which causes it to write that name at the end of the file instead of were seekp was set.

Here is the problem area, and also my full code.
CODE

                        //namePos is set from a tellg that I uses earlier when finding the name and holds the position at the end of that name
            namePos = (nameLoc - 20 ); // I am using char arrays with 20 elements threwout my program could also replaced 20 with sizeof(empName)
            dataOutput.seekp(namePos);// Sets the new position infront of the orginal name <- it will set the place but wont hold it

            cout << "Update employees name" <<endl;
            cout << "Please enter in the employees name" <<endl;
            cout << "Employee: ";
            cin.getline(empName,sizeof(empName)); // Gets the new name from the user

            dataOutput.write(empName,sizeof(empName)); // Writes the new name to the file <- Problem: writing info to end of file instead of seekp position


Full Code so far, will run just the remove function is dont complete and the update function will run but only for updating names currently do to this issue.
CODE

//
//
//

#include <iostream>
#include <string>
#include <fstream>

using namespace std;

int menu();
void create(ofstream &dataOutput);
void update(ifstream &dataInput, ofstream &dataOutput);
//void remove();


void main()
{
    int choice = 0;
        
    ofstream dataOutput;    
    ifstream dataInput;
    choice = menu();                                                        // Calls menu function which is set equal to choice

    if(choice == 1)                                                            // If choice = 1 will call create function and pass it dataFile stream
        create(dataOutput);
    if(choice == 2)                                                            // If choice = 2 will call update function and pass it dataFile stream
        update(dataInput, dataOutput);
    /*if(choice == 3)                                                            // If choice = 3 will call remove function and pass it dataFile stream
        remove(;*/
    if(choice == 4)                                                         // If choice = 4 will exit application
        exit(1);
                                                    

}

int menu() // Menu Function - Displays options to user and dose input validation
{
    int choice = 0;                                                            // choice variable declared as a integer that stores the choice of the user

    cout << "MENU" <<endl;
    cout << "(1) Create sales person's record" <<endl;
    cout << "(2) Update sales person's recrod" <<endl;
    cout << "(3) Delete sales person's record" <<endl;
    cout << "(0) Exit" <<endl;

    cout << "Please enter in a choice from the menu" <<endl;
    cout << "Selection: ";
    cin >> choice;

    while( choice != 0 && choice != 1 && choice !=2 && choice !=3)            // While statement that dose input validation, if data is not valid will prompt with error
    {                                                                        // And ask user to enter in their choice
        cout << "Error:Invalid Selection" <<endl;
        cout << "Please select again" <<endl;
        cout << "Selection: ";
        cin >> choice;
    }
    
    return choice;                                                            // Returns choice to the calling function main
}

void create(ofstream &dataOutput) // Create employee record
{
    char empName[20];
    char empStatus[20];
    char empNumber[20];
    char startYear[20];
    char save;
    
    cin.ignore();
                                                
    dataOutput.open("EmpSalesRecord.txt",ios::out | ios::app);        
    if(!dataOutput.is_open())                                                          
    {
        cout << "\nError: File could not be opened" <<endl;
        exit(1);
    }
        

    cout << "CREATE RECORD" <<endl;                                                            //
    cout << "Please enter in the employees name, number, start year, and status" <<endl;
    cout << "Part or Full Time" <<endl;
    cout << "Employees Name: ";
    cin.getline(empName,sizeof(empName));                                                                    //

    cout << "Employees Number: ";
    cin.getline(empNumber,sizeof(empNumber));                                                                    //
    cout << "Employees Start Year: ";                                                        
    cin.getline(startYear,sizeof(startYear));
    cout << "Employees Status (Part or Full Time): ";
    cin.getline(empStatus,sizeof(empStatus));                                                                //
                                                                                            //
    cout << "Is the fallowing data correct" <<endl;
    cout << "Employees Name: "<< empName <<endl;
    cout << "Employees Number: "<< empNumber <<endl;
    cout << "Employees Start Year: "<< startYear <<endl;
    cout << "Employees Status: "<< empStatus <<endl;

    cout << "Would you like to save this data to file" <<endl;
    cout << "Save (Y/N): ";
    cin >> save;                                                                            //
        
    while(save != 'Y' && save != 'y' && save != 'N' && save != 'n')                            //
    {                                                                                        //
        cout << "Error: You must select Y or N" <<endl;
        cin >> save;
    }
    
    if(save == 'Y' || save == 'y' || save == 'N' || save == 'n')                            //
    {                                                                                        //
            dataOutput.write(empName,sizeof(empName));
            dataOutput.write(empNumber,sizeof(empNumber));    
            dataOutput.write(startYear,sizeof(startYear));
            dataOutput.write(empStatus,sizeof(empStatus));
        dataOutput.close();
    }
    else
    {
        
        dataOutput.close();
        exit(1);
    }


}
void update(ifstream &dataInput,ofstream &dataOutput) // Allows user to update information from the file
{
    char empName[20];
    char empNameSearch[20];
    char empStatus[20];
    char empNumber[20];
    char startYear[20];
    char save;
    int choice = 0, element = 0, exitLoop = 0;
    long nameLoc = 0, numLoc = 0 ,statusLoc = 0, yearLoc = 0;
    long namePos = 0, numPos = 0, statusPos = 0, yearPos = 0;

    cin.ignore();                                                
    dataInput.open("EmpSalesRecord.txt",ios::in | ios::app);        
    if(!dataInput.is_open())                                                          
    {
        cout << "\nError: File could not be opened" <<endl;
        exit(1);
    }

    cout << "Update Records" <<endl;

    cout << "Please enter in the name of the employee you would like to update" <<endl;
    cout << "Employee: ";

    cin.getline(empName,sizeof(empName));

// ********************************************************************************
****** SEARCH FOR NAME
    do
    {
        
        dataInput.read(empNameSearch,sizeof(empNameSearch));

        while(empName[element] == empNameSearch[element] &&  element < 20)
        {
                element++;
        }
        if(element == 20)
        {
            exitLoop = 1;
        }
        element = 0;
        if(dataInput.eof())
        {
            cout << "The employee " << empName << " could not be found" <<endl;
            cout << "PLease check the employee name and try again" <<endl;
            exit(1);
        }

    }while(exitLoop != 1);
// ********************************************************************************
****** SEARCH FOR NAME

    nameLoc = dataInput.tellg();

    dataInput.read(empNumber,sizeof(empNumber));
    numLoc = dataInput.tellg();

    dataInput.read(startYear,sizeof(startYear));
    yearLoc = dataInput.tellg();

    dataInput.read(empStatus,sizeof(empStatus));
    statusLoc = dataInput.tellg();

    cout << "Current Information" <<endl;
    cout << "Employees Name: "<< empName <<endl;
    cout << "Employees Number: "<< empNumber <<endl;
    cout << "Employees Start Year: "<< startYear <<endl;
    cout << "Employees Status: "<< empStatus <<endl;

    dataInput.close();

    dataOutput.open("EmpSalesRecord.txt",ios::out | ios::app);    

    if(!dataOutput.is_open())                                                          
    {
        cout << "\nError: File could not be opened" <<endl;
        exit(1);
    }
//******************************************************************************* Problem area        
    cout << "What would you like to update for " << empName <<endl;
    cout << "(1) Employees Name" <<endl;
    cout << "(2) Employees Number" <<endl;
    cout << "(3) Employees Start Year" <<endl;
    cout << "(4) Employees Status" <<endl;
    cout << "(0) Exit" <<endl;
    cout << "Selection: ";
    cin >> choice;

    long curn; //TESTING
        if(choice == 0)
            exit(1);

        if(choice == 1)
        {
            cin.ignore();
            curn = dataOutput.tellp();
            namePos = (nameLoc - 20 );
            namePos = (curn + namePos);
            dataOutput.seekp(namePos);

            cout << curn <<endl;   //TESTING
            cout << nameLoc <<endl;// TESTING
            cout << namePos <<endl; // TESTING
            cout << "Update employees name" <<endl;
            cout << "Please enter in the employees name" <<endl;
            cout << "Employee: ";
            cin.getline(empName,20);
            nameLoc = dataOutput.tellp();
            cout << nameLoc <<endl;
            dataOutput.put('t');
            nameLoc = dataOutput.tellp();
            cout << nameLoc <<endl;
//*****************************************************************************
    
        }

        if(choice == 2)
        {
            cin.ignore();
            cout << "Update employees number" <<endl;
            cout << "Please enter the employess number" <<endl;
            cout << "Number: ";
            
        }

        if(choice == 3)
        {
            cin.ignore();
            cout << "Update employees start year" <<endl;
            cout << "Please enter in the start year" <<endl;
            cout << "Start Year: ";
            
        }

        if(choice == 4)
        {
            cin.ignore();
            cout << "Update employees status"<<endl;
            cout << "Please enter in the employees status"<<endl;
            cout << "Employees Status (Part or Full Time): ";
            
        }
    dataOutput.close();
}

/*void remove(fstream &dataFile)
{


}*/


Please help with this guys, it has been driving me crazy for the last couple days.
Thanks in advance
- Lockdown
User is offlineProfile CardPM
+Quote Post

Reply to this topicStart new topic
Time is now: 12/1/08 06:29PM

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month