By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,190 Members | 833 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,190 IT Pros & Developers. It's quick & easy.

test.c:24: error: `read' undeclared (first use this function)

P: n/a
Hello,
when I try to compile the following code:
/* This fragment of code is taken from an online tutorial */
#include<stdio.h>
#include<fcntl.h>
#include<stdlib.h>

float bigbuff[1000];

main(int argc, char **argv)

{ int fd;
int bytes_read;
int file_length;

if ( (fd = open(argv[1],O_RDONLY)) = -1)
{ /* error file not open */
perror("Datafile");
exit(1);
}
if ( (bytes_read = read(fd,&file_length,
sizeof(int))) == -1)
{ /* error reading file */
exit(1);
}
if ( file_length > 999 ) {/* file too big */ }
if ( (bytes_read = read(fd,bigbuff,
file_length*sizeof(float))) == -1)
{ /* error reading open */
exit(1);
}
}
I get this error message:

test.c: In function `int main(int, char**)':
test.c:24: error: `read' undeclared (first use this function)
test.c:24: error: (Each undeclared identifier is reported only once for
each function it appears in.)
I have seen, that the function read() isn´t defined in <stdio.h>. Does
anybody can help me? Do you know, if there is defined a function read()
anywhere with the prototype
"int read(int handle, char *buffer, unsigned length)"
and can tell me.

Thanks a lot, if you could help me.

Peter
Dec 9 '05 #1
Share this Question
Share on Google+
4 Replies


P: n/a
Peter Rothenbuecher wrote:
Hello,
when I try to compile the following code:
/* This fragment of code is taken from an online tutorial */
[...]
I get this error message:

test.c: In function `int main(int, char**)':
test.c:24: error: `read' undeclared (first use this function)
test.c:24: error: (Each undeclared identifier is reported only once for
each function it appears in.)
I have seen, that the function read() isn´t defined in <stdio.h>. Does
anybody can help me? Do you know, if there is defined a function read()
anywhere with the prototype
"int read(int handle, char *buffer, unsigned length)"
and can tell me.


There is no function 'read' in Standard C++. It is platform-specific,
and your particular implementation may not even have it. You might want
to read about stream-based file handling (FILE* and 'fXXXX' functions
instead of 'XXXX' ones), those _are_ standard.

V
Dec 9 '05 #2

P: n/a
Victor Bazarov wrote:
Peter Rothenbuecher wrote:
Hello,
when I try to compile the following code:
/* This fragment of code is taken from an online tutorial */
> [...]
I get this error message:

test.c: In function `int main(int, char**)':
test.c:24: error: `read' undeclared (first use this function)
test.c:24: error: (Each undeclared identifier is reported only once for
each function it appears in.)
I have seen, that the function read() isn´t defined in <stdio.h>. Does
anybody can help me? Do you know, if there is defined a function read()
anywhere with the prototype
"int read(int handle, char *buffer, unsigned length)"
and can tell me.


There is no function 'read' in Standard C++. It is platform-specific,
and your particular implementation may not even have it. You might want
to read about stream-based file handling (FILE* and 'fXXXX' functions
instead of 'XXXX' ones), those _are_ standard.


Or iostreams, which are also standard:

http://www.parashift.com/c++-faq-lit....html#faq-15.1

Cheers! --M

Dec 9 '05 #3

P: n/a
mlimber wrote:
Victor Bazarov wrote:
Peter Rothenbuecher wrote:
Hello,
when I try to compile the following code:
/* This fragment of code is taken from an online tutorial */

> [...]

I get this error message:

test.c: In function `int main(int, char**)':
test.c:24: error: `read' undeclared (first use this function)
test.c:24: error: (Each undeclared identifier is reported only once for
each function it appears in.)
I have seen, that the function read() isn´t defined in <stdio.h>. Does
anybody can help me? Do you know, if there is defined a function read()
anywhere with the prototype
"int read(int handle, char *buffer, unsigned length)"
and can tell me.


There is no function 'read' in Standard C++. It is platform-specific,
and your particular implementation may not even have it. You might want
to read about stream-based file handling (FILE* and 'fXXXX' functions
instead of 'XXXX' ones), those _are_ standard.

Or iostreams, which are also standard:

http://www.parashift.com/c++-faq-lit....html#faq-15.1

Cheers! --M

Ok, thanks.
Peter
Dec 9 '05 #4

P: n/a
Peter Rothenbuecher wrote:
Hello,
when I try to compile the following code:
/* This fragment of code is taken from an online tutorial */
#include<stdio.h>
#include<fcntl.h>
#include<stdlib.h>

float bigbuff[1000];

main(int argc, char **argv)

{ int fd;
int bytes_read;
int file_length;

if ( (fd = open(argv[1],O_RDONLY)) = -1)
{ /* error file not open */
perror("Datafile");
exit(1);
}
if ( (bytes_read = read(fd,&file_length,
sizeof(int))) == -1)
{ /* error reading file */
exit(1);
}
if ( file_length > 999 ) {/* file too big */ }
if ( (bytes_read = read(fd,bigbuff,
file_length*sizeof(float))) == -1)
{ /* error reading open */
exit(1);
}
}
I get this error message:

test.c: In function `int main(int, char**)':
test.c:24: error: `read' undeclared (first use this function)
test.c:24: error: (Each undeclared identifier is reported only once for
each function it appears in.)
I have seen, that the function read() isn´t defined in <stdio.h>. Does
anybody can help me? Do you know, if there is defined a function read()
anywhere with the prototype
"int read(int handle, char *buffer, unsigned length)"
and can tell me.

Thanks a lot, if you could help me.

Peter


read() is non-stdandard.

In Windows, include <io.h> and <fcntl.h> -and- the function
name is "_read()". Check your compiler's on-line docs.

In unix/linux (usually), include <unistd.h> -and- the function
name is "read()". Check your compiler's 'man pages'.

Larry
Dec 10 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.