Thread: Having trouble with lseek and file pointer

  1. #1
    Registered User
    Join Date
    Jul 2015
    Posts
    33

    Having trouble with lseek and file pointer

    I have a file formatted something like this:

    Code:
    HERO NAME: <hero_name>
    EQUIPMENT 1: <equip_1>
    EQUIPMENT 2: <equip_2>
    EQUIPMENT 3: <equip_3>
    EQUIPMENT 4: <equip_4>
    ...
    I want to read that file, skip the "HERO NAME: " part and just get the name; same with the equipment. But I'm having trouble with lseek and the file pointer. It seems every time the file is read, the pointer is at a different place?

    Code:
    while ((dirEntry = readdir(dir)) != NULL) {
            if (strcmp(dirEntry->d_name, ".") == 0 || strcmp(dirEntry->d_name, "..") == 0) {
                // do nothing
            } else {
                sprintf(filename, "%s/%s", dirName, dirEntry->d_name);
                fileDesc = open(filename, O_RDONLY);
    
                if (fileDesc < 0) {
                    perror("Error: ");
                    exit(EXIT_FAILURE);
                }
                
                // skip the text "ROOM NAME: "
                lseek(fileDesc, 11, SEEK_CUR);
                
                int j = 0, offset = 0;
                char content[MAX_FILECONTENT_LENGTH];
    
                // read until newline is encountered
                while ((nRead = read(fileDesc, content, 1)) > 0) {
                    if (content[j] == '\n') {
                        content[j] = '\0';
                    }
                   
                    j++;
                }
            }
        }
    I just cannot accurately grab the name that I want.
    Last edited by jjwooyoung; 10-17-2016 at 11:28 PM.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by jjwooyoung
    I want to read that file, skip the "HERO NAME: " part and just get the name; same with the equipment. But I'm having trouble with lseek and the file pointer. It seems every time the file is read, the pointer is at a different place?
    It looks like you are overwriting your content, i.e., instead of:
    Code:
    while ((nRead = read(fileDesc, content, 1)) > 0) {
    you should have written:
    Code:
    while ((nRead = read(fileDesc, &content[j], 1)) > 0) {
    Actually, I would restructure the whole loop as it presently doesn't account for the loop ending before the newline is reached, and relies too much on the content length actually being less than MAX_FILECONTENT_LENGTH. For example:
    Code:
    // read until newline is encountered
    while (j < MAX_FILECONTENT_LENGTH - 1 && (nRead = read(fileDesc, &content[j], 1)) > 0 && content[j] != '\n) {
        j++;
    }
    content[j] = '\0';
    I don't think your lseek trick is going to work for equipment though, since you presumably have to handle skipping both "EQUIPMENT 1: " and "EQUIPMENT 10: ". But it should be easy to read up to ": " and discard whatever came before that on each line.

    You should also close the file when done. It would be better if you moved the entire opening/reading/closing of each file into a separate function. This way, you can more easily test it separately.

    EDIT:
    We don't get to see the declaration of filename, so maybe it is a non-problem, but you otherwise have a buffer overflow vulnerability in your use of sprintf to construct the filename. You should use snprintf, or (double) check that the combined lengths of dirName, "/", and dirEntry->d_name do not exceed the maximum length for filename before calling sprintf.
    Last edited by laserlight; 10-18-2016 at 12:16 AM.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    Using seek on a text file is fraught with all sorts of difficulties, not least of which is the variable length of each line.

    Try something like this, if all your tags like "HERO NAME" are well formed.
    Code:
    while ( fgets( buff, sizeof(buff), fp ) ) {
      if ( sscanf(buff, "HERO NAME: %s", hero ) == 1 ) {
      }
    }
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. writing to a file after using lseek
    By bos1234 in forum C Programming
    Replies: 5
    Last Post: 06-11-2012, 12:40 PM
  2. lseek. Positioning cursor at end of file problem
    By bos1234 in forum C Programming
    Replies: 4
    Last Post: 04-01-2012, 12:09 PM
  3. Trouble with file pointer
    By nirvana21 in forum C++ Programming
    Replies: 25
    Last Post: 01-26-2009, 01:05 PM
  4. write a word to file by using lseek and mmap
    By SoFarAway in forum C Programming
    Replies: 1
    Last Post: 03-28-2005, 01:33 PM
  5. What does lseek do?
    By simly01 in forum C++ Programming
    Replies: 0
    Last Post: 07-29-2002, 03:45 PM

Tags for this Thread