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

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




tellp and seekp issues

 
Reply to this topicStart new topic

tellp and seekp issues

lockdown
11 Apr, 2008 - 05:26 PM
Post #1

D.I.C Regular
Group Icon

Joined: 29 Sep, 2007
Posts: 376



Thanked: 1 times
Expert In: PC, Support

My Contributions
I am creating a employee information application were the user has the ability to create and update information. The user enters in the name of the person they want to update which is working correctly.

However when they want to update that person name I run into a issue. So far the best way I have though to update the users name would be to uses tellp once I found the data and subtracting that by the string length to determine the starting position. In theory I assumed that would over right the data when I uses seekp to set the position the data should start to be written to. But when ever I set seekp to the position I want it to write to it jumps back to the end of the file. Am I missing something with seekp that would cause this?

It might sound a little confusing but here is my code if it helps:
CODE

    cout << "Update Records" <<endl;

    cout << "Please enter in the name of the employee you would like to update" <<endl;
    cout << "Employee: ";
    getline(cin,empNameSearch);        // Ask user to enter in the name of the employee they want to find the recrd of
                                       // empNameSearch and empName are declared as strings

    while(empNameSearch != empName)    //Finds employees name (work in progress but dose not effect the issue I am having)
    {
        getline(dataFile,empName);

        if(dataFile.eof())
        {
            cout << "A record for " << empName << " could not be found" <<endl;
            cout << "Please check your data and try again" <<endl;
            main();
        }
    }

    position = dataFile.tellp();        //position declared as int
    stringSize = empName.size();        //string size declared as int

    position = (position - stringSize); //Determine the start point of the orginal name
    dataFile.seekp(position);           //Sets the position to write data to

    cout << "Please enter in the employees new name" <<endl;
    cout << "Employee: ";
    getline(cin,empName);               //Gets updated name from user to write to file

    dataFile << empName;                //Writes empName string back to file (Here is the issue cause it wont write it to the point I set seekp to


Also I am open to suggestion of other methods about updating the users name with the new one. So if tellp and seekp are not the best please feel free to mention it.
User is offlineProfile CardPM
+Quote Post

jeronimo0d0a
RE: Tellp And Seekp Issues
12 Apr, 2008 - 07:47 AM
Post #2

D.I.C Head
**

Joined: 3 Mar, 2008
Posts: 141


My Contributions
Using studio 2005, there is no IOfstream and it is a tellg not p to get the position. (still setp) Since you are not testing for success this will run without complaint, just giving wierd results.

I would get the position, close the ifstream, open an ofstream, seek the position, and write to it.

Your code didn't have a driver and there were too many variables to chase down so I couldn't send it to you patched, but these are oblivious problems. If you don't test, you can run this with a filename that doesn't even exist and it won't complain, it just won't give you the data you expect. If you have other questions about testing, see the help or ask.

Hope this helps,
Jeronimo
User is offlineProfile CardPM
+Quote Post

lockdown
RE: Tellp And Seekp Issues
13 Apr, 2008 - 07:52 AM
Post #3

D.I.C Regular
Group Icon

Joined: 29 Sep, 2007
Posts: 376



Thanked: 1 times
Expert In: PC, Support

My Contributions
Yeah sorry for my code not all being their. I have the complete code but its pretty long and I did not think anyone wanted to sort threw it to find the issues. I am using Fstream for my input and output which tellp and seekp will work in. Its just their position once I uses seekp to set it, it will not stay their.

Do i need to clear the buffer possibly to get it to stay?

This post has been edited by lockdown: 13 Apr, 2008 - 07:53 AM
User is offlineProfile CardPM
+Quote Post

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

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