I'm trying to read a file into a vector using structure...

#include <fstream>
#include <iostream>
#include <vector>
#include <cstdlib>
using namespace std;

struct StudentRecord
{	
		int ID;
		char FirstName[30];
		char LastName[30];
		char Grade[1];
  };
  
void read_file(char FileName[20],vector<StudentRecord> &students)
{
               
               ifstream Students(FileName, ios::in);
			   char S_ID[10];
              /* char FName[30];
               char LName[30];
               char S_Grade[1];*/
               
               StudentRecord entry;
               
               do
               {		   Students.getline(S_ID,10, '-');
                           Students.getline(entry.FirstName,30, '-');
                           Students.getline(entry.LastName,30, '-');
                           Students.getline(entry.Grade,1, '-');
						   
						   entry.ID = atoi(S_ID);
                           students.push_back(entry);

               }
			   while(!Students.eof());
               
}

void print_order(vector<StudentRecord> &students)
{
       
     cout << "\n\Panther ID\t First Name\tLast Name\tGrade" << endl;
     
     vector<StudentRecord>::iterator iter;
     
     for(iter = students.begin();iter < students.end(); iter++)
     {
		cout << iter->ID << "\t" << iter->FirstName << "\t" << iter->LastName<< "\t" << iter->Grade <<endl;
                   
     }
     
     cout << "-------------------------------------------------------" << endl;
}               


int main(int argc, char *argv[])
{ 
	vector<StudentRecord> students;
	char choice;
	char FileName[20];


do
    {
         cout << "\n1. Enter a file name to read from" << endl;
         cout << "2. Print the Order Record" << endl;
         cout << "Q. Quit" << endl;
         cin >> choice;
         
         switch(choice)
         {
                       case '1':
                            
                            cout << "Please enter the name of a file: " << endl;
                            cin >> FileName;
                            
                            read_file(FileName, students);
                            break;
                            
                       case '2':
                            
                            print_order(students);
                            break;
                            
                       case 'q':
                            
                            choice = 'Q';
                            
                       case 'Q':
                            
                            break;
                            
                       default:
                               
                            cout << "Invalid Choice" << endl;
                            
         }
         
    }
	while(choice != 'Q');
    
    
    system("PAUSE");
    return EXIT_SUCCESS;
} 



  /*  cout << "Enter the name of the file you want to open: ";
    cin >> FileName;
    ifstream Students(FileName);
   
while (!Students.eof())
	{ 
	Students >>  ID >> FirstName >>LastName >> Grade;
			cout << "\nPanther ID: " << ID;
			cout << "\nFirst Name:  " << FirstName;
			cout << "\nLast Name:  " << LastName;
			cout << "\nGrade: "<< Grade;
			cout << "\n";
				
         Students.get();	
       
			
		
	}	


  
	

	return 0;
}
*/
VernonDozier commented: Code tags on first post! +16

Thank you for using code tags! (+rep)

What's the question? Does it compile? Does it run? Does it run to completion, but give bad results? Also, please provide a short input file.

It compiles but it does not print my text file. I have a text file with 10 students (first name, last name), student ID and grade. When I call the read_file function the program just runs. Then when I call the print_order function it prints nothing.

It compiles but it does not print my text file. I have a text file with 10 students (first name, last name), student ID and grade. When I call the read_file function the program just runs. Then when I call the print_order function it prints nothing.

Are you getting an infinite loop? Stick a cout statement in the do-while(!Students.eof()); and see if it's an infinite loop. What's the exact input file?

I think you should make grade a char, not a char[1] array. If you make it a C-String, it should have a null terminator, so you'd have to have to have the array be at least 2 characters (1 for the character, 1 for the null terminator).

I'm not sure what the file looks like, but my guess is that you should change Grade to a char, read it in with a get rather than a getline, then read till the end of the line with another getline to grab that '\n' at the end of the line, then you'll be at the right spot to read the NEXT line at the top of the loop. Please post the exact input file. Maybe I'm not visualizing it correctly.

Please post the smallest example that you can extract, a sample input, the expected output, and the current output.

This is what I have in the text file "Students". I expect the print_order function to print this file.

1229831
Dirk
Diggler
A
1346587
Jack
Black
B
2378621
Jim
Jones
C
2137382
Sarah
Jacobs
B
1783624
Partice
Knicks
C
1569829
Walter
Redd
C
2569872
Jacobii
Erentry
A
1234567
Jackson
Walters
C
2345678
Alfred
Henry
A
1256378
Joeseph
Xion
A

Students.getline(S_ID,10, '-');

This might be appropriate for a dash delimited file, but it may well cause problems if S_ID is less than 10 char long in a space delimited file, which is what post #6 implies.

Be prepared for problems if you mix getline() and other input methods, like >>, in the same program.

Be prepared for problems if you use the return value of eof() to control loop performance.

If no field (line) will have white space that should be maintained within the program then you don't need to use getline(). You may use getline() if you wish, but you don't _need_ to.

If the input file looks like in post#6, why '-' line terminator (see Lerner's post)? However the code has more serious defect(s).


You should make char Grade[2] and change Grade input field size to 2. It's impossible to get 1-char string into 1-char field (see Vernon's explanation) so after

Students.getline(entry.Grade,1, '-'); // or '\n' ?

the stream is failed and all other input operations are suppressed.
The stream state is not eof at this moment and forever. You have infinite "input" loop (constantly true condition).

Several code defects provoked so bad final:
1. Input into char arrays. Use std::string for text fields.
2. Untested input statements. The program can't detect i/o errors.
3. Bad loop condition: try to avoid eof test only, use more general condition:

while (filestream)

4. Never use eof test to terminate input loops. See what happens if you correct Grade field size: after the last record entered eof() == false (input is stopped by the last '-' char) so yet another loop starting, but the statement

Students.getline(S_ID,10);
...

can't input ID-field (end of file), it's suppressed but the code adds inexistent record to the vector...

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.