you cant read certain lines of a text file, without reading the contents up to (if reading from the start) or the contents after (if reading from the end). similarly, on a set of 10 stairs, you cant
walk (i.e. not jump) up to stair 5 without going to stairs 4, 3, 2, 1.
since you have some structure to your data, why not use a defined
data structure? since it is just that--
data--and it doesnt have logic, you could define a
struct (opposed to a class, which has logic). if you use a data structure, such as a struct, you can directly access read specific structs (or "sets" as your calling your data structure). the only thing is, you must define a maximum value for the "string name", which you didnt say whether this is a problem or not (besides the fact that the names are variable length). you might define your structure this way:
Code:
struct Set
{
char name[30];
int values[5];
};
you can then use c++ file IO classes (input: ifstream; output: ofstream) to do
binary input and output, respectively. it is binary IO because you are dealing with the atomic (i.e. cannot be broken down) "Set" type you create, rather than the atomic type of "character", as your file is now. also, when viewing the file in some plain-text editor, it is inherently unreadable because it is binary information, not a string of characters as your file is now.
using a struct and the fstream classes, if you wanted to read only the 34th "Set", you could do:
Code:
Set myEntry;
// here declare an 'ifstream' object, 'myFile', for binary input, see the documentation for it
myFile.seekg( 34 * sizeof(Set), ios::beg); // from the beginning of the file, go to the 34th "Set" entry (determined by the number of bytes, i.e. 34 * num bytes of "Set")
myFile.read( &myEntry, sizeof(Set) ); // EDIT: i forgot to mention, you will have to use 'reinterpret_cast' for this, search around for example
if you do it this way, the input of course must be the same format (binary struct). therefore you would have to, once, read the file line by line (linearly, as you probably are now), create a "Set" from each set of 6 lines, or whatever it is, and write all of these "Set"s out to a binary file. after you have the file in the correct format, you can read/write in constant time (i.e. to read the first set takes the same amount of time to read the 1000th set, or Nth set).
however, you could do a similar thing and use plain text files. you would still need to define a data structure (it could just be logical, and not actually any "struct" or "class"). because of this, as with structs, your strings must be a constant size and padded with '\0' null characters or whatever you want, in order to make every "string name" line the same size, say 10 (or whatever) characters
Code:
m y N a m e \0 \0 \0 \n
. doing it like this, keeping it plain text with no binary information or data structures, probably isnt the best approach ('structs' may be). with plain-text, it boils down that every Nth row must be the same length (or padded with information to make it that length). every 6th line (from line 1) must be the same number of characters long. every 6th line (from line 2, i.e. the first number in each set) must be the same number of characters, if your number is 5, you must pad it to make it constant length, such as 005 (if you had numbers such as 123, etc).
i hope this makes sense to you, but, again, the short answer is that you cant go to line N directly. and again, i would recommend structs or some binary data structure.