Thread: moving file pointer to specific location

  1. #1
    Registered User
    Join Date
    Mar 2011
    Posts
    45

    moving file pointer to specific location

    I have a file "data.txt" with following content

    Code:
    mark      zuckerbergsiliconvalcalifornia
    jimmy     wales     london    unitedking
    As you can see every record is on new line and size of each record is 40.Now when i directly try to go to location 45 with this below function..

    Code:
    ifstream ifile("data.txt");
    ifile.seekg(45,ifile.cur);
    it does not take me to the location 45.it takes some arbitrary position.
    As file contents change the arbitrary location changes.

    Considering the above example of file contents,what is the correct
    method to go to location "45" which has the letter y.

  2. #2
    Registered User
    Join Date
    May 2010
    Posts
    4,595
    So what value are you finding a location "45"? Would it perhaps be an 'm' instead?

    I don't recommend trying to seek to a specific location in a text file because of differences in line endings. You are taking the line endings into account, right?

    Jim

  3. #3
    Registered User
    Join Date
    Mar 2011
    Posts
    45
    Quote Originally Posted by jimblumberg View Post
    You are taking the line endings into account, right?

    Jim
    Oh.i did not take them into account.
    And there is no guarantee that i find 'm' in that location.
    Like i said as data changes value at that location changes.
    how to go that exact location?

  4. #4
    Registered User
    Join Date
    May 2010
    Posts
    4,595
    how to go that exact location?
    With a text file, read 45 characters worth of information, don't forget to take into account any new line characters. If you want random access you should either consider binary files or since you state the "records" are fixed lengths avoid placing new lines in the file.

    And there is no guarantee that i find 'm' in that location.
    Like i said as data changes value at that location changes.
    When I asked my question I was basing it on the data presented, not some mystical future value.

    Jim

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 10
    Last Post: 07-26-2011, 10:48 PM
  2. Moving a file pointer in a function
    By 127.0.0.1 in forum C Programming
    Replies: 27
    Last Post: 05-04-2011, 06:26 PM
  3. Running Function from specific memory location
    By mshearer123 in forum C Programming
    Replies: 2
    Last Post: 01-27-2010, 11:14 AM
  4. moving charecter location in array
    By arya6000 in forum C Programming
    Replies: 1
    Last Post: 09-21-2008, 12:59 PM
  5. location specific console colors
    By bennyandthejets in forum C Programming
    Replies: 7
    Last Post: 07-24-2002, 07:32 AM

Tags for this Thread