Thread: structures, reading from and writing to a file

  1. #1
    Registered User
    Join Date
    Feb 2010
    Posts
    58

    structures, reading from and writing to a file

    i posted about this earlier but have made a lot of changes, but i'm still almost as lost as i was before. so the idea is that i have to read from the text file "players.txt" (which i'll post at the bottom so you can see what it is) and then split up all the information in it. it contains a name, then goals, assists, and shots on goal. each of those separated by colons. i need to write it to a file and make it look nice. right now i'm getting a ton of errors that i don't understand.
    this is the assignment
    CSCI 240 - Program 10 - Spring 2010

    the code i'm posting is only the first program, i'll deal with the second later, if i can figure this one out. does anyone have any tips as to what i'm doing wrong? i know there are tons of errors, but i am in way over my head with this. thanks

    Code:
    #include <iostream>
    #include <iomanip>
    #include <fstream>
    
    using namespace std;
    
    struct playerInfo
    	{
    		char playerName[25];
    		int shots;
    		int goals ;
    		int assists;
    	};
    
    void makePlayer(char *inputLine, playerInfo &aPlayer); 
    
    
    
    int main()
    {
    
    struct playerInfo
    	{
    		char playerName[25];
    		int shots;
    		int goals ;
    		int assists;
    	};
    	
    	int playerInfo;
    	char aPlayer;
    	char ar[50];
    	
    	playerInfo& aPlayer;
    	
    	ifstream input;
    	ofstream output;
    	
    	input.open( "players.txt" );
    	
    	if ( input.fail() )
    		
    		{
    		cout << " Players.txt failed to open ";
    		exit(0);
    	}
    
    	output.open( "teams.txt" );
    	if ( output.fail() )
    	{
    		cout << " Teams.txt failed to open ";
    		exit(0);
    	}
    	input.getline(ar,50);
    	while (input)
    	{
    		makePlayer(ar, aPlayer);
    		output.write( (char *) &aPlayer, sizeof(aPlayer) );
    		
    		input.getline(ar,50);
    	}
    
    
    	
    	return 0;
    	
    }
    
    void makePlayer(char *inputLine, playerInfo &aPlayer)
    {
    
    	int goals;
    	int assists;
    	int shots;
    	
    	
    	char *ptr;
    	ptr = strchr(inputLine, ':');
    	*ptr = '10';
    	
    	strcpy(aPlayer, playerName, inputLine);
    	ptr++;
    	
    	inputLine = strchr(*ptr, ':');
    	inputLine = '10';
    	
    	aPlayer; goals = atoi(ptr);
    	inputLine++;
    	
    	ptr = strchr(inputLine, ':');
    	*ptr = '10';
    
    	aPlayer; assists = atoi(inputLine);
    	ptr++;
    	
    	aPlayer; shots = atoi(ptr);
    }
    this is the text file i'm reading from
    Code:
    Patrick Kane:28:54:233
    Duncan Keith:13:52:193
    Patrick Sharp:22:39:241
    Jonathan Toews:22:37:185
    Marion Hossa:22:22:174
    Troy Brouwer:21:18:108
    Brian Campbell:7:31:131
    Kris Versteeg:17:21:164
    Andrew Ladd:16:18:130
    Dustin Byfuglien:16:13:188
    Brent Seabrook:4:20:118
    John Madden:10:13:118
    Kim Johnsson:7:10:94
    Tomas Kopecky:7:8:79
    Niklas Hjalmarsson:2:12:52
    Colin Fraser:2:11:80
    Dave Bolland:4:9:42
    Ben Eager:5:5:57
    Nick Boynton:1:7:50
    Brent Sopel:1:5:38
    Jordan Hendry:1:5:33
    Bryan Bickell:2:1:16
    Jake Dowell:1:1:4
    Jack Skille:1:1:9
    Adam Burish:1:1:4
    Cristobal Huet:0:0:0
    Antti Niemi:0:0:0
    Corey Crawford:0:0:0

  2. #2
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    You forget that, when people know what they are doing, they can recognise symptoms of someone who has not even tried.

    When you have hacked code without any real thought, which you have obviously done here, you may sometimes get lucky and have the code work. The result you have - a bitterly complaining compiler - is more likely.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

Popular pages Recent additions subscribe to a feed