how to write a null character to a file
This is a discussion on how to write a null character to a file within the C Programming forums, part of the General Programming Boards category; I have a file descriptor fd , and would like to use it to write a null character
-
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!
-
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;
}
-
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 09:17 PM.
-
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.
Now you open the file in vim editor you will see a character like ^@
Last edited by karthigayan; 03-04-2010 at 09:15 PM.
-
That should work unless fd isn't open for writing.
-
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.
-
Popular pages Recent additions
Similar Threads
-
By dagorsul in forum C Programming
Replies: 12
Last Post: 05-02-2008, 03:53 AM
-
By Abda92 in forum C Programming
Replies: 15
Last Post: 05-22-2007, 01:19 PM
-
By trippeer in forum C Programming
Replies: 1
Last Post: 04-08-2005, 03:43 AM
-
By carrja99 in forum C++ Programming
Replies: 3
Last Post: 04-28-2003, 05:46 PM
-
By laxmi in forum C Programming
Replies: 6
Last Post: 05-10-2002, 04:10 PM