GetLine woes

Started by
2 comments, last by Hades11 20 years, 5 months ago
I can''t find out why this is misbehaving. The problem is that it will read from a text file ten "words" then keep reading but the word objects are empty. Its not a problem with the text file because if I remove the first 10 words it still will only read in 10. I''m basiclly making a mini dictionary so i can practice using files and search methods. Thanks guys header:

#include <iostream>
using namespace std;
class word
{

  public:
      
      void dispWord();
      char word[20], def[700];
}; 

void word::dispWord()
{
cout<<"\nWord:\n"< 

cpp:

#include <stdlib.h> 
#include <cstring>
#include <fstream>
#include <iostream>
#include "dict.h"
using namespace std;
void readFile();

int main ()
{
readFile();
system("pause");
}

void readFile()
{
   ifstream fin;
   word *database= new word[23231];//23231
   
   unsigned long i=0;
   fin.open("a.txt");
  
   while(i<50){
   fin.getline(database.word,20,'')'' );
   fin.getline(database.def,700,''\n'');
   database.dispWord();
   i++; 
   }
   fin.close();
   delete[] database;
}  </i>  
--Ah but you very well mean you can't take less, it's very easy to take more than nothing.'
Advertisement
I just tried this.. and it worked (with a 20 definition file)

#include <stdlib.h> #include <cstring>#include <fstream>#include <iostream>void readFile();class word{    public:        void dispWord();        char word[20], def[700];};void word::dispWord(){    std::cout<<"Word: "<<word<<std::endl<<"Definition: "<<def<<std::endl<<std::endl;}int main (){readFile();system("pause");}void readFile(){std::ifstream fin;word *database= new word[23231];//23231unsigned long i=0;fin.open("a.txt");while(fin.peek()!=EOF&&i<23231){fin.getline(database.word,20,'')'' );<br>fin.getline(database.def,700,''\n'');<br>database.dispWord();<br>i++; <br>}<br>fin.close();<br>delete[] database;<br>} <br></pre><!–ENDSCRIPT–><br><br>I would however recommend that you store the words and definitions in strings (to let words/definitions be arbitrarily long). Also store the words themselves (database) in a vector, to let the dictionary be arbitrarily large.   
Sorry, I didn''t read your post very carefully. But, do check if you suffer from this: http://www.gamedev.net/community/forums/topic.asp?topic_id=190308 (if applicable in your case)
Thanks for the replys guys. I''ll look inot strings and vectors as well.
--Ah but you very well mean you can't take less, it's very easy to take more than nothing.'

This topic is closed to new replies.

Advertisement