in reply to Which is the better option?

I have a preference for a method along the lines of the first layout you have mentioned.
However, have you considered using a xml structure and a perl module to manipulate it.
I am very fond of XML::Twig (requires XML::Parser) it has provided a very simple and enjoyable means of organising data and allows for simplifying the way in which I organise and handle data structures. Its kind of interesting and I have found it useful for a wide variety of tasks (especially in terms of organising site content).

You could see a relationship between the first file structure above and a xml file structure, if you stretch your imagination a little.

The flat line file pattern already suggests that students are containers for exams (I don't know whether the students would like that idea) so either way xml allows you to use differing attributes to identify unique objects sharing the same element type, instead of incrementing an identifier such as
pupil1==
Its a bit like saying:

#pupil=A Student|&exam=Maths 1&result=21#pupil=Foo Student|&exam=Scien +ce&result=35|&exam=knitting&result=75#pupil=Joe Bloggs|&exam=Head Sta +nd&result=100

I hope that doesn't fall flat...:)