Thread: input file into a string

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

    input file into a string

    i really am lost as to what to do here. the program i need to write is that i have to open up a text file (i'll display that a the bottom of the post), read the info in the text file and put it into strings, and after sorting through all of that, write the information into a different text file. here is a link to what i need to do. (just program 1 at the moment)
    https://webcourses.niu.edu/courses/1...1/240pgm10.htm

    so the input text takes a player name, and then some various stats of theirs. so the suggested logic in the link i posted is to create a c style string. ok. what exactly would that be? i know the basic string would be something like:

    char s[50]="blah blah blah"

    so i guess my first question is, how do i put the input file into such a string? and what would be the subscript inside? i just randomly made up 50 here, but i have no clue what to use for this program.

    my extremely basic code so far is this
    Code:
    
    #include <iomanip>
    #include <iostream>
    #include <fstream>
    #include <ctype.h>
    #include <math.h>
    using namespace std;
    
    
    
    
    
    
    
    
    int main()
    {
    
    ifstream inputFile; 
    
    inputFile.open( "players.txt" );
    	if( inputFile.fail() )
    	{
    		cout << "players.txt failed to open";
    		exit( -1 );
    	}
    	
    ofstream myfile;
    
    myfile.open( "team.txt", ios::binary );
    	if( inputFile.fail() )
    	{
    		cout << "team.txt failed to open";
    		exit( -1 );
    	}
    
    
    
    
    
    
    
    
    
    
    
    
    return 0;
    }
    this is the text file that i have to read 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
    go hawks!

  2. #2
    Registered User NeonBlack's Avatar
    Join Date
    Nov 2007
    Posts
    431
    Well, I can't get to the link you posted, but I would probably read the file one line at a time and then parse the line into the fields you need. For example:
    Code:
    char line[256];
    while (inputFile.getline(line, 256))
    {
        // Extract data from "line" here.
    }
    I copied it from the last program in which I passed a parameter, which would have been pre-1989 I guess. - esbo

  3. #3
    Registered User
    Join Date
    Feb 2010
    Posts
    58
    hm i don't know what the link problem is, i'll try to fix that. thanks for the tip, that makes sense. now i need to figure out how to split all this information up and sort it out correctly. i'll see if i can't get that link to work

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

  5. #5
    Registered User
    Join Date
    Feb 2010
    Posts
    58
    the function that i'm supposed to write is quite an issue. i don't know where to begin there.


    if the link isn't working, this is the description of the function

    Function to write and use
    void makePlayer( char *inputLine, playerInfo &aPlayer )

    This function will take the information in inputLine and break it into the 4 individual fields for a player and use them to build a playerInfo structure. It takes 2 arguments: a C-Style string that contains player information and a reference to a playerInfo structure that can be used to pass back a player.

    As was mentioned above, each of the fields in the C-Style string is separated by a colon. Use the strchr function to search for a colon (':') so that each field can be isolated, strcpy or atoi can then be used to put the isolated string into the appropriate field of the structure.

    The atoi function will be used to convert a string to its numeric representation.


    so first i'll have to declare "playerInfo". i'm not even sure what to declare it as though.

  6. #6
    Registered User
    Join Date
    Feb 2010
    Posts
    58
    well i guess it'd just be an int.

  7. #7
    Registered User NeonBlack's Avatar
    Join Date
    Nov 2007
    Posts
    431
    The structure that will be used in both of the programs will represent one player. It has fields for the player name, number of goals scored, number of assists, and number of shots attempted.

    Code:
    struct playerInfo
    {
        char playerName[25];
        int goalsScored;
        int assists;
        int shots;
    };
    Read the assignment carefully.
    I copied it from the last program in which I passed a parameter, which would have been pre-1989 I guess. - esbo

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 8
    Last Post: 04-25-2008, 02:45 PM
  2. Replies: 14
    Last Post: 01-18-2008, 04:14 PM
  3. Inheritance Hierarchy for a Package class
    By twickre in forum C++ Programming
    Replies: 7
    Last Post: 12-08-2007, 04:13 PM
  4. Message class ** Need help befor 12am tonight**
    By TransformedBG in forum C++ Programming
    Replies: 1
    Last Post: 11-29-2006, 11:03 PM
  5. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 09:54 AM