Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Reading data from file

Reply
Thread Tools

Reading data from file

 
 
Krzysztof Kolago
Guest
Posts: n/a
 
      11-14-2003
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[i] = bufor[i];}
close(Plik);
for(int i = 0; i < dlugosc; i++){printf("%i ", Tablica[i]);}
printf("--- %i", dlugosc);
}

+Chris-


 
Reply With Quote
 
 
 
 
Karl Heinz Buchegger
Guest
Posts: n/a
 
      11-14-2003


Krzysztof Kolago wrote:
>
> 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" );


--
Karl Heinz Buchegger
http://www.velocityreviews.com/forums/(E-Mail Removed)
 
Reply With Quote
 
 
 
 
Krzysztof Kolago
Guest
Posts: n/a
 
      11-14-2003
Uzytkownik "Karl Heinz Buchegger" <(E-Mail Removed)> napisal w wiadomosci
news:(E-Mail Removed)...
>
>
> Krzysztof Kolago wrote:
> >
> > 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" );
>
>
> --
> Karl Heinz Buchegger
> (E-Mail Removed)


Thanks! It really works!

Chris


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Reading a file and resuming reading. Karim Ali Python 2 05-25-2007 02:04 PM
UnauthorizedAccessException when reading XML files (no problem when reading other file-types) blabla120@gmx.net ASP .Net 0 09-15-2006 02:08 PM
An Automated process of watching a network file folder, reading a file in it and deleting the file using ASP.NET ? Luis Esteban Valencia Muņoz ASP .Net 3 06-04-2005 10:56 AM
reading the DB vs. reading a text file...performance preference? Darrel ASP .Net 3 11-11-2004 02:27 PM
reading output file data as input data Stephen Moon Perl Misc 5 02-29-2004 02:16 PM



Advertisments