LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   How to check for NULL characters in file ? (https://www.linuxquestions.org/questions/programming-9/how-to-check-for-null-characters-in-file-509377/)

sharathkv 12-11-2006 08:42 AM

How to check for NULL characters in file ?
 
1)
Code:

  FILE *fp;
  long len;
  char *buf;
  fp=fopen("test.txt","rb");
  fseek(fp,0,SEEK_END);    //go to end
  len=ftell(fp);          //get length
  fseek(fp,0,SEEK_SET);    //go to beginning.
  buf=(char *)malloc(len); //malloc buffer
  fread(buf,len,1,fp);    //read into buffer
 
  if ( buf != NULL )
  {
        ...
        ...

'buf' contains NULL characters ( say 4 NULL characters are printed in the file ). len return 4.

The code within

Code:

if (buf != NULL)
 {
  ...
 }

:confused:
is being executed. How do I check if a file contains only NULL characters? ( file length > 0, file contents = NULL )

2)
How do I ensure ^M characters are not printed at the end of each line of the file I write into?

Tried appending Carriage return (13) at the end of each line. Not working...??

Thanks

Broder 12-11-2006 09:02 AM

In vi type (in escape mode)
:set list

This will show any ^M characters and line breaks tabs etc.

If you run

dos2unix filename newfilename
it should get rid of the ^M characters.

sharathkv 12-11-2006 09:06 AM

Thanks for the reply.

I know how to get rid of ^M from vi.

I would like to programatically get rid of it,when I am printing to a file using fprintf(...)?

Thanks

matthewg42 12-11-2006 09:26 AM

You could use a perl script:
Code:

#!/usr/bin/perl -w

use strict;

my $null_found = 0;

foreach my $file (@ARGV) {
    if ( ! open(F, "<$file") ) {
        warn "couldn't open $file for reading: $!\n";
        next;
    }

    while(<F>) {
        if ( /\000/ ) {
            print "detected NULL at line $. in file $file\n";
            $null_found = 1;
            last;
        }
    }
    close(F);
}

exit $null_found;


sharathkv 12-11-2006 09:33 AM

Thanks for the reply.

This is a small peice of code,I posted from a large C program with a number of files & the program would continue to execute after the mentioned piece of code is executed.

So I wouldn't be able to use external programs like perl,awk,sed,etc. I need to do this in plain old C :)

Thnks

matthewg42 12-11-2006 10:08 AM

Code:

#include <stdio.h>

int main(int argc, char** argv)
{
    int i, c;
    FILE* IN;
    int foundnull = 0;

    for(i=1; i<argc; i++) {
        IN = fopen(argv[i], "r");
        if ( IN == NULL ) {
            fprintf(stderr, "WARNING: cannot open %s for reading\n", argv[i]);
        }
        else {
            while( (c = fgetc(IN)) != EOF  ) {
                if ( c == 0 ) {
                    printf("detected NULL at offset %ld in file %s\n", ftell(IN), argv[i]);
                    foundnull = 1;
                    fseek(IN, 0, SEEK_END);
                }
            }
            fclose(IN);
        }
    }

    return foundnull;
}


Wim Sturkenboom 12-11-2006 11:14 PM

Code:

FILE *fp;
  long len;
  char *buf;
  fp=fopen("test.txt","rb");
  fseek(fp,0,SEEK_END);    //go to end
  len=ftell(fp);          //get length
  fseek(fp,0,SEEK_SET);    //go to beginning.
  buf=(char *)malloc(len); //malloc buffer
  fread(buf,len,1,fp);    //read into buffer
 
  if ( buf != NULL )
  {
        ...
        ...

Unless malloc failed, buf is not NULL. Your last few lines should be used to check if malloc failed before you even try to read data into buf.
To check if there is one or more null characters, I would use strlen(buf). If that's smaller than the length of the file, you have NULL characteres.

With regards to ^M, use strchr() to search for it (I always have to think which one is which, probably search for '\r').

PS you can also iterate through buf to find NULL in which case you must use
Code:

if(*buf != '\0')
{
}

Please note that I don't use NULL in this case as we basically are checking for characters. It's just a theretical difference.


All times are GMT -5. The time now is 06:18 PM.