Thread: how to write a null character to a file

  1. #1
    Registered User
    Join Date
    Feb 2010
    Posts
    50

    how to write a null character to a file

    I have a file descriptor fd, and would like to use it to write a null character \0 to the connected file. I am trying to write this using:

    Code:
    write(fd, '\0', 1);
    however, this doesn't seem to be writing these characters. Does anyone know how I can accomplish this? Thanks!

  2. #2
    Registered User
    Join Date
    Feb 2010
    Posts
    36
    It is working for me.The function write returns 1 as its value which means 1 character written into the file.I tried a program.
    When I opened the created file in vim editor,it showed me the character as ^@.
    You execute it.

    Code:
    #include	<stdio.h>
    #include	<stdlib.h>
    #include	<fcntl.h>
    #include	<sys/types.h>
    #include	<sys/stat.h>
    	int
    main ( int argc, char *argv[] )
    {
            int fd;
    	if((fd=open("Temp",O_WRONLY,0777))<0)
    	{
    		perror("File");
    		exit(1);
    	}
    	printf ( "No of Characters Written:%d\n", write(fd,"\0",1) );
    	return 0;
    }

  3. #3
    Registered User
    Join Date
    Feb 2010
    Posts
    37
    You try the following code
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    #define BUFFER_SIZE 50
    
    int main()
    {
      FILE *pFile = NULL;
      char *filename = "test_write.txt";
      char buffer[80] = "this is for testing ";//this is the data i am going to write into the file
      int buffer_size = BUFFER_SIZE;
    
      size_t str_length = 0;
    
      pFile = fopen(filename, "w"); //opening the file for writing
      if(pFile == NULL)//checking the error in open
      {
        printf("Error opening %s for writing. Program terminated.", filename);
        abort();
      }
    
      str_length = strlen(buffer);
      fwrite(buffer, str_length, 1, pFile); //writing the string to the file
    
      fclose(pFile);
      printf("\nFile write complete\n");
    
      //reading that data from file
      pFile = fopen("test_write.txt","r"); //opening the file for reading
      if(pFile == NULL)
      {
        printf("Error opening %s for writing. Program terminated.", filename);
        abort();
      }
      char data[1024];
      memset(data,'\0',sizeof(data));//initializing with nulls o avoid buffer problems
      fread(data,sizeof(char),34,pFile);//reading the data what i have written to file
      fclose(pFile);
      printf("%s\n",data);//this prints this is for testing
    
    
      pFile = fopen(filename, "w"); //again opening for writing
      if(pFile == NULL)
      {
        printf("Error opening %s for writing. Program terminated.", filename);
        abort();
      }
      memset(data,'\0',sizeof(data)); //array is initialized with null
    
      str_length = strlen(buffer);
      fwrite(data, str_length, 1, pFile); //writing that null data
      fclose(pFile);
      printf("\nNull File write complete\n");
    
      //reading that data from file
      pFile = fopen("test_write.txt","r"); //now opening for reading the null data
      if(pFile == NULL)
      {
        printf("Error opening %s for writing. Program terminated.", filename);
        abort();
      }
      memset(data,'\0',sizeof(data));
      fread(data,sizeof(char),34,pFile); //reading the data to the "data" buffer. Now it contains the null what we have written
      fclose(pFile);
      printf("%s\n",data); //this will print nothing because that is null
    
    
    
    }
    Last edited by thillai_selvan; 03-04-2010 at 10:17 PM.

  4. #4
    Registered User
    Join Date
    Nov 2008
    Location
    INDIA
    Posts
    64

    Thumbs up

    Actually the second argument of write should be a string .But you tried to write a character.Because of that only you can not write the '\0'.You need to give that null as a string.

    Code:
         write(fd, "\0",1);
    Now you open the file in vim editor you will see a character like ^@
    Last edited by karthigayan; 03-04-2010 at 10:15 PM.

  5. #5
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,059
    That should work unless fd isn't open for writing.

  6. #6
    Registered User
    Join Date
    Feb 2010
    Posts
    36
    Yeah,it works when fd is opened as O_WRONLY mode as well as if fd is opened for read and write mode which is O_RDWR flag in open function.

  7. #7
    Registered User
    Join Date
    Feb 2010
    Posts
    50
    perfect. Thanks!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Formatting a text file...
    By dagorsul in forum C Programming
    Replies: 12
    Last Post: 05-02-2008, 03:53 AM
  2. Basic text file encoder
    By Abda92 in forum C Programming
    Replies: 15
    Last Post: 05-22-2007, 01:19 PM
  3. Help with yacc/compiler design/seg fault
    By trippeer in forum C Programming
    Replies: 1
    Last Post: 04-08-2005, 03:43 AM
  4. How can I traverse a huffman tree
    By carrja99 in forum C++ Programming
    Replies: 3
    Last Post: 04-28-2003, 05:46 PM
  5. simulate Grep command in Unix using C
    By laxmi in forum C Programming
    Replies: 6
    Last Post: 05-10-2002, 04:10 PM