There's quite a bit wrong with that program. Surely you received warnings about what you wrote.
You need to provide prototypes for the POSIX functions you're using, stdio.h for snprintf, ...
When you write to the file, you have two serious problems.
1. sz is uninitialised.
2. You're treating the content of data as a pointer to the data to be written to the file. As data is zero, the program should segfault, but it doesn't for some reason.
I am creating a file to store data, but the file does not get created at all.
the ls -lrt command also does not give the file create
Note that I have added some color to your C code above. Then note that the text in red shows that sz is allocated but not initialized. It is then used to specify how many bytes write() is supposed to transfer. And when the number of bytes to be transferred does not match the return code from write() we know that write failed. When write() fails you execute the printf() printing the green text saying that the data was written successfully (even though we know it was not written successfully). And, after write() fails you also unlink() the file (as shown in blue text). Since you removed the file, ls won't find it. And as has already been pointed out, if you want to write the contents of data, you need to use &data instead of (char *) data in the call to write().
Also note that the 3rd argument to write() has type size_t, not int and that the return value of write() is a ssize_t. So, sz should be declared to be a size_t instead of an int and the if statement should typecast sz to a ssize_t for the comparison to the return value from write(). After changing the declaration of sz, if you add the statement:
before your call to write(), you'll probably have better luck.
Since you're calling snprintf() and printf() as shown in magenta, you should add:
as the 1st or 2nd line of your code. And, since your snprintf() statement's format string doesn't contain any conversion specifiers, the integer argument in orange after the format string is ignored and can be removed. And since this is the only place it is used, the #define that defines DEFAULT_ID can also be removed.
New program after alteration:
questions::
1) Is this way of writing integer to the file is correct?
if correct the why i am getting an empty file?
if wrong means how should i write an integer? because i am able to write and string easilt in to the file. the problem comes when the value is an integer.
New program after alteration:
questions::
1) Is this way of writing integer to the file is correct?
if correct the why i am getting an empty file?
if wrong means how should i write an integer? because i am able to write and string easilt in to the file. the problem comes when the value is an integer.
No! You can't use an uninitialized value to specify the number of bytes to write. You shouldn't use functions without providing their function prototypes. You shouldn't use cat to write binary data to a terminal.
The problem is not that you're writing an integer; the problem is that you're telling it to write a random number of bytes because you never set the value of sz AND if data has been written to the file, you're expecting null bytes written to a terminal to be visible.
Add
as a new line at the start of te.c.
Change:
to:
Delete the lines:
Change:
to:
If it completes successfully, use:
instead of:
to display the contents of the file.
This User Gave Thanks to Don Cragun For This Post:
I apologize is this isn't an appropriate post for the 'advanced' UNIX, so please let me know if I should post this under UNIX for dummies, but here's my problem in a nutshell: I having problems creating a mirrored logical volume.
I have created two new physical volumes
... (2 Replies)
Using KSH, I have one text file which just contains a list of distinct references on each line, e.g.;
123456789
987654321
15457544X
164450200
etc. The file will always be called "InputRefs.txt".
The number of distinct refs will be different each time.
For each line (distinct ref) I... (1 Reply)
Hi every one,
I am new to xml data files,I have two xml files with same data but only small difference as shown below
<List>
<number>1101</number>
<Area>inner walls in a room.</Area>
<Detection>less pressure.</Detection>
<reason>
<normal>
<Component Num="15"... (1 Reply)
Hi,
I am getting tired in creating several users in a day. Can anyone have a script to create several users in the same server, as well as, to create one user in several servers.
where i have to put script and how to run the script.
Waiting for your reply.
Thanks in advance (5 Replies)
I recently got a problem with a program. It said that Create a file using shell script and insert details in it and take a String from the user and see whether it matches with any of the details of the String. I know how to create a file, insert details, but hw to check whether the String matches... (2 Replies)
Hello ,
I came up with a new problem during creation of an exceutable file.
My application all the c files and links them to create a new executable file. Till last month everything was fine and yester day i tried to change one file and tried to create an exe file but unsuccesfull. I am... (8 Replies)
Hi to all,
I am facing a strange problem on the Solaris-10 server. I am unable to create/write files on the server. After writing, when I go for the save, then it gives me and 'jag: I/O error'..where 'jag' is a file name. Please suggest me...
Regards,
Jagdish Machhi (2 Replies)
Hiiii,
here is my script--
BackupLocation="$OPTARG"
if ]; then
echo "Either option l or L should be given to $Programname"
echo "$Usage"
echo "$Programname is terminated"
... (1 Reply)
I have a home directory namely /pto/ppa/ridbmw/etl/
Now i have various directorys below the home directory. For eg
from the home directory if i say cd /roddata it changes to that directory. I think its kind of link (I don't know if it's hard or soft). Now i need to create a new directory and... (4 Replies)
hello,
Actually I want to create a user for our brower based custom application for the mail access from our mailserver(linux).
I create user dummy and I granted all the privileages to dummy user and made dummy equivelent to root and if I tried to create a another user logging as dummy ... (1 Reply)