having trouble reading into Struct from File

This is a discussion on having trouble reading into Struct from File within the C++ Programming forums, part of the General Programming Boards category; i'm still having trouble reading this info into the proper struct fields... any help would be appreciated... thanks guys... the ...

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

    having trouble reading into Struct from File

    i'm still having trouble reading this info into the proper struct fields... any help would be appreciated... thanks guys...

    the "players.txt" file looks something like this...

    34, Marcus, Allby, TE, University of Illinois, New England Patriots
    54, Thomas, Shelby, QB, Texas A & M, Chicago Bears

    etc...

    The idea is to read number 34 into the "number" field of the struct, the last name into the "last name" field of the struct, and so on... here is the code i have so far, with alot of errors on the "getline" part...

    Code:
    #include <iostream>
    #include <fstream>
    #include <string>
    
    using namespace std;
    
    
    struct initInfo
    {
    	int number;
    	char lastName [20];
    	char firstName [20];
    	char position [5];
    	char college [20];
    	char nflTeam [30];
    
    };
    
    
    int main()
    {
    
    
    	ifstream playersFile ("players.txt");
    
    	initInfo info;
    
    
    	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())
    	{
    
    		getline(playersFile, info.number, ',');
    		getline(playersFile, info.lastName, ',');
    		
    	}
    
    	
    	playersFile.close();
    
    
    
    
    return 0;
    
    }

  2. #2
    Registered User
    Join Date
    Mar 2002
    Posts
    125
    First off I'm pretty sure the getline function is a member of the istream class and the arguments are, in order, the target you want to read to and the character to stop at.
    So you'll probably want to make that

    playersFile.getline((char *)&info.number,11,',');
    playersFile.getline(info.lastName,20,',');
    Typing stuff in Code::Blocks 8.02, compiling stuff with MinGW 3.4.5.

  3. #3
    Shake Zula- The Mic Rula!
    Join Date
    Sep 2004
    Posts
    69
    the line...

    playersFile.getline((char*)&info.number, 11, ',');

    is giving me wild numbers and not what is in the text file... it's giving me memory addresses i believe... something like -88345340228 and then 743920542393 multiple times...

    know why??

  4. #4
    Shake Zula- The Mic Rula!
    Join Date
    Sep 2004
    Posts
    69
    is there a way to get an integer instead of a string or a character using the "getline" function???

  5. #5
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    >is there a way to get an integer instead of a string or a character using the "getline" function???
    No. Read the integer into either a string or char array, then convert it to an int.

  6. #6
    Registered User
    Join Date
    Jan 2005
    Posts
    7,328
    You #include <string>, but you aren't using the C++ string class that the header is for. You could make all the character arrays in your initInfo struct strings instead. Then your version of getline will work (except for the part about the number).

    In this case, I would mix operator >> and getline. I would use playersFile >> info.number; to read in the number, then use players.file.get() to get the comma, then use the calls to getline like getline(playersFile, info.lastName, ','); to get the rest of the information. This way you don't have to convert the string to a number.

    A couple notes about the rest of your code. Using eof() to control your loop is generally a bad idea. It usually leads to your loop executing one too many times. You should use the return value of getline (or operator>> if you take my suggestion) to control the loop. For example:
    Code:
    while (playersFile >> info.number)
    The return value will evaluate to false if there is an error (including if the number couldn't be read due to eof()).

    In addition, you might find problems reading in multiple records if each record does not end in a comma. Pay attention to how you separate each record and how you read it in. I don't know whether this is a problem or not right now, it's just something for you to look out for later.

  7. #7
    Shake Zula- The Mic Rula!
    Join Date
    Sep 2004
    Posts
    69
    new problem!!!

    ok.... i have to compare the data for info.college against all PAC 10 schools, such as Arizona, Arizona State, California, Oregona.... etc...

    what is the best way to compare a single piece of data against multiple values??

    should i set up an array with all the PAC10 schools in it, and then somehow run through the array with the current data in info.college, and if it finds a match, then have it do something??? and if this is the best approach, how is this done??

  8. #8
    Registered User
    Join Date
    Jul 2003
    Posts
    450
    Thats a good idea, use a vector for the pac10 schools then do a comparison. I think this may be the best method unless your dealing with a lot of data in which case you could use a binary search to find the specific school(s) to make your comparison against and the rest is elementry.

  9. #9
    Shake Zula- The Mic Rula!
    Join Date
    Sep 2004
    Posts
    69
    How is this done?? I'm having alot of trouble with it for some reason and i can't find any literature on it??? anyone have any ideas??

  10. #10
    Registered User
    Join Date
    Jul 2003
    Posts
    450
    You can learn all about vectors in the book The C++ Standard Library by Nicolai M. Josuttis.

    I haven't been on the board in somtime but Prelude wrote several tutorials on Binary Trees. It's good to learn about data structures.

    I'm sorry I am not giving you an example but I am very tired.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Updating in a sequential file?
    By Ronnyv1 in forum C Programming
    Replies: 1
    Last Post: 03-24-2009, 04:41 PM
  2. Replies: 1
    Last Post: 12-03-2008, 02:10 AM
  3. sequential file program
    By needhelpbad in forum C Programming
    Replies: 80
    Last Post: 06-08-2008, 01:04 PM
  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