Using read() & open()

Using read() & open()

Post by Ford Prefe » Sun, 30 Sep 2001 06:57:31



Hi, I'm trying to use open and read to open a file and read it's
contents.  The file will contain a bunch of characters.  All I want to
do is open it, read all the characters and find out how many bytes
there were in the file, actually seeing the characters isn't important
to me.  But I can't get it to work, I'll show you what I did.

char buffer[buff_size];
int n, val = 1;

n = open( "filename.dat", val);
read( n, &buffer, sizeof( char) );

ok, this is a stripped down version, and I've tried it several ways,
but no luck.  as for open, I'm not to sure what exactly to put for the
flag, the man command in unix game me a bunch of words, but I just
kept getting back that they weren't recognized when I compiled the
program.
Thanks for any help, and yes, I am pretty new to C.

Ford

 
 
 

Using read() & open()

Post by David Schwart » Sun, 30 Sep 2001 07:26:00



> ok, this is a stripped down version, and I've tried it several ways,
> but no luck.

        What's the problem? Phrases like "doesn't work" and "no luck" don't
help.

        DS

 
 
 

Using read() & open()

Post by Barry Margoli » Sun, 30 Sep 2001 08:02:44




>Hi, I'm trying to use open and read to open a file and read it's
>contents.  The file will contain a bunch of characters.  All I want to
>do is open it, read all the characters and find out how many bytes
>there were in the file, actually seeing the characters isn't important
>to me.  But I can't get it to work, I'll show you what I did.

>char buffer[buff_size];
>int n, val = 1;

>n = open( "filename.dat", val);
>read( n, &buffer, sizeof( char) );

You need to keep reading until read() returns 0 to indicate that you've
reached EOF.  Also, I don't understand why you're using sizeof(char) in the
read() call; it would be more normal to use sizeof(buffer).

Quote:>ok, this is a stripped down version, and I've tried it several ways,
>but no luck.  as for open, I'm not to sure what exactly to put for the
>flag, the man command in unix game me a bunch of words, but I just
>kept getting back that they weren't recognized when I compiled the
>program.

Does the man page show a line beginning with '#include'?  You need to have
that line in your program to get all the macros defined.

--

Genuity, Woburn, MA
*** DON'T SEND TECHNICAL QUESTIONS DIRECTLY TO ME, post them to newsgroups.
Please DON'T copy followups to me -- I'll assume it wasn't posted to the group.

 
 
 

Using read() & open()

Post by eric » Sun, 30 Sep 2001 21:51:11



> Hi, I'm trying to use open and read to open a file and read it's
> contents.  The file will contain a bunch of characters.  All I want to
> do is open it, read all the characters and find out how many bytes
> there were in the file, actually seeing the characters isn't important
> to me.  But I can't get it to work, I'll show you what I did.

> char buffer[buff_size];
> int n, val = 1;

> n = open( "filename.dat", val);
> read( n, &buffer, sizeof( char) );

> ok, this is a stripped down version, and I've tried it several ways,
> but no luck.  as for open, I'm not to sure what exactly to put for the
> flag, the man command in unix game me a bunch of words, but I just
> kept getting back that they weren't recognized when I compiled the
> program.
> Thanks for any help, and yes, I am pretty new to C.

> Ford

why not use stat instead (man 2 stat)? The st_size element of the
structure it returns is the size in bytes.

Eric

 
 
 

Using read() & open()

Post by David N. Richard » Tue, 02 Oct 2001 06:50:24


Firstly, to answer your question about the various arguments that can be
passed to open() , the flags argument that you refer to tells the system
what you intend to do with the open file (i.e. open for reading or
writing or both). You build this argument by or-ing together the various
'O' flags, re-read the man pages for a description of the various 'O'
flags.  Tech docs can be rather terse but you need to get used to them.
As for the rest I'm not quite sure what you mean when you say:
"...but I just kept getting back that they weren't recognized when I
compiled the program."
Are you getting a compile time error? A runtime error? It's unclear.  By
the way there is a book called "Advanced Programming in the UNIX
Environment" by Richard Stevens that describes many of these system calls
in depth with examples of their use...
Dave

> Hi, I'm trying to use open and read to open a file and read it's
> contents.  The file will contain a bunch of characters.  All I want to
> do is open it, read all the characters and find out how many bytes
> there were in the file, actually seeing the characters isn't important
> to me.  But I can't get it to work, I'll show you what I did.

> char buffer[buff_size];
> int n, val = 1;

> n = open( "filename.dat", val);
> read( n, &buffer, sizeof( char) );

> ok, this is a stripped down version, and I've tried it several ways,
> but no luck.  as for open, I'm not to sure what exactly to put for the
> flag, the man command in unix game me a bunch of words, but I just
> kept getting back that they weren't recognized when I compiled the
> program.
> Thanks for any help, and yes, I am pretty new to C.

> Ford