Originally posted by: Judgement
I need to open a file, and pull the data from it and put it into an array of structs. The struct has 4 variables.
The function header we were given is passed 4 variables: an ifstream ( passed by &), the array of structures, the array size, and a counter (passed by &) used to count the number of total structures found in the file. The counter is the value passed back to the main program.
I can't seem to get the data from the file into the array of structures properly.
I opened the file using ifstream in the main part of the program because the header given passes the ifstream variable, which makes me think the file is not supposed to be opened in the file itself.
I then used a while statement with the condition that the end of the file has not been reached yet. Then I used a getline(arrayname, arraySize). The next statement was counter++, then the while loop is closed off.
Then return counter;
I called the function in the main part of the program with its return value being assigned to an interger variable.
Well obviously its not working and I'm doing something wrong 🙁
I'm thinking my getline statement is wrong, my book does a horrible join explaining it, and the one example they give is a cin.getfile statement... I have no idea if I'm supposed to be using a cin.getfile statement or a normal getfile statement heh.
Originally posted by: atom
Do you have the getline in the format:
ifstream someVar("testfile.txt");
someVar.getline(temp,100);
Originally posted by: atom
Getline just gets the entire line, you have to manually put the repsective data into the variables inside the struct. So when you do the getline don't make it put the data into the struct but rather a temp array or something like that. Once you have the line inside a temp array then break up the line into the individual data and then put them into the struct.
Originally posted by: atom
Forget what i said in the previous post, if the data is on separate lines then what i said doesn't apply. You don't even need a temp variable unless you want to verify data before assigning it. To convert chars to ints look at the "atoi" function?
The format you posted here:
playerData(ifstream& inData, Players playerlist[], int arraySize, int &numPlayers)
{
while (!inData.eof() )
{
playerlist[numPlayers].name = inData.getline(playerlist, arraySize);
playerlist[numPlayers].height = inData.getline(playerlist, arraySize);
playerlist[numPlayers].jersey = inData.getline(playerlist, arraySize);
playerlist[numPlayers].points = inData.getline(playerlist, arraySize);
numPlayers++;
}
return numPlayers;
}
Should be generally ok I think but you might have to adjust it a little (skipping whitespace and things like that) depending on the format of the input file.
Originally posted by: atom
Getline just gets the entire line, you have to manually put the repsective data into the variables inside the struct. So when you do the getline don't make it put the data into the struct but rather a temp array or something like that. Once you have the line inside a temp array then break up the line into the individual data and then put them into the struct.
char_ptr=strtok(inputstring, delimiters);Originally posted by: Judgement
I've been trying to use a function called strtok in cstring library but I can't get it to work with all its damn pointers and what not.
Ideas?
Come on guys just a little more help 🙂
I just need to figure out how to read in multiple variables from a single line in a file, and then split that string up into seperate strings which can be assigned to variables which are part of a structure. I can differentiate where the seperate strings inside the one large string are by a blank space between them.
Example:
The sun is not up yet.
struct words{
char[10] v1;
char[10] v2;
char[10] v3;
char[10] v4;
char[10] v5;
char[10] v6; }
Then assign each word to the seperate variables to make it so that:
v1 = "The"
v2 = "sun"
v3 = "is"
etc
Originally posted by: motoamd
Heh... I'm on the same thing right now!
You wouldn't happen to be from Austin?