problem in open sys call
Hi,
I have a small prblem i the open call. it fails every time and I dont know why.
thanx in advance,
Joy.M.M
#include<stdio.h>
#include<stdlib.h>
#include<unistd.h>
#include<string.h>
#include<sys/types.h>
#include<sys/stat.h>
#include<fcntl.h>
const char opcode[]="opcode.sheet";
const char target[]="opcode.sheet";
main()
{
char buff[8192],temp[10],bin=0,opcode[3];
int fd,fd1,bytes_read=8192,count,i=0,inter_count=0;
int offset=0,j=0,tempy;
char head[]="OPCODE SHEET";
if((fd=open(opcode, O_RDONLY))==-1)
printf("oops");
if((fd1=open(target, O_RDWR|O_CREAT))==-1)
printf("O");
while(bytes_read==8192)
{
bytes_read=read(fd,buff,8192);
while(inter_count<bytes_read)
{
i=0;
do
{
bin=buff[inter_count];
temp[i]=buff[inter_count];
inter_count++;
i++;
}while(bin!=10);
temp[i++]=0;
j=0;
write(fd1,head,sizeof(head));
for(j=0;j<i;j++)
{
tempy=(int)(temp[j]-0);
offset+=tempy;
}
if(lseek(fd1,offset,SEEK_SET)==-1)
printf("HI");
printf("%s : ",temp);
scanf("%s",opcode);
write(fd1,opcode,sizeof(opcode));
printf("%s %d \n",temp ,offset);
}
}
return(0);
}Quoted 6 lines · joy
I have a small prblem i the open call. it fails every time and I dont know why.
const char opcode[]="opcode.sheet";
char buff[8192],temp[10],bin=0,opcode[3];
==> ^^^^^^
Quoted 2 lines
if((fd=open(opcode, O_RDONLY))==-1)
printf("oops");In the open() call, the variable "opcode" refers to the local variable of that name, which will be uninitialised, and not the global variable which actually contains your filename.
Running your program under strace or a debugger would show that the filename which is passed to open() isn't what you are expecting.
Signature · 1 line
Glynn Clements <[email protected]>
Quoted 16 lines · Glynn Clements
Quoted 9 lines · joy
I have a small prblem i the open call. it fails every time and I dont know why.
const char opcode[]="opcode.sheet";
Quoted 11 lines
Quoted 3 lines
char buff[8192],temp[10],bin=0,opcode[3];==> ^^^^^^
Quoted 4 lines
if((fd=open(opcode, O_RDONLY))==-1) printf("oops");
thanks a lot, Glynn. I seem to be learning so much but must have somehow missed the commonsense classes!
Quoted 9 lines
In the
open()call, the variable "opcode" refers to the local variable of that name, which will be uninitialised, and not the global variable which actually contains your filename.Running your program under strace or a debugger would show that the filename which is passed to
open()isn't what you are expecting.
Quoted 8 lines
I have a small prblem i the open call. it fails every time and I dont know why.
const char opcode[]="opcode.sheet";
main(){ char buff[8192],temp[10],bin=0,opcode[3];
Umm... did you mean to hide the global opcode behind a local which never gets initialised?
Also, you may want to look at the man pages for:
errno(3)
err(3)
strerror(3)
gdb(1)
strace(1)
Steven Smith.
Best way to avoid this problems, effecient use of printf's and yes GDB would
have solved your prblem then.
Quoted 3 lines
if((fd=open(opcode, O_RDONLY))==-1)
printf("oops"); Instead of printf("oops") go with perror(3) will prints cause of the error of the function
like this.
if ( ( fd = open (opcode, O_RDONLY ) ) == -1 )
perror("open Failed:");
this will print the error message prefixed with your message "open Failed"
man perror for manual.
Cheers,
Signature · 2 lines
With Best Regards Vadiraj C S