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

  • Thread starter Peter Rothenbuecher
  • Start date
P

Peter Rothenbuecher

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
 
V

Victor Bazarov

Peter said:
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
 
M

mlimber

Victor said:
Peter said:
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-lite/input-output.html#faq-15.1

Cheers! --M
 
P

Peter Rothenbuecher

mlimber said:
Victor said:
Peter said:
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-lite/input-output.html#faq-15.1

Cheers! --M
Ok, thanks.
Peter
 
L

Larry I Smith

Peter said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,482
Members
44,901
Latest member
Noble71S45

Latest Threads

Top