help with fstream and multiple variables.

I have used a text file to read and input multiple variables. I have 3 lines, with 3 players, including ID, name and money variables.

I am looking for a way to simply display the variables for 1 player, rathern than output all three players and their respective variables.

Any advice?

Thanks

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
  #include <iostream>
#include <fstream>
using namespace std;

int main () {

    ifstream theFile("players.txt");

        
        int id;
        string name;
        double money;
  
        while (thefile >> id >> name >> money ){
            cout << id << ",   " << name << ",   " << money << endl;
        }


    }


Text file:
1 Brad 146
2 John 654
3 Mark 454
Well. Then skip the while loop.

1
2
3
thefile >> id >> name >> money 

cout << id << ",   " << name << ",   " << money << endl;


This should give you the output

1 Brad 146

Firstly, your condition in the while loop won't work as you expect. The extraction operator >> returns a reference to the stream (thefile in this case), and will always return that reference even in cases of failure. Instead use a function that checks the state of the stream, such as while (thefile.good()). As for your actual question, what exactly are you asking? If you only want to print data for one player then just perform the extraction once and get rid of the loop. I doubt this is what you mean though.
closed account (SECMoG1T)
Advice : working with data from textfiles by means of streaming directly from them will always prove to be expensive operation escpecially as the records get large, I would advice you to load your file into ram maybe into a vector or an array of structs then operate on them while they are in memory, that will significantly reduce the latency induced by disc IO.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
struct player
{
 player(int id, string nm, double mny):Id (id), Nane (nm), money (mny){}
 int Id;
 string Name;
 double money;
 };

 vector<player> records;

 void load (vector<player>& vec)
  {
     ifstream in ("players.txt");
     if (! In) {std::cout <<" couldn't open your file "; return; }

     int id; double csh; string name;

    while (in>> id>> name>> csh)
      {
          vec.push_back (player (id, name, csh));
      }
   }

///use your vector now. 


@Modshop AFAIK streams do provide implicit Boolean type conversions that allow you to enquire about the stream state by checking the currently set bits {goodbit, badbit, failbit,...) so I believe his while condition should work as expected.
Last edited on
I would like to store all of the data in a array of structs, and then be able to call on individual players and print our their variables.
closed account (SECMoG1T)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
struct player
 {
   player(int id, string nm, double mny):Id (id), Nane (nm), money (mny){}
    int Id;
   string Name;
   double money;
 }; 

 player records[10];

  void load (player array [], int size)
  { 
    ifstream in ("players.txt");
    if (! In) {std::cout <<" couldn't open your file "; return; } 

     int count=0;
     player temp;

      while ((in>> temp.id>> temp.name>> temp.money)&&count <size)
     {
        array [count]=temp; ++count;
      }
    }

///use your array 
Ok, thanks. I am getting the following error for line 14:
player records [10];

error: no matching function for call to 'player::player()'
closed account (SECMoG1T)
Sorry add this player (){} to the struct
Topic archived. No new replies allowed.