Reading data from file

Discussion in 'C++' started by Krzysztof Kolago, Nov 14, 2003.

  1. 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-
    Krzysztof Kolago, Nov 14, 2003
    #1
    1. Advertising

  2. 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
    Karl Heinz Buchegger, Nov 14, 2003
    #2
    1. Advertising

  3. Uzytkownik "Karl Heinz Buchegger" <> napisal w wiadomosci
    news:...
    >
    >
    > 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
    >


    Thanks! It really works!

    Chris
    Krzysztof Kolago, Nov 14, 2003
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Darrel
    Replies:
    3
    Views:
    657
    Kevin Spencer
    Nov 11, 2004
  2. Replies:
    0
    Views:
    771
  3. Karim Ali

    Reading a file and resuming reading.

    Karim Ali, May 25, 2007, in forum: Python
    Replies:
    2
    Views:
    356
    Hrvoje Niksic
    May 25, 2007
  4. Stephen Moon

    reading output file data as input data

    Stephen Moon, Feb 28, 2004, in forum: Perl Misc
    Replies:
    5
    Views:
    161
    Tad McClellan
    Feb 29, 2004
  5. Replies:
    5
    Views:
    68
    Chris Angelico
    May 14, 2014
Loading...

Share This Page