Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

C++ reading file problem: I have a txt file: Player.txt yearID,teamID,leagueID,p

ID: 3832117 • Letter: C

Question

C++ reading file problem:

I have a txt file: Player.txt

yearID,teamID,leagueID,playerID,salary,firstName,lastName,birthYear,birthCountry,weight,height,bats,throws
1985,ATL,NL,barkele01,870000,Len,Barker,1955,USA,225,77,R,R
1985,ATL,NL,bedrost01,550000,Steve,Bedrosian,1957,USA,200,75,R,R
1985,ATL,NL,benedbr01,545000,Bruce,Benedict,1955,USA,175,73,R,R
1985,ATL,NL,campri01,633333,Rick,Camp,1953,USA,195,73,R,R
1985,ATL,NL,ceronri01,625000,Rick,Cerone,1954,USA,192,71,R,R
1985,ATL,NL,chambch01,800000,Chris,Chambliss,1948,USA,195,73,L,R
1985,ATL,NL,dedmoje01,150000,Jeff,Dedmon,1960,USA,200,74,L,R
1985,ATL,NL,forstte01,483333,Terry,Forster,1952,USA,200,75,L,L
1985,ATL,NL,garbege01,772000,Gene,Garber,1947,USA,175,70,R,R
1985,ATL,NL,harpete01,250000,Terry,Harper,1955,USA,195,76,R,R
1985,ATL,NL,hornebo01,1500000,Bob,Horner,1957,USA,195,73,R,R
1985,ATL,NL,hubbagl01,455000,Glenn,Hubbard,1957,Germany,150,69,R,R
1985,ATL,NL,mahleri01,407500,Rick,Mahler,1953,USA,195,73,R,R
1985,ATL,NL,mcmurcr01,275000,Craig,McMurtry,1959,USA,195,77,R,R
1985,ATL,NL,mumphje01,775000,Jerry,Mumphrey,1952,USA,185,74,B,R
1985,ATL,NL,murphda05,1625000,Dale,Murphy,1956,USA,210,76,R,R
1985,ATL,NL,oberkke01,616667,Ken,Oberkfell,1956,USA,175,72,L,R

I created a structure:

struct Player{
    int yearID;
    string teamID;
    string leagueID;
    string playerID;
    int salary;
    string firstName;
    string lastName;
    int birthYear;
    string brithCountry;
    int weight;
    int height;
    string bats;
    string throws;
};

I used the getline to skip the first line but I don't know how to deal with the rest of datas.

I want to implement this:

Player test;

test.yearID = 1985;

test.teamID = ATL;

test.leagueID = NL

...

Please help figure out, thank you!

Explanation / Answer

PROGRAM CODE:

#include <iostream>
#include <fstream>
#include <string>
#include <cstdlib>
using namespace std;

struct Player{
int yearID;
string teamID;
string leagueID;
string playerID;
int salary;
string firstName;
string lastName;
int birthYear;
string brithCountry;
int weight;
int height;
string bats;
string throws;
};

string getWord(string* line, string* word)
{
   size_t found = line->find(",");
   if (found!=std::string::npos)
   {
       *word = line->substr (0,found);
       *line = line->substr (found+1);
      
   }
   else
   {
       *word = *line;
       *line = "";
   }
   return *line;
}

void read()
{
   ifstream in("Player.txt");
   string line, word;
   getline(in, line);
   Player test;
   while(getline(in, line))
   {
       line = getWord(&line, &word);
       test.yearID = atoi(word.c_str());
       line = getWord(&line,& word);
       test.teamID = word;
       line = getWord(&line,& word);
       test.leagueID = word;
       line = getWord(&line,& word);
       test.playerID = word;
       line = getWord(&line,& word);
       test.salary = atoi(word.c_str());
       line = getWord(&line,& word);
       test.firstName = word;
       line = getWord(&line,& word);
       test.lastName = word;
       line = getWord(&line,& word);
       test.birthYear = atoi(word.c_str());
       line = getWord(&line,& word);
       test.brithCountry = word;
       line = getWord(&line,& word);
       test.weight = atoi(word.c_str());
       line = getWord(&line,& word);
       test.height = atoi(word.c_str());
       line = getWord(&line,& word);
       test.bats = word;
       line = getWord(&line,& word);
       test.throws = word;
   }
}
int main() {
   read();
   return 0;
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote