LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 12-11-2006, 08:42 AM   #1
sharathkv
Member
 
Registered: Jul 2003
Distribution: HP-UX
Posts: 35

Rep: Reputation: 15
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)
 {
   ... 
 }

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
 
Old 12-11-2006, 09:02 AM   #2
Broder
Member
 
Registered: Oct 2006
Location: Santa Barbara, C.A.
Distribution: Mood dependent
Posts: 117

Rep: Reputation: 15
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.
 
Old 12-11-2006, 09:06 AM   #3
sharathkv
Member
 
Registered: Jul 2003
Distribution: HP-UX
Posts: 35

Original Poster
Rep: Reputation: 15
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
 
Old 12-11-2006, 09:26 AM   #4
matthewg42
Senior Member
 
Registered: Oct 2003
Location: UK
Distribution: Kubuntu 12.10 (using awesome wm though)
Posts: 3,530

Rep: Reputation: 65
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;
 
Old 12-11-2006, 09:33 AM   #5
sharathkv
Member
 
Registered: Jul 2003
Distribution: HP-UX
Posts: 35

Original Poster
Rep: Reputation: 15
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
 
Old 12-11-2006, 10:08 AM   #6
matthewg42
Senior Member
 
Registered: Oct 2003
Location: UK
Distribution: Kubuntu 12.10 (using awesome wm though)
Posts: 3,530

Rep: Reputation: 65
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;
}
 
Old 12-11-2006, 11:14 PM   #7
Wim Sturkenboom
Senior Member
 
Registered: Jan 2005
Location: Roodepoort, South Africa
Distribution: Ubuntu 12.04, Antix19.3
Posts: 3,794

Rep: Reputation: 282Reputation: 282Reputation: 282
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.

Last edited by Wim Sturkenboom; 12-11-2006 at 11:30 PM.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
What is meant by " file > /dev/null 2>&1 </dev/null " attockonian Linux - Newbie 5 06-30-2006 10:51 PM
How to Check to see if a file has Null Data farmerjoe Programming 3 03-15-2005 02:43 PM
how do u check if password has enough characters?? luxpops Programming 4 09-12-2004 04:30 AM
Help! Null Characters to spaces FoxyNewbie Programming 7 05-27-2004 12:10 PM
replace null characters in a file Philipp Programming 2 09-20-2001 02:29 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 08:30 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration