1

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);
    }           

1 Answer 1

2

There are several ways you can do that. You can use queues or arrays of strings. I'll show you how to do it using arrays of strings because it may be a simplier concept.

First of all, you need the size of the file and the number of lines in the file. I'll leave it to you to figure out how to do that.

Once you have the file_size and the line_count sorted, you will alloc a shared memory space of size file_size + line_count + (line_count + 1) * sizeof(char *) in order to store all the information we need shared.

Declare a variable char **index and set it as index = (char **)shm to define that the beginning of the shared memory block is where our lines index is, and set index[line_count] = NULL so you know later when your index[n] return NULL its because you have reached its end.

Declare a variable char *buffer and set it to buffer = (char *)(&index[line_count+1]) so now we have buffer pointing to the spot in your memory block right after where the index ends. Its where you are going to store the lines.

Now read the lines like this:

int i = 0;
while(!feof(fp)) {
    index[i++] = buffer;
    fgets(buffer, file_size, fp);
    buffer += strlen(buffer) +1;
}

Once its done reading the file, you have it all organized in the index. The first line is index[0], second is index[1] and so on. All lines are separated and null-terminated.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.