reading into Struct from file

This is a discussion on reading into Struct from file within the C++ Programming forums, part of the General Programming Boards category; I need to open a file that looks something like this: 45, Owen, Terrance, Tight End, Green Bay Packers 35, ...

  1. #1
    Shake Zula- The Mic Rula!
    Join Date
    Sep 2004
    Posts
    69

    reading into Struct from file

    I need to open a file that looks something like this:

    45, Owen, Terrance, Tight End, Green Bay Packers
    35, Green, Josh, Quarterback, New England Patriots

    and so on...

    i need to read this information into a structure i guess, along the lines of:

    Code:
    struct newInfo
    {
    
         int number;
         char lastName [];
         char firstName [];
         char position [];
         char team [];
    }
    how would this be done??

  2. #2
    Registered User Micko's Avatar
    Join Date
    Nov 2003
    Posts
    712
    What have you done so far?

    Please post some code and we'll help!

    - Micko
    Gotta love the "please fix this for me, but I'm not going to tell you which functions we're allowed to use" posts.
    It's like teaching people to walk by first breaking their legs - muppet teachers! - Salem

  3. #3
    Shake Zula- The Mic Rula!
    Join Date
    Sep 2004
    Posts
    69
    here's all i have so far... as soon as i get the info into the struct properly, i'll be well on my way to doing what i need with the information... here's the code...

    Code:
    #include <iostream>
    #include <stdlib.h>
    #include <math.h>
    #include <fstream.h>
    #include <string>
    
    
    
    struct initInfo
    {
    	int number;
    	char lastName [20];
    	char firstName [20];
    	char position [5];
    	char college [20];
    	char nflTeam [30];
    
    };
    
    
    int main()
    {
    
    
    
    	ifstream playersFile;
    	initInfo info;
    	char c;
    
    	playersFile.open ("players.txt");
    
    	if (playersFile.fail())
    	{
    		cout<<"An error occured while trying to open the file."<<endl;
    		system ("PAUSE");
    		exit (1);
    	}
    
    	cout<<"File opened"<<endl;
    	
    	
    	
    
    	while (!playersFile.eof())
    	{
    		cin.getline(info.number, ',');
    		cin.getline(info.lastName, ',');
    	}
    
    	
    	playersFile.close();
    
    	cout<<"number is: "<<info.number<<endl;
    	cout<<"last name is: "<<info.lastName<<endl;
    
    
    
    return 0;
    
    }

  4. #4
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,798
    Overload the stream extraction operator (>>) for the struct:

    Code:
    struct initInfo
    {
        int number;
        char lastName [20];
        char firstName [20];
        char position [5];
        char college [20];
        char nflTeam [30];
    };
    
    istream& operator>>(istream& is,initInfo& rhs)
    {
        // Your code here... maybe use "getline" as a start
        // followed by some further processing
    }
    
    int main()
    {
        initInfo info;
        ifstream playersFile;
    
        ...
    
        while ( playersFile >> info )
        {
            cout<<"number is: "<<info.number<<endl;
            cout<<"last name is: "<<info.lastName<<endl;
            ...
        }
    
        ...
    
    }
    Maybe overload the stream insertion operator (<<) as well...?

    Code:
    #include <iostream>
    #include <stdlib.h>
    #include <math.h>
    #include <fstream.h>
    #include <string>
    I'd suggest replacing those with these:

    Code:
    #include <iostream>
    #include <cstdlib>
    #include <cmath>
    #include <fstream>
    #include <string>
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  5. #5
    Shake Zula- The Mic Rula!
    Join Date
    Sep 2004
    Posts
    69
    What?? I don't understand what you mean by overloading the operator?? and what is

    istream& operator>>(istream& is,initInfo& rhs)

    What is rhs? Can you explain what this line does?

  6. #6
    Super Moderator
    Join Date
    Sep 2001
    Posts
    4,913
    istream& operator>>(istream& is,initInfo& rhs)
    That is overloading the operator. It's basically a function prototype. The name of the function is operator>>, which tells the compiler that you're overloading the operator, ">>". When you overload an operator you define behavior for it so you can use it with your own data types. Inside the parentheses are obviously the parameters - which will be the operands used in the statement with your operator.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Link List math
    By t014y in forum C Programming
    Replies: 17
    Last Post: 02-20-2009, 05:55 PM
  2. having trouble reading into Struct from File
    By bcianfrocca in forum C++ Programming
    Replies: 9
    Last Post: 09-06-2005, 10:54 PM
  3. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 09:54 AM
  4. Need a suggestion on a school project..
    By Screwz Luse in forum C Programming
    Replies: 5
    Last Post: 11-27-2001, 01:58 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21