Thread: Move pointer to next line in .txt file

  1. #1
    Registered User motarded's Avatar
    Join Date
    Feb 2006
    Location
    Djibouti, Africa
    Posts
    14

    Move pointer to next line in .txt file

    I'm trying to select a value at random from a .txt document. Each value is on a separate line (I understand that the computer interprets that as Value 1\nValue 2\n Value3).

    Example.txt
    Code:
    WORD | SUBJECT
    WORD | SUBJECT
    WORD | SUBJECT
    WORD | SUBJECT
    I can pull the same word and subject without a problem. Is there anyway to randomly set the pointer to begin on a different line of the .txt file? Short of going through and finding all the pointer positions?

    I've already initialized rand() to a truly random number using srand(time(NULL)). I just can't figure out a way to move to a random line and return the information.

    To sum it up...is there anyway to move the pointer "vertically" by identifying '\n' chars?
    Last edited by motarded; 03-01-2006 at 06:47 AM.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,656
    > Is there anyway to randomly set the pointer to begin on a different line of the .txt file?
    > Short of going through and finding all the pointer positions?
    That is exactly what you have to do with a text file - read the whole thing and build an index of all the start of line positions.

    Then you can randomly seek to a line and read it.

    Or consider reading the whole file into a std::vector< std::string >
    Then pick one from that.

  3. #3
    Registered User motarded's Avatar
    Join Date
    Feb 2006
    Location
    Djibouti, Africa
    Posts
    14
    Ok...I wrote it all down and it made a little more sense...

    I read it all into a buffer, then go through and see how many '\n' characters there are. But how can I declare the positions after the \n's as possible start points for the input from the file?

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,656
    Well in C terms, you do

    ftell() to find the current file pointer, store this in an index
    fgets() to read a line

    Do that until you've indexed the whole file.

    Then to pick a line, you fseek() to a position in the index and do fgets() to read your random line.

  5. #5
    Registered User motarded's Avatar
    Join Date
    Feb 2006
    Location
    Djibouti, Africa
    Posts
    14
    Here's what I have right now:

    Code:
    void Player::GetWord()
    {
         srand( (time(NULL) );
         int NumLines(0), RandNum, length;
         ifstream File;
         char * buffer;
    
         File.open("example.txt")
         if(File.is_open())
         {
              File.seekg(0, ios::end);
              length = File.tellg();
              File.seekg(0, ios::beg);
    
              buffer = new char [length];
    
              File.read(buffer, length);
    
              for(int x(0); x < length; x++)
              {
                   if(buffer[x] == '\n')
                        NumLines++;
              }
    
              RandNum = rand()%NumLines;
         }
    }
    Is there a way to flag the \n chars? Or should I just record their position in an array? In your example, are you suggesting making an array of strings, each with a different line of the file?

  6. #6
    Registered User motarded's Avatar
    Join Date
    Feb 2006
    Location
    Djibouti, Africa
    Posts
    14
    Hmm...I guess I could store them as a bidimensional array of strings....hmmmmmmmm

  7. #7
    Registered User
    Join Date
    Oct 2005
    Location
    Hyderabad, India
    Posts
    33
    I will suggest you use a vector of strings if the file is not too big
    Code:
    // reading a text file
    #include <iostream>
    #include <fstream>
    #include <string>
    #include <vector>
    using namespace std;
    
    int main () {
      string line;
      ifstream myfile ("example.txt");
      vector<string> v;
      if (myfile.is_open())
      {
        while (! myfile.eof() )
        {
          getline (myfile,line);
          v.push_back(line);
    
        }
        myfile.close();
    
      // you have the vector of lines in your file
      // get which ever line you want by line number
      for(unsigned int i = 0; i < v.size(); ++i)
        cout<<v.at(i)<<"\n";
    
      }
    
      else cout << "Unable to open file"; 
    
      return 0;
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Basic text file encoder
    By Abda92 in forum C Programming
    Replies: 15
    Last Post: 05-22-2007, 01:19 PM
  2. Game Pointer Trouble?
    By Drahcir in forum C Programming
    Replies: 8
    Last Post: 02-04-2006, 02:53 AM
  3. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  4. Batch file programming
    By year2038bug in forum Tech Board
    Replies: 10
    Last Post: 09-05-2005, 03:30 PM
  5. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 10:33 AM