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.