0
// Filename : SchoolScores.cpp 
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
using namespace std;

		struct student
	{
		string studentid;
	   vector <int> examcode;	

		
	}; 
		int main()
	{
        ifstream stream1 ("1.txt");
        char a[611];
        if(!stream1)
	{
        cout << "While opening a file an error is encountered" << endl;
	}
        else
	{
        cout << "File is successfully opened" << endl;
	}	
        vector <student> students;
        student aStudent;
         
        while (stream1 >> aStudent.studentid)  //part 1
    {
        for (int i = 0; i < 10; i++)       // part 2
    {
        stream1 >> aStudent.examcode[i];
        stream1 >> aStudent.studentid;
    }
        stream1 >> aStudent.examcode[10];     //part 3
        students.push_back (aStudent);
	}
   		stream1.close ();
        for (int i = 0; i < students.size (); i++)
	{  
		cout <<"\n"<< endl;
        cout << students.at (i).studentid << endl;
        for (int j = 0; j < studentid(i).examcode.size (); j++)

        cout <<students.at (i).examcode.at(j)<< endl;
	} 

        return 0; 
	}

So, i must change the student.at to studentid? Is it right?

0

I think at this point it's probably best to simply post the entire program. I think you'll learn more by studying the entire program line by line rather than by me giving hints. You've put effort into this, so I think it's okay to give you the whole program. I have added lots of comments. This is ONE way to do it. It is not the only way to do it. Here is the program:

// Filename : SchoolScores.cpp

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

struct student
{
     string studentid;
     vector <int> examcode;	
};

 
int main()

{
    ifstream stream1 ("Standrews83.txt");

    if(!stream1)
    {
            cout << "While opening a file an error is encountered" << endl;
    }
    else
    {
            cout << "File is successfully opened" << endl;
    }	
		 
    vector <student> students;
    student aStudent;
    string tempStudentID;
    bool readEntireFile = false;     // set to true when reach end of file

           
    stream1 >> tempStudentID;    //  read in student id of first student
    while (!readEntireFile)
    {
            aStudent.studentid = tempStudentID;  // new student
            int tempExamCode;
            aStudent.examcode.clear ();
            stream1 >> tempExamCode;    // read in first exam code for this student
            aStudent.examcode.push_back (tempExamCode);  // add this exam code to current student's vector of exam codes
            
            bool newStudent = false;   // true when a new student id is encountered
            while (!newStudent && !readEntireFile)  
            {
                  if (stream1 >> tempStudentID)   // successfully read in student id
                  {
                       if (tempStudentID.compare (aStudent.studentid) == 0)  // student id is same as before
                       {
                            stream1 >> tempExamCode;   // read in exam code
                            aStudent.examcode.push_back (tempExamCode); // add this exam code to this student;s vector of exam codes
                       }
                       else
                            newStudent = true;   // student id is different from before.  Therefore new student.
                  }
                  else
                       readEntireFile = true;  // end of file reached.  Want to exit inner and outer while loops
            }  // if new student, do not repeat this while loop

            students.push_back (aStudent);   // no more exam codes for this student.  Add aStudent to students vector
    }
    stream1.close ();  // We have read the entire file, so time to close it.


                   
    for (int i = 0; i < students.size (); i++)
    {
            cout << students.at (i).studentid << endl;   // output student id
            for (int j = 0; j < students.at (i).examcode.size (); j++)
                 cout << students.at (i).examcode.at (j) << endl;   // output list of exam codes for this student
    } 

    return 0; 
}

A lot of the lines go past the boundaries due to the comments, so you'll view it best if you use the "Toggle Plain Text" to get rid of the line numbers, then copy and paste the code into Dev C++ or some other C++ IDE program or print it out. As you can see, the part where you read in the input file has gotten longer and more complex, but it's roughly the same idea as before when you had exactly eleven exam codes. Here is the text file that I used to try out this program:

s1 0004
s1 0024
s1 0010
s1 0105
s1 0116
s1 0135
s1 0138
s442 0403
s442 0015
s442 0036
s442 0056
s442 0071
s442 0083
s442 0097
s442 0106
s442 0118
s442 0135
s442 0138
s43 0003
s43 0010
s43 0831
s43 0051
s43 0071
s43 0105
s43 0133
s43 0135
s43 0338

As you can see, there are three students, each with a different length name and a different number of exam codes. Here are the results when I ran it:

File is successfully opened
s1
4
24
10
105
116
135
138
s442
403
15
36
56
71
83
97
106
118
135
138
s43
3
10
831
51
71
105
133
135
338

This output matched the input as far as I can tell, so the program appears to work. You should try it out for yourself with this program and this input and make sure it works for you too. THEN try it on the real input file, which is much larger. Keep in mind my assumptions on this input file:

1) Every line has a student name followed by an exam code which is an integer.
2) Student ids are contiguous (all next to each other in the file ) and not interspersed.
3) The input file is not empty.

I'm not sure if you are doing this for school, for work, on your own, whether this a real project or just a project you are doing to learn C++. Anyway, please try it out, make sure it works for you, then print it out and study it line by line. It may take several hours before you get a feel for it. Good luck!

0

it successfully read..but when i close and compile again..it only appear some of the data..i dont know what happen..it makes me had to copy paste again data into a new file..i have tried what you told before..

0

The copy and paste makes absolutely no sense to me. This is a text file. This is an ifstream, not an ofstream. The input text file should not change at all when you run the program. Cutting and pasting into a new text file should not be needed since the file should remain 100% unchanged when you run the program.

Do this:

1) Get the text file the way it should be and save it. Close any text editor you have open.
2) Compile, but do not run, the program. Close whatever C++ IDE you compiled the program with.
3) Get a command line and go to the directory where the executable program and the data file is.
4) type "more Standrews83.txt"
5) If you see a bunch of weird characters when you do that, you have a corrupted text file. This could result from having created the text input file in Microsoft Word or Word Pad and not saving it in the "text only" format. Resave the file as text only.
6) If the file looks right, do either the "ls" (if you are in linux) or the "dir" command (if you are in Windows). Note the times of "Standrews83.txt" and the program and write them down.
7) Run the program.
8) Do the "dir" or "ls" command and note the times next to the files again. They should be exactly the same as they were before. If they are not, something very weird has occurred because they should NOT change.
9) If they have not changed (and they should not have), cutting and pasting and redoing the input file makes absolutely no sense to me because the files have not change at all.

My guess is that there is something wrong with your input file. If the program is trying to read in a string and it encounters an integer, the program will give you extremely bad results (it will not change the files though). It does not implement error checking so it assumes that you have provided the text file it is looking for. I provided you with the input file and it worked for me. Try the above steps and let me know.

0

How to this Master Vernon, where to get a command line?

Get a command line and go to the directory where the executable program and the data file is.
4) type "more Standrews83.txt"

0

or maybe ...visual basic cannot produce for big data? i try to compile the large big data which have 16 000 student..but only appear from 15 800 -16000

0

Microsoft Windows XP [Version 5.1.2600]
(C) Copyright 1985-2001 Microsoft Corp.

The command prompt has been disabled by your administrator.

Press any key to continue . . .

0

nurulshidanoni, I seriously doubt that it has anything to do with Visual Basic (I assume you mean Visual C++). Visual C++ simply compiles the code. You don't even need to be in Visual C++ to run the code. This thread has gone on for 70 posts and I think it's past time to mark it "solved" and close it. The original question you asked was answered long ago. I assume that you have run the program I provided with a small input file and that it works. Otherwise you would have mentioned that it did not. For a file that huge, the use of the vector rather than an array is probably not advisable. I suppose you could have run out of RAM, though I think you would have gotten a run-time error if you had. I don't know how familiar you are with C++, but based on some of your posts I think this program might be too complex for you at this time and you might want to tackle something a little easier. Best of luck to you and I hope you are successful in whatever you are trying to do with this code.

0

I have know whats the problem. for he ouput , i open a large view, and the output is succesfully appear. When i open with a small view, it cannot appear for the large data.

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.