* problem in open sys call
@ 2004-04-20 18:31 joy
2004-04-20 20:06 ` Glynn Clements
` (2 more replies)
0 siblings, 3 replies; 8+ messages in thread
From: joy @ 2004-04-20 18:31 UTC (permalink / raw)
To: linux-c-programming
[-- Attachment #1: Type: text/plain, Size: 112 bytes --]
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
[-- Attachment #2: opgen.c --]
[-- Type: text/plain, Size: 1021 bytes --]
#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);
}
^ permalink raw reply [flat|nested] 8+ messages in thread
* RE: problem in open sys call
@ 2004-04-20 19:22 Huber, George K RDECOM CERDEC STCD SRI
2004-04-21 9:10 ` joy
0 siblings, 1 reply; 8+ messages in thread
From: Huber, George K RDECOM CERDEC STCD SRI @ 2004-04-20 19:22 UTC (permalink / raw)
To: 'gracecott@sancharnet.in', linux-c-programming
Hi,
Try this:
if((fd=open(opcode, O_RDONLY))==-1)
{
printf("Open failed, error code: %d\n%s\n", errno, strerror(errno)
}
you may need to add errno.h and string.h to the list of includes. This will
give you the error code and desctiption of why open is failing.
George Huber
SRI, International
-----Original Message-----
From: linux-c-programming-owner@vger.kernel.org
[mailto:linux-c-programming-owner@vger.kernel.org]On Behalf Of joy
Sent: Tuesday, April 20, 2004 2:32 PM
To: linux-c-programming@vger.kernel.org
Subject: 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
^ permalink raw reply [flat|nested] 8+ messages in thread
* Re: problem in open sys call
2004-04-20 18:31 problem in open sys call joy
@ 2004-04-20 20:06 ` Glynn Clements
2004-04-21 9:00 ` joy
2004-04-20 21:35 ` Steven Smith
2004-04-21 6:14 ` Vadiraj C S
2 siblings, 1 reply; 8+ messages in thread
From: Glynn Clements @ 2004-04-20 20:06 UTC (permalink / raw)
To: gracecott; +Cc: linux-c-programming
joy wrote:
> 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];
==> ^^^^^^
> 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.
--
Glynn Clements <glynn.clements@virgin.net>
^ permalink raw reply [flat|nested] 8+ messages in thread
* Re: problem in open sys call
2004-04-20 18:31 problem in open sys call joy
2004-04-20 20:06 ` Glynn Clements
@ 2004-04-20 21:35 ` Steven Smith
2004-04-21 6:14 ` Vadiraj C S
2 siblings, 0 replies; 8+ messages in thread
From: Steven Smith @ 2004-04-20 21:35 UTC (permalink / raw)
To: joy; +Cc: linux-c-programming
[-- Attachment #1: Type: text/plain, Size: 407 bytes --]
> 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.
[-- Attachment #2: Type: application/pgp-signature, Size: 187 bytes --]
^ permalink raw reply [flat|nested] 8+ messages in thread
* Re: problem in open sys call
2004-04-20 18:31 problem in open sys call joy
2004-04-20 20:06 ` Glynn Clements
2004-04-20 21:35 ` Steven Smith
@ 2004-04-21 6:14 ` Vadiraj C S
2 siblings, 0 replies; 8+ messages in thread
From: Vadiraj C S @ 2004-04-21 6:14 UTC (permalink / raw)
To: gracecott; +Cc: linux-c-programming
Best way to avoid this problems, effecient use of printf's and yes GDB would
have solved your prblem then.
>
> 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,
--
With Best Regards
Vadiraj C S
^ permalink raw reply [flat|nested] 8+ messages in thread
* Re: problem in open sys call
2004-04-20 20:06 ` Glynn Clements
@ 2004-04-21 9:00 ` joy
0 siblings, 0 replies; 8+ messages in thread
From: joy @ 2004-04-21 9:00 UTC (permalink / raw)
To: Glynn Clements; +Cc: linux-c-programming
Glynn Clements wrote:
>joy wrote:
>
>
>
>>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];
>>
>>
>==> ^^^^^^
>
>
>
>> 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!
>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.
>
>
>
^ permalink raw reply [flat|nested] 8+ messages in thread
* Re: problem in open sys call
2004-04-20 19:22 Huber, George K RDECOM CERDEC STCD SRI
@ 2004-04-21 9:10 ` joy
2004-04-21 10:08 ` Holger Kiehl
0 siblings, 1 reply; 8+ messages in thread
From: joy @ 2004-04-21 9:10 UTC (permalink / raw)
To: Huber, George K RDECOM CERDEC STCD SRI; +Cc: linux-c-programming
Huber, George K RDECOM CERDEC STCD SRI wrote:
>Hi,
>
>Try this:
>
> if((fd=open(opcode, O_RDONLY))==-1)
> {
> printf("Open failed, error code: %d\n%s\n", errno, strerror(errno)
> }
>
>you may need to add errno.h and string.h to the list of includes.
This will
>give you the error code and desctiption of why open is failing.
>
>
thanks a lot, george but I think Glynn hit the nail on the head. the
code was working fine before I added the opcode variable in main
and totlly neglected to see it while gdb'ing.
>George Huber
>SRI, International
>
>-----Original Message-----
>From: linux-c-programming-owner@vger.kernel.org
>[mailto:linux-c-programming-owner@vger.kernel.org]On Behalf Of joy
>Sent: Tuesday, April 20, 2004 2:32 PM
>To: linux-c-programming@vger.kernel.org
>Subject: 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
>-
>To unsubscribe from this list: send the line "unsubscribe
linux-c-programming" in
>the body of a message to majordomo@vger.kernel.org
>More majordomo info at http://vger.kernel.org/majordomo-info.html
>
>
>
^ permalink raw reply [flat|nested] 8+ messages in thread
* Re: problem in open sys call
2004-04-21 9:10 ` joy
@ 2004-04-21 10:08 ` Holger Kiehl
0 siblings, 0 replies; 8+ messages in thread
From: Holger Kiehl @ 2004-04-21 10:08 UTC (permalink / raw)
To: gracecott; +Cc: Huber, George K RDECOM CERDEC STCD SRI, linux-c-programming
On Wed, 21 Apr 2004, joy wrote:
> Huber, George K RDECOM CERDEC STCD SRI wrote:
>
> >Hi,
> >
> >Try this:
> >
> > if((fd=open(opcode, O_RDONLY))==-1)
> > {
> > printf("Open failed, error code: %d\n%s\n", errno, strerror(errno)
> > }
> > >you may need to add errno.h and string.h to the list of includes. This
> will
> >give you the error code and desctiption of why open is failing.
> >
> >
> thanks a lot, george but I think Glynn hit the nail on the head. the
> code was working fine before I added the opcode variable in main
> and totlly neglected to see it while gdb'ing.
>
But George and others also made a good point. With programming it is always
good practice to write good error messages and always try to be more
verbose. Believe me this can save you a lot of time! I even go so far
to always print out __FILE__ and __LINE__, so I know exacly where the
error has happened.
Holger
^ permalink raw reply [flat|nested] 8+ messages in thread
end of thread, other threads:[~2004-04-21 10:08 UTC | newest]
Thread overview: 8+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2004-04-20 18:31 problem in open sys call joy
2004-04-20 20:06 ` Glynn Clements
2004-04-21 9:00 ` joy
2004-04-20 21:35 ` Steven Smith
2004-04-21 6:14 ` Vadiraj C S
-- strict thread matches above, loose matches on Subject: below --
2004-04-20 19:22 Huber, George K RDECOM CERDEC STCD SRI
2004-04-21 9:10 ` joy
2004-04-21 10:08 ` Holger Kiehl
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).