I'm trying to read maps
line by line, but I get the error core segmentation fault
error... Can anyone give me some advice on my code? I'm using 3.13.0-32-generic
int main()
{
char buf[512];
FILE *f;
sprintf(buf, "/proc/%d/maps", getpid());
f = fopen(buf, "rt");
while (fgets(buf, 512, f)) {
unsigned int from, to, pgoff, major, minor;
unsigned long ino;
char flags[4];
int ret = sscanf(buf, "%x-%x %c%c%c%c %x %x:%x %lu ", &from, &to, flags[0],flags[1],flags[2],flags[3], &pgoff, &major, &minor,ino);
if (ret != 10)
break;
}
}
You need to pass pointers to the members of the flags
array instead of passing them directly, the same applies to the ino
argument:
sscanf(buf, "%x-%x %c%c%c%c %x %x:%x %lu ", &from, &to,
&flags[0], &flags[1], &flags[2], &flags[3],
&pgoff, &major, &minor, &ino);
Alternatively, scan all characters at once:
sscanf(buf, "%x-%x %4c %x %x:%x %lu ", &from, &to,
flags, &pgoff, &major, &minor, &ino);
Notice the missing &
before flags
as the array is automatically converted into a pointer to its first element when passed to a function.
In the scanf call replace
flags[x]
by
&flags[x]
same goes for ino
.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.