delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/1999/07/09/00:38:01

Message-ID: <F821915ECD30D3119CC3005004267603065BF3@spzmg01.spzexch01>
From: Poonam Jain <poonam DOT jain AT seepz DOT tcs DOT co DOT in>
To: djgpp AT delorie DOT com
Subject: File opening error
Date: Fri, 9 Jul 1999 10:08:27 +0530
MIME-Version: 1.0
X-Mailer: Internet Mail Service (5.5.2448.0)
Reply-To: djgpp AT delorie DOT com

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 -


  webmaster     delorie software   privacy  
  Copyright © 2019   by DJ Delorie     Updated Jul 2019