Operating System - HP-UX
1831339 Members
2972 Online
110024 Solutions
New Discussion

fgets coredumps on nullbyte in textfile

 
SOLVED
Go to solution
Andreas D. Skjervold
Honored Contributor

fgets coredumps on nullbyte in textfile

Hi

I have some text files that only contains null bytes hex 00.( By error from an application)

When fgets reads these files it core dumps.
buffer is allokated char of 32000

char buffer[32000]
bufferlen = 32000

The file is opened like this
if ((fp = fopen(szparsefile,"rb")) == NULL)
if(fgets(buffer,bufferlen,fp)==NULL) {

HP-UX 11iv1

Any ideas?

rgds
Andreas
Only by ignoring what everyone think is important, can you be aware of what everyone ignores!
9 REPLIES 9
Steven E. Protter
Exalted Contributor
Solution

Re: fgets coredumps on nullbyte in textfile

If its c/c++ check the code for null pointers, or improperly initialized variables.

SEP
Steven E Protter
Owner of ISN Corporation
http://isnamerica.com
http://hpuxconsulting.com
Sponsor: http://hpux.ws
Twitter: http://twitter.com/hpuxlinux
Founder http://newdatacloud.com
Simon Hargrave
Honored Contributor

Re: fgets coredumps on nullbyte in textfile

I've just compiled that code and tested it (though the filename should be quoted but I guess you know this). It worked fine, so I don't know your problem. Perhaps if you posted the whole code? This is what I tested: -

#include

char buffer[32000];
int bufferlen;
FILE* fp;

main()
{
if ((fp=fopen("testfile","rb")) == NULL){
if (fgets(buffer,bufferlen,fp)==NULL){
}
}
}

However shouldn't you really be using fread? fgets will give you a null-terminated buffer, and since your file is full of nulls, how will you know where your read buffer ends?
Andreas D. Skjervold
Honored Contributor

Re: fgets coredumps on nullbyte in textfile

Attached is the textfile with nullbytes
Only by ignoring what everyone think is important, can you be aware of what everyone ignores!
Stephen Keane
Honored Contributor

Re: fgets coredumps on nullbyte in textfile

You are opening a file in binary mode ("rb" in open) then using fgets to read the contents. But fgets is designed for text-mode processing, looking (as it does) for end of line characters to break on (or the size arg, whichever happens first).
TwoProc
Honored Contributor

Re: fgets coredumps on nullbyte in textfile

I agree with Simon - you probably should be using fread. But, you should be able to use fgets - you'll get one returned line per null in the file - which would be the same of using fgetc instead. But, if I needed to read a unknown length of nulls I think I'd put in a while loop to do fgetc until I didn't get any more null characters. If you're skipping a block of known length of nulls - just fseek right around them if you know the byte offset in the file where they stop occuring.
We are the people our parents warned us about --Jimmy Buffett
Dietmar Konermann
Honored Contributor

Re: fgets coredumps on nullbyte in textfile

Hmm, maybe I'm too stupid to read that code sniplet, but shouldn't we check for fp!=NULL?

if ((fp = fopen(szparsefile,"rb")) != NULL)

Otherwise we don't read anything at all, do we?
"Logic is the beginning of wisdom; not the end." -- Spock (Star Trek VI: The Undiscovered Country)
Simon Hargrave
Honored Contributor

Re: fgets coredumps on nullbyte in textfile

Good point on reading the file! But it still works even when I recompiled my test with != (also put a printf in to prove it gets there).

But Andreas, when I asked for you to send the file, I meant the source code, not the file full of nulls ;) That's easy for us to reproduce!
Dietmar Konermann
Honored Contributor

Re: fgets coredumps on nullbyte in textfile

My point was that Andreas never calls fgets(), so the buffer never gets initialized or null-terminated.

char buffer[32000]="";

This could change a lot, I think.
"Logic is the beginning of wisdom; not the end." -- Spock (Star Trek VI: The Undiscovered Country)
Andreas D. Skjervold
Honored Contributor

Re: fgets coredumps on nullbyte in textfile

Hi again.

We found the solution... Steven you had it at once. Uninitialized variables..

Thanks for your help. This forum is great.

rgds
Andreas
Only by ignoring what everyone think is important, can you be aware of what everyone ignores!