these two samples let you create a 5 GB file and read its contents back.
/* #undef __ANSIC__ or ftell64() etc will not be declared */
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
static void print64u( unsigned long long x) {
unsigned long long quot = x / 1000;
if ( quot != 0) {
print64u( quot);
printf( ".%03u", ( unsigned int)( x % 1000));
}
else {
printf( "%u", ( unsigned int)( x % 1000));
}
}
int main( int argc, char* argv[]) {
FILE* fp;
off64_t size;
size_t i;
if ( argc != 2) {
printf( "Usage: %s <filename>\n", argv[ 0]);
return EXIT_FAILURE;
}
fp = fopen( argv[ 1], "rb");
if ( fp == NULL) {
printf( "fopen failed, errno = %d (%s)\n", errno, strerror( errno));
return EXIT_FAILURE;
}
/* verify the file size */
fseek64( fp, 0, SEEK_END);
size = ftell64( fp);
printf( "file size = ");
print64u(( unsigned long long) size);
puts( "");
fseek64( fp, 0, SEEK_SET);
if ( size != 5 * 1024 * 1024 * ( unsigned long long) 1024) {
fclose( fp);
puts( "file size != 5G");
return EXIT_FAILURE;
}
/* verify the data */
for ( i = 0; i < 5 * 1024 * 1024; ++i) {
char buffer[ 1024];
size_t bytes_read;
size_t j;
bytes_read = fread( buffer, 1, sizeof( buffer), fp);
if ( bytes_read != sizeof( buffer)) {
fclose( fp);
printf( "bytes_read != sizeof( buffer), %u %u\n", bytes_read, sizeof( buffer));
return EXIT_FAILURE;
}
if (( i + 1) %( 1024 * 1024) == 0) {
printf( "Read %u MB\n", ( i + 1) / 1024);
}
if ( buffer[ 0] != ( char) ( i / ( 1024 * 1024))) {
fclose( fp);
puts( "Unexpected data (1)");
return EXIT_FAILURE;
}
for ( j = 1; j < sizeof( buffer); ++j) {
if ( buffer[ j] != ( char) j) break;
}
if ( j != sizeof( buffer)) {
fclose( fp);
puts( "Unexpected data (2)");
return EXIT_FAILURE;
}
}
fclose( fp);
puts( "Verified");
return EXIT_SUCCESS;
}
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
void NXThreadYield(void);
int main( int argc, char* argv[]) {
FILE* fp;
char data[ 1024];
size_t i;
if ( argc != 2) {
printf( "Usage: %s <filename>\n", argv[ 0]);
return EXIT_FAILURE;
}
fp = fopen( argv[ 1], "wb");
if ( fp == NULL) {
printf( "fopen failed, errno = %d (%s)\n", errno, strerror( errno));
return EXIT_FAILURE;
}
/* initialize the buffer */
for ( i = 1; i < sizeof( data); ++i) {
data[ i] = ( char) i;
}
/* write 5G */
for ( i = 0; i < 5 * 1024 * 1024; ) {
size_t bytes_written;
if ( i % ( 1024 * 1024) == 0) {
data[ 0] = ( char) ( i / ( 1024 * 1024));
}
bytes_written = fwrite( data, 1, sizeof( data), fp);
if ( bytes_written != sizeof( data)) {
fclose( fp);
printf( "bytes_written != sizeof( data): %u %u\n", bytes_written, sizeof( data));
return EXIT_FAILURE;
}
++i;
NXThreadYield();
delay( 0);
if ( i % ( 1024 * 1024) == 0) {
printf( "Written %u MB\n", i / 1024);
}
}
fclose( fp);
puts( "Created");
return EXIT_SUCCESS;
}
© 2009 Novell, Inc. All Rights Reserved.