Printable Version of Topic

Click here to view this topic in its original format

Linuxhelp _ Programming in Linux _ Envz Utilities

Posted by: Klinux Mar 24 2005, 12:43 PM

I'm trying to perform file manipulation using the envz utilities and I'm having problems using envz_add. I have no problem with envz_get, but when I use envz_add to update a value I get a seg fault.

If anyone has used the envz utilities, I would appreciate the help. Below is an example of what I'm trying to do.

pFile = fopen(file, "r+");

if(pFile != NULL)
fseek(pFile, 0, SEEK_END);
size = ftell(pFile);
fseek(pFile, 0, SEEK_SET);

filebuf = malloc(size);
fread(filebuf, size, 1, pFile);

envz_add(&filebuf, &size, key, value); // Seg Fault occurs here filebuf is char *, size is size_t
// key is char[], value is char[]


Posted by: Hemant Mar 25 2005, 04:51 AM

try this...


filebuf = (char *)malloc(size);

The problem might be:

1.Either malloc() has failed
2. Or fread() has failed.

Just tell me which one has actually failed.You can find that out...using strace and gdb.
do a
$strace a.out

Posted by: Klinux Mar 25 2005, 08:43 AM

I figured out the problem. The strings in the file need to be NULL terminated. When creating the test file is there any way to NULL terminate a string?

Posted by: Hemant Mar 28 2005, 05:15 AM

I don't know if that's possible.All the files are EOF terminated and trying to make them NULL terminated is kind of weird.

Why not use this...

while(pFile != EOF)

Powered by Invision Power Board (
© Invision Power Services (