I'm trying to read a file and put each line into shared memory (yes I know this isn't the most practical way of doing it, but lets just say I have to used shared memory). Is it possible to read these lines into shared memory in such a way that I can quickly jump to a certain line in shared memory?
For example if my file is:
ABCD
EFGH
IJKL
could I jump directly to the 3'rd line in shared memory so that I get "IJKL"?
I'm currently reading it into memory like this:
key_t key; /* key to be passed to shmget() */
int shmflg; /* shmflg to be passed to shmget() */
int shmid; /* return value from shmget() */
int size; /* size to be passed to shmget() */
char *shm, *s;
// we'll name our shared memory segment: 1234
key = 1234;
if((shmid = shmget(key,size, S_IRUSR | S_IWUSR)) < 0){
perror("shmget failed");
exit(1);
}
// attach the segment to our data space
if((shm = shmat(shmid, NULL, 0)) == (char*) -1){
perror("shmat failed");
exit(1);
}
s = shm;
// note: line is a character array that's large enough to include the whole file
while(fgets(line, 128, fp) != NULL){
// try putting the line into our shared memory:
sprintf(s, line);
}