Mail Archives: djgpp/1999/07/09/00:38:01
I was trying to execute the code developed and compiled in RHIDE.
Application was giving error while opening the file(say temp.dat) in write
mode, though the file exists. So I changed the file opening mode from
O_WRONLY to O_WRONLY|O_CREAT, deleted the file(say temp.dat).Executed the
changed code. First time it runs fine but on executing it for the second
time, encountered file opening error in write mode.
What can be the possible reason ??
Below is the piece of code, I was trying to execute. Is there anything wrong
in the code??
#include<stdio.h>
#include<unistd.h>
#include<fcntl.h>
#include<sys/stat.h>
typedef struct
{
int magic;
char c;
}test_struct;
main()
{
int fd;
test_struct test;
test_struct in_data,out_data;
_fmode=O_BINARY;
in_data.magic=10;
in_data.c='A';
if(( fd = open("temp.dat", O_WRONLY)) < 0)
{
printf("Not able to open the file for writing \n");
return;
}
write(fd,&in_data,sizeof(in_data));
close(fd);
if(( fd = open("temp.dat", O_RDONLY)) < 0)
{
printf("Not able to open the file for reading \n");
return;
}
read(fd,&out_data,sizeof(out_data));
close(fd);
printf("Output magic %d and char %c \n",out_data.magic,out_data.c);
}
Regards
Poonam
- Raw text -