Code:
Example
/* READ.C: This program opens a file named
* READ.C and tries to read 60,000 bytes from
* that file using _read. It then displays the
* actual number of bytes read from READ.C.
*/
#include <fcntl.h> /* Needed only for _O_RDWR definition */
#include <io.h>
#include <stdlib.h>
#include <stdio.h>
char buffer[60000];
void main( void )
{
int fh;
unsigned int nbytes = 60000, bytesread;
/* Open file for input: */
if( (fh = _open( "read.c", _O_RDONLY )) == -1 )
{
perror( "open failed on input file" );
exit( 1 );
}
/* Read in input: */
if( ( bytesread = _read( fh, buffer, nbytes ) ) <= 0 )
perror( "Problem reading file" );
else
printf( "Read %u bytes from file\n", bytesread );
_close( fh );
}
evo primera za read direkt iz MSDN-a
EOF i WEOF su definisani u visual studiu, i mogu da se koriste uz getc fgetc i slicne funkcije:
Code:
/* GETC.C: This program uses getchar to read a single line
* of input from stdin, places this input in buffer, then
* terminates the string before printing it to the screen.
*/
#include <stdio.h>
void main( void )
{
char buffer[81];
int i, ch;
printf( "Enter a line: " );
/* Read in single line from "stdin": */
for( i = 0; (i < 80) && ((ch = getchar()) != EOF)
&& (ch != '\n'); i++ )
buffer[i] = (char)ch;
/* Terminate string with null character: */
buffer[i] = '\0';
printf( "%s\n", buffer );
}
Za automatsko dobijanje Super Moderatorskog statusa na Elitesecurity forumima pritisnite Alt+F4