Results 1 to 2 of 2

Thread: Random Access in Text Files

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Jan 2002
    Posts
    21

    Random Access in Text Files

    I have an text file that stores the details of objects of an account class. I need to be able to search the text file based on what account number the user has entered. each account number is a 4 digit integer.
    I have it working for the first line of a file but i dont know how I can move it on to search the start of the next line of the file. This is the code taht i have so far.

    char card[5];
    char c;
    long int file_pos, last_pos;
    string line;

    cin >> card;

    ifstream in("account.txt") ;

    while (!in.eof())
    {
    getline(in, line);
    cout <<line<<endl;
    file_pos = line.find(card);
    }

    Any help would be appreciated. I have not been able to find a tutorial on the web to explain this to me.

  2. #2
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    You can't use the seek functions (find in the iostream library) for text mode files. Well, you can, but it won't give you the expected results. They can be used only in binary files. You must read the file from the beginning and search for the place you want in memory.
    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.

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