Results 1 to 9 of 9

Thread: direct access to a location in a text file

  1. #1

    Thread Starter
    Banned learning c's Avatar
    Join Date
    Mar 2007
    Location
    canberra (australia's capital)
    Posts
    198

    direct access to a location in a text file

    can i read and write to a particular location in a text file rather than reading every line in one at a time? the goal is to use the text file as a database and not have it slow down as the file gets bigger?

  2. #2
    Hyperactive Member Iron Skull's Avatar
    Join Date
    Aug 2005
    Location
    The Netherlands
    Posts
    325

    Re: direct access to a location in a text file

    Its not so hard.
    If you want to read from a specific line.
    http://www.cplusplus.com/doc/tutorial/files.html that might help you.

    506C65617365205261746520506F7374732E2E2E

  3. #3
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594

    Re: direct access to a location in a text file

    Actually, it's impossible. You can't just jump to the start of any line in a text file, you have to scan it linearly.
    Text files do not make good databases.
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  4. #4

    Thread Starter
    Banned learning c's Avatar
    Join Date
    Mar 2007
    Location
    canberra (australia's capital)
    Posts
    198

    Re: direct access to a location in a text file

    well i am getting into that tutorial today iron skull.

    cornedbee, well i don't need a relational database, that would be wastefull for my purposes, i just need to store single field records of variable length.

    in the header of the file i was hoping to record the name, location and size of each record an index if you will, and then be able to move down the file to read the record without having to read in every line. Surely there must be an easy way to do it?

  5. #5
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594

    Re: direct access to a location in a text file

    The C++ standard says that the only valid values for fstream::seekg() for text files are those previously returned by tellg(). I'm not sure if pos_t is serializable - if it is, then you can create an index (but it must be external to your main file), if not, then you can't.

    That's not to say that it won't work on your platform, but it isn't defined according to the standard.

    ...

    OK, looked up fpos. There is no requirement that the type is streamable, therefore one must assume that it isn't.
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  6. #6
    Addicted Member
    Join Date
    Mar 2005
    Posts
    228

    Re: direct access to a location in a text file

    or maybe when you store/write it to a file, write in ascending order and then use binary search to find it. i dont know if it would be any help or not.
    effort effort effort and still effort

  7. #7
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594

    Re: direct access to a location in a text file

    Binary search requires random access, which you don't have.
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  8. #8
    Addicted Member
    Join Date
    Mar 2005
    Posts
    228

    Re: direct access to a location in a text file

    hmmm what do you mean random access CB? sorry newbie.
    effort effort effort and still effort

  9. #9
    G&G Moderator chemicalNova's Avatar
    Join Date
    Jun 2002
    Location
    Victoria, Australia
    Posts
    4,246

    Re: direct access to a location in a text file

    Quote Originally Posted by ayahnabunda
    hmmm what do you mean random access CB? sorry newbie.
    It means you can retrieve the data from the file, for example, using an index, directly.
    Code:
    file[2]; // gets the 3rd byte in the file
    ..CornedBee has already explained that to get to a position in a file, you have to scan it linearly, which means from beginning to end. Thus, unable to randomly access anything without a linear scan beforehand.

    chem

    Visual Studio 6, Visual Studio.NET 2005, MASM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width