Reading data from file

K

Krzysztof Kolago

Hello!

I wrote program that should read binary file, but it read only a part of
it (13690 of 64KB). Maybe somebody can help me, and will tell me, how to
modificate program source. I need to read all bytes of the file!

My source:
#include <string.h>
#include <stdio.h>
#include <fcntl.h>
#include <io.h>

main() {

signed int Plik = open("tomo.raw", O_RDONLY);
unsigned char bufor[64*1024];
int Tablica[64*1024];
int dlugosc;

if(Plik < 0) return(1);
dlugosc = read(Plik,bufor,sizeof(bufor));
for(int i = 0; i < dlugosc; i++) {Tablica = bufor;}
close(Plik);
for(int i = 0; i < dlugosc; i++){printf("%i ", Tablica);}
printf("--- %i", dlugosc);
}

+Chris-
 
K

Karl Heinz Buchegger

Krzysztof said:
Hello!

I wrote program that should read binary file, but it read only a part of
it (13690 of 64KB). Maybe somebody can help me, and will tell me, how to
modificate program source. I need to read all bytes of the file!

Then read them!

read() may read fewer bytes then requested. But that's not a problem,
read() tells you how much could be read in one rush. Just subtract
what read() has already read from twhat was requested and do another
read() until you have read everything (read() returns 0 if end of file
was reached or -1 if there was an error)

ToRead = some_number;

while( ( HaveRead = read( Plik, bufor, ToRead ) ) > 0 ) {
// do something with the read bytes.
ToRead -= some_number;
}

if( HaveRead == -1 )
printf( "There was an error during read\n" );
 
K

Krzysztof Kolago

Uzytkownik "Karl Heinz Buchegger said:
Then read them!

read() may read fewer bytes then requested. But that's not a problem,
read() tells you how much could be read in one rush. Just subtract
what read() has already read from twhat was requested and do another
read() until you have read everything (read() returns 0 if end of file
was reached or -1 if there was an error)

ToRead = some_number;

while( ( HaveRead = read( Plik, bufor, ToRead ) ) > 0 ) {
// do something with the read bytes.
ToRead -= some_number;
}

if( HaveRead == -1 )
printf( "There was an error during read\n" );

Thanks! It really works!

Chris
 

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

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,534
Members
45,008
Latest member
Rahul737

Latest Threads

Top