all i can do is get each line at a time....
how can i read it to only read till the space....

plz help.... heres my lil bit of code

while (getline(iofile,line))
  {
   cout << line << endl; 
  }

plz i know i can use this aswell for line by line

while (!iofile.fail())
  {
    getline(iofile,line)
    cout << line << endl; 
  }

thanx in advanced

Recommended Answers

All 13 Replies

while (getline(iofile,line))
{
   cout << line << endl; 
}

Your code is on the right track. Check out the getline function's prototypes here: getline - C++ Reference. The file structure you have described is known as a "Space-delimited File". In your getline function, specify the delim parameter. (If you have followed the link, you will know what to pass as this third parameter).

Hope this helps!

NOTE: This type of question has been asked many times before, in fact some very visible in the main Index of this forum! Try to at least search these forums/or the web before posting your question. A LOT of information is available on the net on this topic.

thanx...will check it out right away...

but b4 i do... im using something like this now

iofile >> data1 >> data2 >> data3 >> data4;

will this help???

thanx...will check it out right away...

but b4 i do... im using something like this now

iofile >> data1 >> data2 >> data3 >> data4;

will this help???

I have no idea where you are going to put this, so I don't know what it will accomplish. If it is inside your while-loop, then, no it will not help at all. Anyway, this approach undermines the purpose of getline in the first place. Stick with your original code.

so far i got this

fighter fight[80]; // this is a class with an array size

//the class looks as follows
class fighter
 { 
  string name, special; 
  int str, agi, spd;  
  
public: 


  void set(string n, string s, int st, int ag, int sp)
   {
   name = n;
   special = s;
   str = st;
   agi = ag;
   spd = sp;
   } 
// end of class

 while (getline(iofile,line))
  {
   iofile >> name >> special >> str >> agi >> spd;

   fight[n].set(name, special, str, agi, spd);
n++;
  }

now is what im trying to do right? read from the text file and put it into an array?

Line by line, word by word:

void WordByWord(const char* filename)
{
    if (!filename)
        return;
    ifstream file(filename);
    string line, word;
    for (int lineno = 1; getline(file,line); lineno++) {
        cout << "Line #" << lineno << ":\n";
        istringstream words(line);
        while (words >> word)
            cout << word << '\n';
    }
}

Think ;)...

Did you actually go through the links I gave, and the clue I provided in my previous posts? If so, did you not understand it? and no, your code still will read your file line-by-line.

> read from the text file and put it into an array?

A similar(if not exactly the same) question has been asked at least twice in the past week in this same forum. Search for answers first, and iff you did not understand it, or else your problem hasn't been addressed to yet, then ask the question.

>
(To ArkM): Nice Code!

yes i did read ur post... and actually what i posted is working talking bout the

iofile >> data1 >> data2 >> data3 >> data4;

it actually does what i want...

only prob i have now is storing it in2 an class array :/

>>only prob i have now is storing it in2 an class array

What array? You need to post the exact class declaration instead of making us guess what you are doing.

heres my entire class

class fighter
 { 
  string name, special;  // when i compile it says error is here
  int str, agi, spd;  
  
public: 

  void set(string n, string s, int st, int ag, int sp)
   {
   name = n;
   special = s;
   str = st;
   agi = ag;
   spd = sp;
   } 
 };

what im trying to do here... is display the class array

for (n = 0; n < 80; n++)
  {
  cout << fight[n].name << endl; // and this is part of the error
  }

>> // when i compile it says error is here
I suspect you failed to include <string> header and/or failed to identify the namespace. But again I don't know because I can't see your monitor.

#include <string>
...
...
std::cout << fight[n].name << std::endl;

>> // when i compile it says error is here
I suspect you failed to include <string> header and/or failed to identify the namespace. But again I don't know because I can't see your monitor.

#include <string>
...
...
std::cout << fight[n].name << std::endl;

im mayb new 2 c++ but im not dumb.... any way thanx for all your help... i actually figured it all out... thanx to those that did help...

and another thing plz be more friendly... there are millions of forums out there.... i nearly decided to just leave this cause ppl are so unfriendly.... yes if i make a mistake, its fine by telling me where... but dont assume im an idiot... and say sh*t things

once again thanx

You are trying to access private class members outside class member functions. Declare name and other data members as public (bad solution) or better add yet another member function:

void fighter::print() const
{
    std::cout << name << ...
}

and use it:

fight[n].print();

or (the best solution) add overloaded operator<<(std::iostream&,const fighter&) . I'm not sure that the last solution is actual for you now...

im mayb new 2 c++ but im not dumb.... any way thanx for all your help... i actually figured it all out... thanx to those that did help...

and another thing plz be more friendly... there are millions of forums out there.... i nearly decided to just leave this cause ppl are so unfriendly.... yes if i make a mistake, its fine by telling me where... but dont assume im an idiot... and say sh*t things

once again thanx

I was only going on what you said in the comments
>> // when i compile it says error is here

That points to the line that declares several string object. The only conclusion I can make, without seeing your entire program, is that you failed to include a header file. If you took my comment as a shitty thing then you should have included more information that I could use to evaluate your problem. I can't see your monitor and I can read your mind.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.