How to read from a particular line in a file

Hi!
I am trying to do a Hangman game program where the words are stored in a file called words.txt separated by 'Return'. Now I want to read the word from a particular line as required. Suppose I pass 3 as a parameter and then another function will read the word at line number 3 and show it. I have the following code but it dosen't work. Please help.

[code]
#include
#include
#include

/* Hangman game */

class hang {
char word[50];
public:
void open(int pos);
void process();
};

void hang::open(int pos) //function that reads the words
{
ifstream h1;
int i=0;
h1.open("words.txt");
h1.seekg(0,ios::beg);
while (!h1.eof())
{
i++;
if (i==pos)
{
h1>>word;
}
}
cout<<word;
h1.close();
};

void hang::process()
{

};

void main()
{
clrscr();
hang h;
h.open(1); //parameter passed
getch();
};
[/code]

Thanks,
Abhishek.

Comments

  • [b][red]This message was edited by stober at 2006-1-20 21:56:28[/red][/b][hr]
    1. use instead of the old obsolete
    2. same with

    3. If the file contains one word per line, just start at the beginning of the file and read X number of words. something like this: (Note: testing for eof() is not reliable way to test for end-of-file or other error).
    [code]
    #include
    #include
    #include
    : std::string hang::open(int wordnum) //function that reads the words
    : {
    std::string word;
    : std::ifstream in("words.txt");
    if( !in.is_open())
    return "";
    : int i=0;
    : while ((in >> word) && i++ < wordnum)
    ;
    : return word;
    }
    [/code]




  • Its not working. Now its giving the following errors. I am on Turbo C++ 3.0.

    [code]
    Compiling ..WORKHANG.CPP:
    Error ..WORKHANG.CPP 1: Unable to open include file 'IOSTREAM'
    Error ..WORKHANG.CPP 2: Unable to open include file 'FSTREAM'
    Error ..WORKHANG.CPP 3: Unable to open include file 'STRING'
    Error ..WORKHANG.CPP 15: Type qualifier 'std' must be a struct or class name
    Error ..WORKHANG.CPP 15: Declaration terminated incorrectly
    Error ..WORKHANG.CPP 50: Function 'clrscr' should have a prototype
    Error ..WORKHANG.CPP 53: Function 'getch' should have a prototype
    [/code]

    : [b][red]This message was edited by stober at 2006-1-20 21:56:28[/red][/b][hr]
    : 1. use instead of the old obsolete
    : 2. same with
    :
    : 3. If the file contains one word per line, just start at the beginning of the file and read X number of words. something like this: (Note: testing for eof() is not reliable way to test for end-of-file or other error).
    : [code]
    : #include
    : #include
    : #include
    : : std::string hang::open(int wordnum) //function that reads the words
    : : {
    : std::string word;
    : : std::ifstream in("words.txt");
    : if( !in.is_open())
    : return "";
    : : int i=0;
    : : while ((in >> word) && i++ < wordnum)
    : ;
    : : return word;
    : }
    : [/code]
    :
    :
    :
    :
    :

  • : Its not working. Now its giving the following errors. I am on Turbo C++ 3.0.
    :

    you will have to use the files with .h extension -- that compiler is just too ancient. Or you can upgrade to a free modern good compiler at www.bloodshed.net -- Dev-C++.
  • Thank you. But our course allows us Turbo C++ only. Can you please suggest a way to make the program work.

    Abhishek.


    : : Its not working. Now its giving the following errors. I am on Turbo C++ 3.0.
    : :
    :
    : you will have to use the files with .h extension -- that compiler is just too ancient. Or you can upgrade to a free modern good compiler at www.bloodshed.net -- Dev-C++.
    :

  • : Thank you. But our course allows us Turbo C++ only. Can you please suggest a way to make the program work.
    :
    : Abhishek.
    :

    Just add .h to the include's


    Happy coding wishes
    the one and only
    [b]Niklas Ulvinge[/b] [white]aka [b]IDK[/b][/white]

  • Sorry to sound confusing but I was talking about the original problem.

    : : Thank you. But our course allows us Turbo C++ only. Can you please suggest a way to make the program work.
    : :
    : : Abhishek.
    : :
    :
    : Just add .h to the include's
    :
    :
    : Happy coding wishes
    : the one and only
    : [b]Niklas Ulvinge[/b] [white]aka [b]IDK[/b][/white]
    :
    :

Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories