CASTing

CASTing

Post by Thomas Ravnho » Fri, 20 Sep 1991 16:24:21



Hello !

 I am a novice C-programmer, and I am having
 trouble with the systemcalls write/open.

 Please look at the following source-code :

 void main()
 {
   struct person
   {  char name[30];
      int age;
      float weight;
   };
   register int fd;
   struct person ps;
   int rs;

   copystr("Mr.Johnson",ps.name);
   ps.age=52;
   ps.weight=82;

  fd=open("file",1);
  if(fd==-1)
  {
    printf("Could not open file\n");
    exit(1);
  }

  rs=write(fd,(char *)ps,sizeof(struct person));
  if(rs==-1)
  {
    printf("Could not write to file\n");
  }
  (void)close(fd);
 }

 But, the compiler says :

  CAST is not a permitted struct/union operation

 Well, what can I do instead of CASTing ??
 If you'll answer this I'll be very happy.

 If you have a sample-code using fopen,fwrite,
 please mail it to me.

 Thanks in advance.

 
 
 

CASTing

Post by Wing Keung F » Sat, 21 Sep 1991 08:11:12


ok, i made some additions to the initial file, they include the
#include
files, the name "sys/file.h" is from the K&R C 2nd edition. and i
have
changed the copystr() to strcpy(), and lastly, i added a third
argument
to open() which was open("file", 1)

Quote:>#include<stdio.h>
>#include<string.h>
>#include<sys/file.h>

> void main()
> {
>   struct person
>   {  char name[30];
>      int age;
>      float weight;
>   };
>   register int fd;
>   struct person ps;
>   int rs;

>   (void)strcpy(ps.name, "Mr.Johnson");
>   ps.age=52;
>   ps.weight=82;

>  fd=open("file",O_WRONLY, 0);
>  if(fd==-1)
>  {
>    printf("Could not open file\n");
>    exit(1);
>  }

>  rs=write(fd,&ps,sizeof(struct person));
>  if(rs==-1)
>  {
>    printf("Could not write to file\n");
>  }
>  (void)close(fd);
> }

it compiles (cc/gcc), and does write some garbage to the file "file".
in run time, if "file" does not exist, it could not open file,
that is consistent with the C book, because you have to "create()"
a new file.  for a pre-existing file, it pre-pends the bytes to
the file.

personally, i would use fopen, fclose, fprintf, fscanf though.
PS: hi thomas, i do drink danish beer  8-)

--

                                        FRed W. K. Au
                                        An "Elephant" drinker, not Bud
                                        ...!{ece,cs,ri}.cmu.edu!wkau

 
 
 

CASTing

Post by Greg Hu » Fri, 20 Sep 1991 23:22:07



>  I am a novice C-programmer, and I am having
>  trouble with the systemcalls write/open.

>  Please look at the following source-code :

>  [part of code deleted]

>    copystr("Mr.Johnson",ps.name);

>  [part of code deleted]

>   rs=write(fd,(char *)ps,sizeof(struct person));

>  But, the compiler says :

>   CAST is not a permitted struct/union operation

>  Well, what can I do instead of CASTing ??
>  If you'll answer this I'll be very happy.

You're thinking right that you have to cast something to use the
structure there, but it's what you're casting that is wrong.  As it
is, you're trying to cast the contents of the structure, which is
indeed illegal.  The write system call wants a pointer to the data,
not the data itself.

What you want to cast here is a pointer to the structure, like this:

    (char *) &ps

You might consider getting a C book that explains how to use the C
language, including pointer manipulation.  One good one that I like is:

   "The C Programming Language", by Brian Kernighan and Dennis Ritchie
   ISBN 0-13-110362-8

Second, unless your system's C libraries have a function called
copystr, that line's not going to work either.  Try this instead:

    strcpy (ps.name, "Mr.Johnson");

For more details about the standard C string routines, try this:

    man strcpy | more
    man strcat | more
    man strcmp | more
    man strlen | more

Enjoy!

--

DG/UX Kernel Development         UUCP:     {world}!mcnc!rti!dg-rtp!hunt
Data General Corporation
Research Triangle Park, NC, USA  These opinions are mine, not DG's.